/* 
 * WMM-Application:
 * single raised strip: dispersion curves,
 * modes and propagation constants for varying width
 */

/*
 * 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 Wgpl  1.3  // vacuum wavelength 
#define Wgph  0.7  // rib height

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

	g.hx(0) = 0.0;
	g.hx(1) = Wgph;

	g.hy(0) = -w/2.0;
	g.hy(1) =  w/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;
}

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

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

	p.ini_d_alpha   = 0.01;
	p.ini_N_alpha   = 20;
	p.ini_alpha_max = 2.0;

	p.ini_steps = 50;
	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 = 2.5;

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

	return p;
}

int main()
{
//	Polarization pol = QTE;
	Polarization pol = QTM;
	Fcomp fc;	
	WMM_Parameters par = pardef();
	int m;
	WMM_ModeArray ma;
	WMM_ModeArray modes;
	Waveguide wg;
	int nfms, nfma;
	// files for propagation constants
	char pcnam[20] = "__pc__";
	int w;
	double width;
	double neffmax;

	fc = principalcomp(pol);
	pcnam[0] = poltochar1(pol);
	pcnam[1] = poltochar2(pol);
	if(pol == QTE) { neffmax = 2.11; }
	if(pol == QTM) { neffmax = 2.10; }

//	for(w=0; w<=50; w+=1)
	for(w=0; w<=50; w+=25)
	{
// define the waveguide 
		width = 0.7+w*0.1; 
		wg = wgdef(width);

		modes.clear();

// compute symmetrical modes
		nfms = WMM_modeanalysis(wg, pol, SYM, Wgpns, neffmax, 
					par, dig10(w), dig1(w), ma);
// append propagation constants to appropriate file
		for(m=0; m<=nfms-1; ++m)
		{
			pcnam[4] = 's';
			pcnam[5] = '0'+m;
			apptoxyf(pcnam, width, ma(m).beta);
			fprintf(stderr, "\n -> sm%d(%g) %g\n", 
			                m, width, ma(m).beta);
		}
// store symmetrical modes 
		modes.merge(ma);
		ma.clear();

// compute antisymmetrical modes
		nfma = WMM_modeanalysis(wg, pol, ASY, Wgpns, neffmax, 
					par, dig10(w), dig1(w), ma);
// append propagation constants to appropriate file
		for(m=0; m<=nfma-1; ++m)
		{
			pcnam[4] = 'a';
			pcnam[5] = '0'+m;
			apptoxyf(pcnam, width, ma(m).beta);
			fprintf(stderr, "\n -> am%d(%g) %g\n",
					m , width, ma(m).beta);
		}
// store antisymmetrical modes 
		modes.merge(ma);
		ma.clear();

// save the entire mode set to disk, sort first 
		modes.sort();
		modes.write_def(dig10(w), dig1(w));
	}
	return 0;
}
