# clean up the environment
clear;
select("EME");
delete("EME");
select("mesh_left_taper");
delete("mesh_left_taper");
select("mesh_right_top_taper");
delete("mesh_right_top_taper");
select("mesh_right_bottom_taper");
delete("mesh_right_bottom_taper");
select("monitor_field_profile");
delete("monitor_field_profile");
deletesweep("sweep_wavelength");
deletesweep("sweep_s_parameter_matrix");

# add EigenMode Expansion Solver
addeme;
# set dimension
## EME
### EME setup
set("x min", -10e-6);
set("y", 0);
set("y span", 8e-6);
set("z", 0.11e-6);
set("z span", 3e-6);
# set cell properties
set("number of modes for all cell groups", 20);
set("allow custom eigensolver settings", 1);
set("display cells", 1);
set("number of cell groups", 3);
set("group spans", [10e-6; 25e-6; 10e-6]); #m
set("cells", [10; 1; 10]);
set("subcell method", [1; 0; 1]);   # 0 = none,  1 = CVCS
set("modes", [10; 50; 10]);
### Transverse mesh settings
set("mesh cells y", 60);
set("mesh cells z", 60);
### Boundary conditions
set("z min bc", "Symmetric");
### Advanced options
set("pml kappa", 1);
## Ports
# set up ports: port 1
select("EME::Ports::port_1");
### Geometry
set("use full simulation span", 1);
set("y", 0);
set("y span", 2e-6);
set("z", 0);
set("z span", 4e-6);
set("mode selection", "fundamental mode");
### EME port
seteigensolver("number of trial modes", 1);
# set up ports: port 2
select("EME::Ports::port_2");
### Geometry
set("use full simulation span", 0);
set("y", 1.3e-6);
set("y span", 2e-6);
set("z", 0);
set("z span", 4e-6);
set("mode selection", "fundamental mode");
### EME port
seteigensolver("number of trial modes", 1);
# set up ports: port 3
setactivesolver("EME");
addemeport;
### Geometry
select("EME::Ports::port_3");
set("port location", "right");
set("use full simulation span", 0);
set("y", -1.3e-6);
set("y span", 2e-6);
set("z", 0);
set("z span", 4e-6);
set("mode selection", "fundamental mode");
### EME port
seteigensolver("number of trial modes", 1);
#run;

# add Mesh
## left taper
addmesh;
set("name", "mesh_left_taper");
### Geometry
# set dimension
set("x", -5e-6);
set("x span", 10e-6);
set("y", 0);
set("y span", 1.2e-6);
set("z", 0.11e-6);
set("z span", 0.22e-6);
### General
# enable in Y and Z direction and disable in X directions
set("override x mesh", 0);
set("override y mesh", 1);
set("override z mesh", 1);
# restrict mesh by defining maximum step size
set("set maximum mesh step", 1);
set("dy", 20e-9);
set("dz", 20e-9);
## right top taper
addmesh;
set("name", "mesh_right_top_taper");
### Geometry
# set dimension
set("x", 30e-6);
set("x span", 10e-6);
set("y", 1.3e-6);
set("y span", 1.2e-6);
set("z", 0.11e-6);
set("z span", 0.22e-6);
### General
# enable in Y and Z direction and disable in X directions
set("override x mesh", 0);
set("override y mesh", 1);
set("override z mesh", 1);
# restrict mesh by defining maximum step size
set("set maximum mesh step", 1);
set("dy", 20e-9);
set("dz", 20e-9);
## right bottom taper
addmesh;
set("name", "mesh_right_bottom_taper");
### Geometry
# set dimension
set("x", 30e-6);
set("x span", 10e-6);
set("y", -1.3e-6);
set("y span", 1.2e-6);
set("z", 0.11e-6);
set("z span", 0.22e-6);
### General
# enable in Y and Z direction and disable in X directions
set("override x mesh", 0);
set("override y mesh", 1);
set("override z mesh", 1);
# restrict mesh by defining maximum step size
set("set maximum mesh step", 1);
set("dy", 20e-9);
set("dz", 20e-9);

# add Monitor
## field profile
setactivesolver("EME");
addemeprofile;
set("name", "monitor_field_profile");
### Geometry
# set dimension
set("x", 15e-6);
set("x span", 90e-6);
set("x resolution", 1000);
set("y", 0e-6);
set("y span", 16e-6);
set("z", 0.11e-6);

# add Sweep
## wavelength
### Parameters
addsweep(0);
setsweep("sweep", "name", "sweep_wavelength");
setsweep("sweep_wavelength", "type", "Ranges");
setsweep("sweep_wavelength", "Number of points", 11); 
# define the parameter thickness
para = struct;
para.Name = "wavelength";
para.Parameter = "::model::EME::wavelength";
para.Type = "Length";
para.Start = 1.5e-6;
para.Stop = 1.6e-6;
para.Units = "microns";
# add the parameter taper width to the sweep
addsweepparameter("sweep_wavelength", para);
# Results
# define results
result = struct;
result.Name = "s-matrix";
result.Result = "::model::EME::user s matrix";
# add the results s-matrix to the sweep
addsweepresult("sweep_wavelength", result);
# S-Parameter Matrix
addsweep(3);
## S-Matrix Setup
setsweep("s-parameter sweep", "name", "sweep_s_parameter_matrix");
setsweep("sweep_s_parameter_matrix", "Calculate group delay", 1);
setsweep("sweep_s_parameter_matrix", "Number of points", 10); 
## Export Setup
setsweep("sweep_s_parameter_matrix", "Include group delay", 1);
# run sweep
runsweep("sweep_wavelength");
runsweep("sweep_s_parameter_matrix");

#save simulation model file
save("roMMI1x2");