Skip to content

Commit

Permalink
Merge pull request #22 from KAHR-Alpha/feature/GUI_FDTD_parametric_st…
Browse files Browse the repository at this point in the history
…ructure

GUI fdtd parametric structure
  • Loading branch information
KAHR-Alpha authored Dec 28, 2023
2 parents bfaab46 + 2ed572e commit 9da3d64
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Todo:
- Tangent vectors managements for Selene
- Implement the Lambertian, Guassian and ABg scattering models ( see "What scattering models are available in OpticStudio?" )

0.12
- The FDTD GUI can now load structures that have parameters

0.11.1
- Fixed materials loading in CLI FDTD
- Fixed the use of the output directory for sensors in GUI FDTD
Expand Down
9 changes: 9 additions & 0 deletions samples/fdtd/structures/basic_parametric.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare_parameter("px",200e-9)
declare_parameter("py",300e-9)
declare_parameter("pz",400e-9)

lx=px
ly=py
lz=pz

default_material(0)
21 changes: 18 additions & 3 deletions src/GUI/FDTD/gui_fdtd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,9 +774,24 @@ void FDTD_Frame::reconstruct_tree()
if(!fdtd_parameters.structure->script.empty())
tree->AppendItem(structure_ID,fdtd_parameters.structure->script.generic_string());
else tree->AppendItem(structure_ID,"None");
tree->AppendItem(structure_ID,"Dx="+add_unit(fdtd_parameters.Dx));
tree->AppendItem(structure_ID,"Dy="+add_unit(fdtd_parameters.Dy));
tree->AppendItem(structure_ID,"Dz="+add_unit(fdtd_parameters.Dz));
tree->AppendItem(structure_ID,"Dx= "+add_unit(fdtd_parameters.Dx));
tree->AppendItem(structure_ID,"Dy= "+add_unit(fdtd_parameters.Dy));
tree->AppendItem(structure_ID,"Dz= "+add_unit(fdtd_parameters.Dz));

if(!fdtd_parameters.structure->parameter_name.empty())
{
wxTreeItemId tmp=tree->AppendItem(structure_ID,"Parameters");

for(std::size_t i=0;i<fdtd_parameters.structure->parameter_name.size();i++)
{
std::stringstream strm;

strm<<fdtd_parameters.structure->parameter_name[i]<<"= ";
strm<<fdtd_parameters.structure->parameter_value[i];

tree->AppendItem(tmp,strm.str());
}
}

// Incidence

Expand Down
8 changes: 7 additions & 1 deletion src/GUI/FDTD/gui_fdtd.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ class FDTD_Mode_Dialog: public wxDialog
NamedTextCtrl<int> *Nt_points;

// Structure
wxScrolledWindow *structure_panel;
NamedTextCtrl<std::string> *structure;
wxButton *structure_edit_btn;
NamedTextCtrl<double> *dx_ctrl,*dy_ctrl,*dz_ctrl;
LengthSelector *dx_ctrl,*dy_ctrl,*dz_ctrl;
wxPanel *parameters_panel;
wxStaticBoxSizer *parameters_sizer;
std::vector<NamedTextCtrl<double>*> parameters;
std::vector<std::string> parameter_names;

// Incidence
wxChoice *polarization;
Expand Down Expand Up @@ -129,6 +134,7 @@ class FDTD_Mode_Dialog: public wxDialog
void evt_edit_structure(wxCommandEvent &event);
void evt_load_structure(wxCommandEvent &event);
void evt_material_change(wxCommandEvent &event);
void evt_structure_parameter(wxCommandEvent &event);
void evt_ok(wxCommandEvent &event);
void evt_output_directory(wxCommandEvent &event);
void evt_time(wxCommandEvent &event);
Expand Down
108 changes: 95 additions & 13 deletions src/GUI/FDTD/gui_fdtd_mode_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void FDTD_Mode_Dialog::FDTD_Mode_Dialog_Materials(wxNotebook *book,int target_pa

void FDTD_Mode_Dialog::FDTD_Mode_Dialog_Structure(wxNotebook *book,int target_panel)
{
wxPanel *structure_panel=new wxPanel(book);
structure_panel=new wxScrolledWindow(book);
wxBoxSizer *structure_sizer=new wxBoxSizer(wxVERTICAL);

// - Script
Expand All @@ -537,20 +537,58 @@ void FDTD_Mode_Dialog::FDTD_Mode_Dialog_Structure(wxNotebook *book,int target_pa

// - Discretization

wxBoxSizer *bottom_sizer=new wxBoxSizer(wxHORIZONTAL);

wxStaticBoxSizer *structure_discr_sizer=new wxStaticBoxSizer(wxVERTICAL,structure_panel,"Discretization");
dx_ctrl=new NamedTextCtrl<double>(structure_discr_sizer->GetStaticBox(),"Dx: ",data->Dx);
dy_ctrl=new NamedTextCtrl<double>(structure_discr_sizer->GetStaticBox(),"Dy: ",data->Dy);
dz_ctrl=new NamedTextCtrl<double>(structure_discr_sizer->GetStaticBox(),"Dz: ",data->Dz);
dx_ctrl=new LengthSelector(structure_discr_sizer->GetStaticBox(),"Dx: ",data->Dx);
dy_ctrl=new LengthSelector(structure_discr_sizer->GetStaticBox(),"Dy: ",data->Dy);
dz_ctrl=new LengthSelector(structure_discr_sizer->GetStaticBox(),"Dz: ",data->Dz);

dx_ctrl->Bind(EVT_LENGTH_SELECTOR,&FDTD_Mode_Dialog::evt_structure_parameter,this);
dy_ctrl->Bind(EVT_LENGTH_SELECTOR,&FDTD_Mode_Dialog::evt_structure_parameter,this);
dz_ctrl->Bind(EVT_LENGTH_SELECTOR,&FDTD_Mode_Dialog::evt_structure_parameter,this);

structure_discr_sizer->Add(dx_ctrl,wxSizerFlags().Expand());
structure_discr_sizer->Add(dy_ctrl,wxSizerFlags().Expand());
structure_discr_sizer->Add(dz_ctrl,wxSizerFlags().Expand());

structure_sizer->Add(structure_discr_sizer);
bottom_sizer->Add(structure_discr_sizer);

// - Parameters

parameters_panel=new wxPanel(structure_panel);
parameters_sizer=new wxStaticBoxSizer(wxVERTICAL,parameters_panel,"Parameters");

if(data->structure->script.empty())
{
parameters_panel->Hide();
}
else
{
data->structure->finalize();
parameter_names=data->structure->parameter_name;

for(std::size_t i=0;i<data->structure->parameter_name.size();i++)
{
NamedTextCtrl<double> *param=new NamedTextCtrl(parameters_sizer->GetStaticBox(),
data->structure->parameter_name[i]+" : ",
data->structure->parameter_value[i]);
param->Bind(EVT_NAMEDTXTCTRL,&FDTD_Mode_Dialog::evt_structure_parameter,this);

parameters.push_back(param);
parameters_sizer->Add(param,wxSizerFlags().Expand());
}
}

parameters_panel->SetSizer(parameters_sizer);
bottom_sizer->Add(parameters_panel);

structure_sizer->Add(bottom_sizer);

structure_panel->SetSizer(structure_sizer);
book->AddPage(structure_panel,"Structure");


if(target_panel==1) book->SetSelection(N_panels); N_panels++;
}

Expand Down Expand Up @@ -605,6 +643,45 @@ void FDTD_Mode_Dialog::evt_load_structure(wxCommandEvent &event)

new_structure=true;

std::filesystem::path struct_path=structure->get_value();
struct_path=std::filesystem::absolute(struct_path);

// Cleanup

for(NamedTextCtrl<double> *param : parameters)
param->Destroy();

parameters.clear();

// Loading the new parameters

Structure loader(struct_path);
loader.finalize();

parameter_names=loader.parameter_name;

for(std::size_t i=0;i<loader.parameter_name.size();i++)
{
NamedTextCtrl<double> *param=new NamedTextCtrl(parameters_sizer->GetStaticBox(),
loader.parameter_name[i]+" : ",
loader.parameter_value[i]);
param->Bind(EVT_NAMEDTXTCTRL,&FDTD_Mode_Dialog::evt_structure_parameter,this);

parameters.push_back(param);
parameters_sizer->Add(param,wxSizerFlags().Expand());
}

if(loader.parameter_name.empty())
{
parameters_panel->Hide();
}
else
{
parameters_panel->Show();
}

structure_panel->FitInside();

event.Skip();
}

Expand All @@ -617,6 +694,13 @@ void FDTD_Mode_Dialog::evt_material_change(wxCommandEvent &event)
event.Skip();
}


void FDTD_Mode_Dialog::evt_structure_parameter(wxCommandEvent &event)
{
new_structure=true;
}


void FDTD_Mode_Dialog::evt_ok(wxCommandEvent &event)
{
// Main
Expand Down Expand Up @@ -667,16 +751,14 @@ void FDTD_Mode_Dialog::evt_ok(wxCommandEvent &event)
// Structure
data->structure->script=structure->get_value();
data->structure->script=std::filesystem::absolute(data->structure->script);

data->structure->parameter_name.clear();
data->structure->parameter_value.clear();

if(data->Dx!=dx_ctrl->get_value() ||
data->Dy!=dy_ctrl->get_value() ||
data->Dz!=dz_ctrl->get_value() )
for(std::size_t i=0;i<parameters.size();i++)
{
data->Dx=dx_ctrl->get_value();
data->Dy=dy_ctrl->get_value();
data->Dz=dz_ctrl->get_value();

new_structure=true;
data->structure->parameter_name.push_back(parameter_names[i]);
data->structure->parameter_value.push_back(parameters[i]->get_value());
}

// Materials
Expand Down
3 changes: 2 additions & 1 deletion src/common/filehdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ void PathManager::retrieve_executable_path()
char buffer[10000];

#ifdef _WIN32
GetModuleFileNameA(NULL,buffer,buffer_size);
HMODULE handle=GetModuleHandleA(NULL);
GetModuleFileNameA(handle,buffer,buffer_size);

executable_path=buffer;
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/structure/lua_structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,21 @@ int structure_declare_parameter(lua_State *L)
Structure *p_struct=get_structure_pointer(L);

bool found=false;

// Locating in the script parameters

std::size_t k=vector_locate(found,p_struct->parameter_name,parameter_name);

if(!found)
{
p_struct->parameter_name.push_back(parameter_name);
p_struct->parameter_value.push_back(parameter_default_value);
}

// Locating in the input parameters

k=vector_locate(found,p_struct->parameter_name,parameter_name);

if(found) lua_pushnumber(L,p_struct->parameter_value[k]);
else lua_pushnumber(L,parameter_default_value);

Expand Down
35 changes: 35 additions & 0 deletions tests/fdtd/parametric_structure.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
structure=Structure("../../samples/fdtd/structures/basic_parametric.lua")

fdtd=MODE("fdtd_normal")
fdtd:structure(structure)

lx=fdtd:Lx()
ly=fdtd:Ly()
lz=fdtd:Lz()

if lx~=200e-9 or ly~=300e-9 or lz~=400e-9 then
print("Failed initial value")
print("lx " .. lx)
print("ly " .. ly)
print("lz " .. lz)
fail_test()
end

structure:parameter("px",220e-9)
structure:parameter("py",330e-9)
structure:parameter("pz",440e-9)

fdtd=MODE("fdtd_normal")
fdtd:structure(structure)

lx=fdtd:Lx()
ly=fdtd:Ly()
lz=fdtd:Lz()

if lx~=220e-9 or ly~=330e-9 or lz~=440e-9 then
print("Failed modification")
print("lx " .. lx)
print("ly " .. ly)
print("lz " .. lz)
fail_test()
end

0 comments on commit 9da3d64

Please sign in to comment.