/* 
 * WMM-Application:
 * a raised strip waveguide
 */

/*
 * WMM
 * Wave-matching method for mode analysis of dielectric waveguides
 * Manfred Lohmeyer 
 * University of Osnabrueck, Department of Physics
 * Barbarastrasse 7, D-49069 Osnabrueck, Germany
 * (1999)
 */

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"wmminc.h"

/* waveguide parameters */
#define Wgpns 1.95 // substrate refractive index
#define Wgpnf 2.2  // film refractive index
#define Wgpnc 1.0  // cover: air
#define Wgpw  2.0  // strip width
#define Wgph  0.7  // strip height
#define Wgpl  1.3  // vacuum wavelength 

/* waveguide definition */
Waveguide wgdef()
{
	Waveguide g(1, 1);

	g.hx(0) = 0.0;
	g.hx(1) = Wgph;
	g.hy(0) = -Wgpw/2.0;
	g.hy(1) =  Wgpw/2.0;

	g.n(0,0) = Wgpns;
	g.n(0,1) = Wgpns;
	g.n(0,2) = Wgpns;
	g.n(1,0) = Wgpnc;
	g.n(1,1) = Wgpnf;
	g.n(1,2) = Wgpnc;
	g.n(2,0) = Wgpnc;
	g.n(2,1) = Wgpnc;
	g.n(2,2) = Wgpnc;

	g.lambda = Wgpl;

	return g;
}

/* WMM analysis parameters */
WMM_Parameters pardef()
{
	WMM_Parameters p;

	p.vform = HXHY;
	p.vnorm = NRMMH;
	p.ccomp = CCALL;

	p.ini_d_alpha   = 0.05;
	p.ini_N_alpha   = 15;
	p.ini_alpha_max = 2.0;

	p.ini_steps = 30;
	p.ref_num   = 5;
	p.ref_exp   = 4.0;
	p.ref_sdf   = 0.5;

	p.fin_d_alpha   = 0.01;
	p.fin_N_alpha   = 30;
	p.fin_alpha_max = 3.0;

	p.btol   = 1.0e-7;
	p.mshift = 1.0e-10;

	return p;
}

/* semivectorial mode analysis,
   mode profile plots,
   handling of WMM_Mode and WMM_ModeArray objects */ 
int main()
{
	WMM_Parameters par = pardef();
	WMM_ModeArray s;
	WMM_ModeArray ma;
	WMM_Mode m;
	int j;
		
	// define the waveguide
	Waveguide wg = wgdef();

	// display window
	Rect display(-Wgph, -1.0*Wgpw, Wgph*1.5, 1.0*Wgpw);

	// s is to collect all found modes 
	s.clear();

	// semivectorial mode analysis, TE polarization, symmetrical modes 
	WMM_modeanalysis(wg, QTE, SYM, 0.0, 0.0, par, 'e', 's', ma);
	// mode profile plots,  
	// access to the WMM_Mode's in a WMM_ModeArray   
	for(j=0; j<=ma.num-1; ++j)
	{
		m = ma(j);
		m.mfile(EY, SQR, display, 75, 115, 'a'+j, '0', 'C');
	}
	// store the members of ma in s
	s.merge(ma);

	// semivectorial mode analysis, TE polarization, antisymmetrical modes 
	WMM_modeanalysis(wg, QTE, ASY, 0.0, 0.0, par, 'e', 'a', ma);
	// mode profile plots, shortened encoding
	for(j=0; j<=ma.num-1; ++j)
		ma(j).mfile(EY, SQR, display, 75, 115, 'a'+j, '0', 'C');
	s.merge(ma);

	// semivectorial mode analysis, TM polarization, symmetrical modes 
	WMM_modeanalysis(wg, QTM, SYM, 0.0, 0.0, par, 'm', 's', ma);
	// mode profile plots  
	for(j=0; j<=ma.num-1; ++j)
		ma(j).mfile(HY, SQR, display, 75, 115, 'a'+j, '0', 'C');
	s.merge(ma);

	// semivectorial mode analysis, TM polarization, antisymmetrical modes 
	WMM_modeanalysis(wg, QTM, ASY, 0.0, 0.0, par, 'm', 'a', ma);
	// mode profile plots  
	for(j=0; j<=ma.num-1; ++j)
		ma(j).mfile(HY, SQR, display, 75, 115, 'a'+j, '0', 'C');
	s.merge(ma);

	// sort the modes in s with respect to propagation constants
	s.sort();

	// write the abstract information on the guided modes of the strip 
	// to a file
	s.write_def('s', 't');

	// read it again
	s.read_def('s', 't');
	// ...

	return 0;
}
