2010-03-11 15:47:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Thorsten Liebig (Thorsten.Liebig@gmx.de)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-11 09:56:19 +00:00
|
|
|
#include "openems.h"
|
2010-03-15 19:50:49 +00:00
|
|
|
#include <iomanip>
|
2010-03-11 09:56:19 +00:00
|
|
|
#include "tools/array_ops.h"
|
|
|
|
#include "FDTD/operator.h"
|
|
|
|
#include "FDTD/engine.h"
|
|
|
|
#include "FDTD/processvoltage.h"
|
|
|
|
#include "FDTD/processcurrent.h"
|
|
|
|
#include "FDTD/processfields_td.h"
|
2010-03-25 14:08:54 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
2010-03-11 09:56:19 +00:00
|
|
|
|
|
|
|
//external libs
|
|
|
|
#include "tinyxml.h"
|
|
|
|
#include "ContinuousStructure.h"
|
|
|
|
|
2010-03-25 14:08:54 +00:00
|
|
|
double CalcDiffTime(timeval t1, timeval t2)
|
|
|
|
{
|
|
|
|
double s_diff = difftime(t1.tv_sec,t2.tv_sec);
|
|
|
|
s_diff += ((double)t1.tv_usec-(double)t2.tv_usec)*1e-6;
|
|
|
|
return s_diff;
|
|
|
|
}
|
|
|
|
|
2010-03-11 09:56:19 +00:00
|
|
|
openEMS::openEMS()
|
|
|
|
{
|
|
|
|
FDTD_Op=NULL;
|
|
|
|
FDTD_Eng=NULL;
|
|
|
|
PA=NULL;
|
2010-03-11 14:48:55 +00:00
|
|
|
Enable_Dumps = true;
|
2010-03-12 19:39:04 +00:00
|
|
|
DebugMat = false;
|
2010-03-17 22:16:41 +00:00
|
|
|
DebugOp = false;
|
2010-03-15 15:59:37 +00:00
|
|
|
endCrit = 1e-6;
|
2010-03-11 09:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
openEMS::~openEMS()
|
|
|
|
{
|
|
|
|
delete FDTD_Eng;
|
|
|
|
FDTD_Eng=NULL;
|
|
|
|
delete PA;
|
|
|
|
PA=NULL;
|
2010-03-26 07:21:07 +00:00
|
|
|
delete FDTD_Op;
|
|
|
|
FDTD_Op=NULL;
|
2010-03-11 09:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void openEMS::Reset()
|
|
|
|
{
|
|
|
|
delete FDTD_Op;
|
|
|
|
FDTD_Op=NULL;
|
|
|
|
delete FDTD_Eng;
|
|
|
|
FDTD_Eng=NULL;
|
|
|
|
if (PA) PA->DeleteAll();
|
|
|
|
delete PA;
|
|
|
|
PA=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int openEMS::SetupFDTD(const char* file)
|
|
|
|
{
|
|
|
|
if (file==NULL) return -1;
|
|
|
|
Reset();
|
2010-03-11 14:35:12 +00:00
|
|
|
double f0=0;
|
|
|
|
double fc=0;
|
2010-03-11 09:56:19 +00:00
|
|
|
int Excit_Type=0;
|
|
|
|
int bounds[6];
|
|
|
|
|
|
|
|
time_t startTime=time(NULL);
|
|
|
|
|
|
|
|
TiXmlDocument doc(file);
|
|
|
|
if (!doc.LoadFile())
|
|
|
|
{
|
|
|
|
cerr << "openEMS: Error File-Loading failed!!! File: " << file << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Read openEMS Settings..." << endl;
|
2010-03-12 20:14:17 +00:00
|
|
|
TiXmlElement* openEMSxml = doc.FirstChildElement("openEMS");
|
|
|
|
if (openEMSxml==NULL)
|
|
|
|
{
|
|
|
|
cerr << "Can't read openEMS ... " << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TiXmlElement* FDTD_Opts = openEMSxml->FirstChildElement("FDTD");
|
2010-03-11 09:56:19 +00:00
|
|
|
if (FDTD_Opts==NULL)
|
|
|
|
{
|
2010-03-12 20:14:17 +00:00
|
|
|
cerr << "Can't read openEMS FDTD Settings... " << endl;
|
2010-03-11 09:56:19 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
FDTD_Opts->QueryIntAttribute("NumberOfTimesteps",&NrTS);
|
2010-03-15 19:50:49 +00:00
|
|
|
FDTD_Opts->QueryDoubleAttribute("endCriteria",&endCrit);
|
|
|
|
if (endCrit==0)
|
|
|
|
endCrit=1e-6;
|
2010-03-11 09:56:19 +00:00
|
|
|
|
|
|
|
TiXmlElement* Excite = FDTD_Opts->FirstChildElement("Excitation");
|
|
|
|
if (Excite==NULL)
|
|
|
|
{
|
|
|
|
cerr << "Can't read openEMS Excitation Settings... " << endl;
|
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
Excite->QueryIntAttribute("Type",&Excit_Type);
|
2010-03-11 14:35:12 +00:00
|
|
|
if (Excit_Type==0)
|
|
|
|
{
|
|
|
|
Excite->QueryDoubleAttribute("f0",&f0);
|
|
|
|
Excite->QueryDoubleAttribute("fc",&fc);
|
|
|
|
}
|
2010-03-24 17:39:58 +00:00
|
|
|
else if (Excit_Type==1)
|
|
|
|
{
|
|
|
|
Excite->QueryDoubleAttribute("f0",&f0);
|
|
|
|
}
|
2010-03-11 09:56:19 +00:00
|
|
|
|
|
|
|
TiXmlElement* BC = FDTD_Opts->FirstChildElement("BoundaryCond");
|
|
|
|
if (BC==NULL)
|
|
|
|
{
|
|
|
|
cerr << "Can't read openEMS boundary cond Settings... " << endl;
|
|
|
|
exit(-3);
|
|
|
|
}
|
|
|
|
BC->QueryIntAttribute("xmin",&bounds[0]);
|
|
|
|
BC->QueryIntAttribute("xmax",&bounds[1]);
|
|
|
|
BC->QueryIntAttribute("ymin",&bounds[2]);
|
|
|
|
BC->QueryIntAttribute("ymax",&bounds[3]);
|
|
|
|
BC->QueryIntAttribute("zmin",&bounds[4]);
|
|
|
|
BC->QueryIntAttribute("zmax",&bounds[5]);
|
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Read Geometry..." << endl;
|
2010-03-11 09:56:19 +00:00
|
|
|
ContinuousStructure CSX;
|
2010-03-12 20:14:17 +00:00
|
|
|
string EC(CSX.ReadFromXML(openEMSxml));
|
2010-03-11 09:56:19 +00:00
|
|
|
if (EC.empty()==false)
|
|
|
|
{
|
|
|
|
cerr << EC << endl;
|
|
|
|
return(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PMC[6];
|
|
|
|
for (int n=0;n<6;++n)
|
|
|
|
PMC[n]=(bounds[n]==1);
|
|
|
|
|
|
|
|
//*************** setup operator ************//
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Create Operator..." << endl;
|
2010-03-11 09:56:19 +00:00
|
|
|
FDTD_Op = new Operator();
|
|
|
|
if (FDTD_Op->SetGeometryCSX(&CSX)==false) return(-1);
|
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
if (DebugMat)
|
|
|
|
{
|
|
|
|
FDTD_Op->DumpMaterial2File("material_dump.vtk");
|
|
|
|
}
|
2010-03-11 09:56:19 +00:00
|
|
|
FDTD_Op->CalcECOperator();
|
2010-03-17 22:16:41 +00:00
|
|
|
if (DebugOp)
|
|
|
|
{
|
|
|
|
FDTD_Op->DumpOperator2File("operator_dump.vtk");
|
|
|
|
}
|
2010-03-11 09:56:19 +00:00
|
|
|
|
2010-03-24 17:39:58 +00:00
|
|
|
fc = 0;
|
2010-03-11 09:56:19 +00:00
|
|
|
if (Excit_Type==0)
|
2010-03-11 14:35:12 +00:00
|
|
|
FDTD_Op->CalcGaussianPulsExcitation(f0,fc);
|
2010-03-24 17:39:58 +00:00
|
|
|
else if (Excit_Type==1)
|
|
|
|
FDTD_Op->CalcSinusExcitation(f0,NrTS);
|
2010-03-11 09:56:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
cerr << "openEMS: Excitation type is unknown" << endl;
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t OpDoneTime=time(NULL);
|
|
|
|
|
|
|
|
FDTD_Op->ShowSize();
|
|
|
|
|
|
|
|
FDTD_Op->ApplyMagneticBC(PMC);
|
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Nyquist number of timesteps: " << FDTD_Op->GetNyquistNum(f0+fc) << endl;
|
2010-03-11 14:35:12 +00:00
|
|
|
unsigned int Nyquist = FDTD_Op->GetNyquistNum(f0+fc);
|
2010-03-11 09:56:19 +00:00
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Creation time for operator: " << difftime(OpDoneTime,startTime) << " s" << endl;
|
2010-03-11 09:56:19 +00:00
|
|
|
|
|
|
|
//create FDTD engine
|
|
|
|
FDTD_Eng = new Engine(FDTD_Op);
|
|
|
|
|
|
|
|
time_t currTime = time(NULL);
|
|
|
|
|
|
|
|
//*************** setup processing ************//
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Setting up processing..." << endl;
|
2010-03-15 21:19:51 +00:00
|
|
|
PA = new ProcessingArray(Nyquist);
|
2010-03-11 09:56:19 +00:00
|
|
|
|
|
|
|
double start[3];
|
|
|
|
double stop[3];
|
|
|
|
vector<CSProperties*> Probes = CSX.GetPropertyByType(CSProperties::PROBEBOX);
|
|
|
|
for (size_t i=0;i<Probes.size();++i)
|
|
|
|
{
|
|
|
|
//only looking for one prim atm
|
|
|
|
CSPrimitives* prim = Probes.at(i)->GetPrimitive(0);
|
|
|
|
if (prim!=NULL)
|
|
|
|
{
|
|
|
|
bool acc;
|
|
|
|
double* bnd = prim->GetBoundBox(acc,true);
|
|
|
|
start[0]= bnd[0];start[1]=bnd[2];start[2]=bnd[4];
|
|
|
|
stop[0] = bnd[1];stop[1] =bnd[3];stop[2] =bnd[5];
|
|
|
|
CSPropProbeBox* pb = Probes.at(i)->ToProbeBox();
|
|
|
|
Processing* proc = NULL;
|
|
|
|
if (pb)
|
|
|
|
{
|
|
|
|
if (pb->GetProbeType()==0)
|
|
|
|
{
|
|
|
|
ProcessVoltage* procVolt = new ProcessVoltage(FDTD_Op,FDTD_Eng);
|
|
|
|
procVolt->OpenFile(pb->GetName());
|
|
|
|
proc=procVolt;
|
|
|
|
}
|
|
|
|
if (pb->GetProbeType()==1)
|
|
|
|
{
|
|
|
|
ProcessCurrent* procCurr = new ProcessCurrent(FDTD_Op,FDTD_Eng);
|
|
|
|
procCurr->OpenFile(pb->GetName());
|
|
|
|
proc=procCurr;
|
|
|
|
}
|
|
|
|
proc->SetProcessInterval(Nyquist/3); //three times as often as nyquist dictates
|
|
|
|
proc->DefineStartStopCoord(start,stop);
|
|
|
|
PA->AddProcessing(proc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete proc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<CSProperties*> DumpProps = CSX.GetPropertyByType(CSProperties::DUMPBOX);
|
|
|
|
for (size_t i=0;i<DumpProps.size();++i)
|
|
|
|
{
|
|
|
|
ProcessFieldsTD* ProcTD = new ProcessFieldsTD(FDTD_Op,FDTD_Eng);
|
2010-03-11 14:48:55 +00:00
|
|
|
ProcTD->SetEnable(Enable_Dumps);
|
2010-03-11 09:56:19 +00:00
|
|
|
ProcTD->SetProcessInterval(Nyquist/2); //twice as often as nyquist dictates
|
|
|
|
|
|
|
|
//only looking for one prim atm
|
|
|
|
CSPrimitives* prim = DumpProps.at(i)->GetPrimitive(0);
|
|
|
|
if (prim==NULL)
|
|
|
|
delete ProcTD;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool acc;
|
|
|
|
double* bnd = prim->GetBoundBox(acc);
|
|
|
|
start[0]= bnd[0];start[1]=bnd[2];start[2]=bnd[4];
|
|
|
|
stop[0] = bnd[1];stop[1] =bnd[3];stop[2] =bnd[5];
|
|
|
|
CSPropDumpBox* db = DumpProps.at(i)->ToDumpBox();
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
ProcTD->SetDumpType(db->GetDumpType());
|
|
|
|
ProcTD->SetDumpMode(db->GetDumpMode());
|
|
|
|
ProcTD->SetFilePattern(db->GetName());
|
|
|
|
ProcTD->DefineStartStopCoord(start,stop);
|
|
|
|
PA->AddProcessing(ProcTD);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
delete ProcTD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void openEMS::RunFDTD()
|
|
|
|
{
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Running FDTD engine... this may take a while... grab a coup of coffee?!?" << endl;
|
2010-03-25 14:08:54 +00:00
|
|
|
|
|
|
|
timeval currTime;
|
|
|
|
gettimeofday(&currTime,NULL);
|
|
|
|
timeval startTime = currTime;
|
|
|
|
timeval prevTime= currTime;
|
2010-03-15 15:59:37 +00:00
|
|
|
ProcessFields ProcField(FDTD_Op,FDTD_Eng);
|
|
|
|
double maxE=0,currE=0;
|
|
|
|
double change=1;
|
|
|
|
int prevTS=0,currTS=0;
|
|
|
|
double speed = (double)FDTD_Op->GetNumberCells()/1e6;
|
|
|
|
double t_diff;
|
2010-03-11 09:56:19 +00:00
|
|
|
//*************** simulate ************//
|
|
|
|
int step=PA->Process();
|
|
|
|
if ((step<0) || (step>NrTS)) step=NrTS;
|
2010-03-15 15:59:37 +00:00
|
|
|
while ((FDTD_Eng->GetNumberOfTimesteps()<NrTS) && (change>endCrit))
|
2010-03-11 09:56:19 +00:00
|
|
|
{
|
|
|
|
FDTD_Eng->IterateTS(step);
|
|
|
|
step=PA->Process();
|
2010-03-12 19:39:04 +00:00
|
|
|
// cout << " do " << step << " steps; current: " << eng.GetNumberOfTimesteps() << endl;
|
2010-03-15 15:59:37 +00:00
|
|
|
currTS = FDTD_Eng->GetNumberOfTimesteps();
|
|
|
|
if ((step<0) || (step>NrTS - currTS)) step=NrTS - currTS;
|
|
|
|
|
2010-03-25 14:08:54 +00:00
|
|
|
gettimeofday(&currTime,NULL);
|
|
|
|
|
|
|
|
t_diff = CalcDiffTime(currTime,prevTime);
|
2010-03-15 15:59:37 +00:00
|
|
|
if (t_diff>4)
|
|
|
|
{
|
|
|
|
currE = ProcField.CalcTotalEnergy();
|
2010-03-15 21:19:51 +00:00
|
|
|
if (currE>maxE)
|
2010-03-15 15:59:37 +00:00
|
|
|
maxE=currE;
|
2010-03-25 14:08:54 +00:00
|
|
|
cout << "[@" << setw(8) << (int)CalcDiffTime(currTime,startTime) << "s] Timestep: " << setw(12) << currTS << " (" << setw(6) << setprecision(2) << std::fixed << (double)currTS/(double)NrTS*100.0 << "%)" ;
|
2010-03-15 19:50:49 +00:00
|
|
|
cout << " with currently " << setw(6) << setprecision(1) << std::fixed << speed*(double)(currTS-prevTS)/t_diff << " MCells/s" ;
|
2010-03-15 21:19:51 +00:00
|
|
|
if (maxE)
|
|
|
|
change = currE/maxE;
|
|
|
|
cout << " --- Energy: ~" << setw(6) << setprecision(2) << std::scientific << currE << " (decrement: " << setw(6) << setprecision(2) << std::fixed << fabs(10.0*log10(change)) << "dB)" << endl;
|
2010-03-15 15:59:37 +00:00
|
|
|
prevTime=currTime;
|
|
|
|
prevTS=currTS;
|
|
|
|
}
|
2010-03-11 09:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//*************** postproc ************//
|
2010-03-15 15:59:37 +00:00
|
|
|
prevTime = currTime;
|
2010-03-25 14:08:54 +00:00
|
|
|
gettimeofday(&currTime,NULL);
|
2010-03-11 09:56:19 +00:00
|
|
|
|
2010-03-25 14:08:54 +00:00
|
|
|
t_diff = CalcDiffTime(currTime,startTime);
|
2010-03-11 09:56:19 +00:00
|
|
|
|
2010-03-12 19:39:04 +00:00
|
|
|
cout << "Time for " << FDTD_Eng->GetNumberOfTimesteps() << " iterations with " << FDTD_Op->GetNumberCells() << " cells : " << t_diff << " sec" << endl;
|
2010-03-15 15:59:37 +00:00
|
|
|
cout << "Speed: " << speed*(double)FDTD_Eng->GetNumberOfTimesteps()/t_diff << " MCells/s " << endl;
|
2010-03-11 09:56:19 +00:00
|
|
|
}
|