-
Notifications
You must be signed in to change notification settings - Fork 156
/
openems.cpp
1399 lines (1241 loc) · 40.6 KB
/
openems.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2010-2015 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/>.
*/
#include "openems.h"
#include <iomanip>
#include <iostream>
#include <fstream>
#include "tools/array_ops.h"
#include "tools/signal.h"
#include "tools/useful.h"
#include "FDTD/operator_cylinder.h"
#include "FDTD/operator_cylindermultigrid.h"
#include "FDTD/engine_multithread.h"
#include "FDTD/operator_multithread.h"
#include "FDTD/extensions/operator_ext_excitation.h"
#include "FDTD/extensions/operator_ext_tfsf.h"
#include "FDTD/extensions/operator_ext_mur_abc.h"
#include "FDTD/extensions/operator_ext_upml.h"
#include "FDTD/extensions/operator_ext_lorentzmaterial.h"
#include "FDTD/extensions/operator_ext_lumpedRLC.h"
#include "FDTD/extensions/operator_ext_conductingsheet.h"
#include "FDTD/extensions/operator_ext_steadystate.h"
#include "FDTD/extensions/engine_ext_steadystate.h"
#include "FDTD/engine_interface_fdtd.h"
#include "FDTD/engine_interface_cylindrical_fdtd.h"
#include "Common/processvoltage.h"
#include "Common/processcurrent.h"
#include "Common/processfieldprobe.h"
#include "Common/processmodematch.h"
#include "Common/processfields_td.h"
#include "Common/processfields_fd.h"
#include "Common/processfields_sar.h"
#include <hdf5.h> // only for H5get_libversion()
#include <boost/version.hpp> // only for BOOST_LIB_VERSION
#include <vtkVersion.h>
//external libs
#include "tinyxml.h"
#include "ContinuousStructure.h"
#include "CSPropProbeBox.h"
#include "CSPrimBox.h"
#include "CSPropDumpBox.h"
using namespace std;
namespace po = boost::program_options;
double CalcDiffTime(timeval t1, timeval t2)
{
double s_diff = t1.tv_sec - t2.tv_sec;
s_diff += (t1.tv_usec-t2.tv_usec)*1e-6;
return s_diff;
}
openEMS::openEMS()
{
setlocale(LC_NUMERIC, "en_US.UTF-8");
FDTD_Op=NULL;
FDTD_Eng=NULL;
Eng_Ext_SSD=NULL;
m_CSX=NULL;
PA=NULL;
CylinderCoords = false;
Enable_Dumps = true;
DebugMat = false;
DebugOp = false;
m_debugCSX = false;
m_debugBox = m_debugPEC = m_no_simulation = false;
m_DumpStats = false;
endCrit = 1e-6;
m_OverSampling = 4;
m_CellConstantMaterial=false;
m_engine = EngineType_Multithreaded; //default engine type
m_engine_numThreads = 0;
m_Abort = false;
m_Exc = 0;
m_TS_method=3;
m_TS=0;
m_TS_fac=1.0;
m_maxTime=0.0;
for (int n=0;n<6;++n)
{
m_BC_type[n] = 0;
m_PML_size[n] = 8;
m_Mur_v_ph[n] = 0;
}
collectCommandLineArguments();
}
openEMS::~openEMS()
{
Reset();
}
void openEMS::Reset()
{
if (PA) PA->DeleteAll();
delete PA;
PA=0;
delete FDTD_Eng;
FDTD_Eng=0;
delete FDTD_Op;
FDTD_Op=0;
delete m_CSX;
m_CSX=0;
delete m_Exc;
m_Exc=0;
}
void openEMS::collectCommandLineArguments()
{
// register our supported options to g_settings
g_settings.appendOptionDesc(optionDesc());
g_settings.appendOptionDesc(g_settings.optionDesc());
}
po::options_description
openEMS::optionDesc()
{
po::options_description optdesc("Options");
optdesc.add_options()
(
"help,h",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
showUsage();
std::exit(0);
}
),
"Show this help message and exit"
)
(
"disable-dumps",
po::bool_switch()->notifier(
[&](bool val) {
if (!val) return;
cout << "openEMS - force-disabling all field dumps" << endl;
SetEnableDumps(!val);
}
),
"Disable all field dumps for faster simulation"
)
(
"debug-material",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dumping material to 'material_dump.vtk'" << endl;
DebugMaterial();
}
),
"Dump material distribution to a vtk file for debugging"
)
(
"debug-PEC",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dumping PEC info to 'PEC_dump.vtk'" << endl;
DebugPEC();
}
),
"Dump metal distribution to a vtk file for debugging"
)
(
"debug-operator",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dumping operator to 'operator_dump.vtk'" << endl;
DebugOperator();
}
),
"Dump operator to vtk file for debugging"
)
(
"debug-boxes",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dumping boxes to 'box_dump*.vtk'" << endl;
DebugBox();
}
),
"Dump e.g. probe boxes to vtk file for debugging"
)
(
"debug-CSX",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dumping CSX geometry to 'debugCSX.xml'" << endl;
DebugCSX();
}
),
"Write CSX geometry file to debugCSX.xml"
)
(
"engine",
po::value<std::string>()->default_value("fastest")->notifier(
[&](std::string val)
{
if (val == "fastest")
{
// default, don't show console output
m_engine = EngineType_Multithreaded;
}
else if (val == "basic")
{
cout << "openEMS - enabled basic engine" << endl;
m_engine = EngineType_Basic;
}
else if (val == "sse")
{
cout << "openEMS - enabled sse engine" << endl;
m_engine = EngineType_SSE;
}
else if (val == "sse-compressed")
{
cout << "openEMS - enabled compressed sse engine" << endl;
m_engine = EngineType_SSE_Compressed;
}
else if (val == "multithreaded")
{
cout << "openEMS - enabled multithreading" << endl;
m_engine = EngineType_Multithreaded;
}
}
),
"Choose engine type \n\n"
" fastest: \tfastest available engine (default)\n"
" basic: \tbasic FDTD engine\n"
" sse: \tengine using SSE vector extensions\n"
" sse-compressed: \tengine using compressed "
"operator + sse vector extensions\n"
" multithreaded: \tengine using compressed "
#ifdef MPI_SUPPORT
"operator + sse vector extensions + MPI + multithreading\n"
#else
"operator + sse vector extensions + multithreading\n"
#endif
)
(
"numThreads",
po::value<int>()->default_value(0)->notifier(
[&](int val)
{
this->SetNumberOfThreads(val);
if (val > 0)
cout << "openEMS - fixed number of threads: "
<< m_engine_numThreads << endl;
}
),
"Force use n threads for multithreaded engine "
"(needs: --engine=multithreaded)"
)
(
"no-simulation",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - disabling simulation => preprocessing only" << endl;
m_no_simulation = true;
}
),
"only run preprocessing; do not simulate"
)
(
"dump-statistics",
po::bool_switch()->notifier(
[&](bool val)
{
if (!val) return;
cout << "openEMS - dump simulation statistics to '"
<< __OPENEMS_RUN_STAT_FILE__ << "' and '"
<< __OPENEMS_STAT_FILE__ << "'" << endl;
m_DumpStats = true;
}
),
"dump simulation statistics to '" __OPENEMS_RUN_STAT_FILE__
"' and '" __OPENEMS_STAT_FILE__ "'"
);
return optdesc;
}
void openEMS::showUsage()
{
cout << " Usage: openEMS <FDTD_XML_FILE> [<options>...]" << endl;
cout << " ";
g_settings.showOptionUsage(cout);
}
// used by Python binding when running as a shared library
void openEMS::SetLibraryArguments(std::vector<std::string> allOptions)
{
g_settings.parseLibraryArguments(allOptions);
}
void openEMS::SetNumberOfThreads(int val)
{
if ((val<0) || (val>boost::thread::hardware_concurrency()))
val = boost::thread::hardware_concurrency();
m_engine_numThreads = val;
}
string openEMS::GetExtLibsInfo(string prefix)
{
stringstream str;
str << prefix << "Used external libraries:" << endl;
str << prefix << "\t" << ContinuousStructure::GetInfoLine(true) << endl;
// libhdf5
unsigned int major, minor, release;
if (H5get_libversion( &major, &minor, &release ) >= 0)
{
str << prefix << "\t" << "hdf5 -- Version: " << major << '.' << minor << '.' << release << endl;
str << prefix << "\t" << " compiled against: " H5_VERS_INFO << endl;
}
// tinyxml
str << prefix << "\t" << "tinyxml -- compiled against: " << TIXML_MAJOR_VERSION << '.' << TIXML_MINOR_VERSION << '.' << TIXML_PATCH_VERSION << endl;
// fparser
str << prefix << "\t" << "fparser" << endl;
// boost
str << prefix << "\t" << "boost -- compiled against: " << BOOST_LIB_VERSION << endl;
//vtk
str << prefix << "\t" << "vtk -- Version: " << vtkVersion::GetVTKMajorVersion() << "." << vtkVersion::GetVTKMinorVersion() << "." << vtkVersion::GetVTKBuildVersion() << endl;
str << prefix << "\t" << " compiled against: " << VTK_VERSION << endl;
return str.str();
}
void openEMS::WelcomeScreen()
{
#if defined(_LP64) || defined(_WIN64)
string bits = "64bit";
#else
string bits = "32bit";
#endif
cout << " ---------------------------------------------------------------------- " << endl;
cout << " | openEMS " << bits << " -- version " << GIT_VERSION << endl;
cout << " | (C) 2010-2023 Thorsten Liebig <thorsten.liebig@gmx.de> GPL license" << endl;
cout << " ---------------------------------------------------------------------- " << endl;
cout << openEMS::GetExtLibsInfo("\t") << endl;
}
bool openEMS::SetupBoundaryConditions()
{
FDTD_Op->SetBoundaryCondition(m_BC_type); //operator only knows about PEC and PMC, everything else is defined by extensions (see below)
/**************************** create all operator/engine extensions here !!!! **********************************/
for (int n=0; n<6; ++n)
{
FDTD_Op->SetBCSize(n, 0);
if (m_BC_type[n]==2) //Mur-ABC
{
FDTD_Op->SetBCSize(n, 1);
Operator_Ext_Mur_ABC* op_ext_mur = new Operator_Ext_Mur_ABC(FDTD_Op);
op_ext_mur->SetDirection(n/2,n%2);
if (m_Mur_v_ph[n]>0)
op_ext_mur->SetPhaseVelocity(m_Mur_v_ph[n]);
FDTD_Op->AddExtension(op_ext_mur);
}
if (m_BC_type[n]==3)
FDTD_Op->SetBCSize(n, m_PML_size[n]);
}
//create the upml
Operator_Ext_UPML::Create_UPML(FDTD_Op, m_BC_type, m_PML_size, string());
return true;
}
Engine_Interface_FDTD* openEMS::NewEngineInterface(int multigridlevel)
{
Operator_CylinderMultiGrid* op_cyl_mg = dynamic_cast<Operator_CylinderMultiGrid*>(FDTD_Op);
while (op_cyl_mg && multigridlevel>0)
{
int mgl = op_cyl_mg->GetMultiGridLevel();
if (mgl==multigridlevel)
{
if (g_settings.GetVerboseLevel()>0)
cout << __func__ << ": Operator with requested multi-grid level found." << endl;
return new Engine_Interface_Cylindrical_FDTD(op_cyl_mg);
}
Operator_Cylinder* op_cyl_inner = op_cyl_mg->GetInnerOperator();
op_cyl_mg = dynamic_cast<Operator_CylinderMultiGrid*>(op_cyl_inner);
if (op_cyl_mg==NULL) //inner most operator reached
{
if (g_settings.GetVerboseLevel()>0)
cout << __func__ << ": Operator with highest multi-grid level chosen." << endl;
return new Engine_Interface_Cylindrical_FDTD(op_cyl_inner);
}
// try next level
}
Operator_Cylinder* op_cyl = dynamic_cast<Operator_Cylinder*>(FDTD_Op);
if (op_cyl)
return new Engine_Interface_Cylindrical_FDTD(op_cyl);
Operator_sse* op_sse = dynamic_cast<Operator_sse*>(FDTD_Op);
if (op_sse)
return new Engine_Interface_SSE_FDTD(op_sse);
return new Engine_Interface_FDTD(FDTD_Op);
}
void openEMS::SetVerboseLevel(int level)
{
g_settings.SetVerboseLevel(level);
}
bool openEMS::SetupProcessing()
{
//*************** setup processing ************//
if (g_settings.GetVerboseLevel()>0)
cout << "Setting up processing..." << endl;
unsigned int Nyquist = FDTD_Op->GetExcitationSignal()->GetNyquistNum();
PA = new ProcessingArray(Nyquist);
double start[3];
double stop[3];
bool l_MultiBox = false;
vector<CSProperties*> Probes = m_CSX->GetPropertyByType(CSProperties::PROBEBOX);
for (size_t i=0; i<Probes.size(); ++i)
{
CSPropProbeBox* pb = Probes.at(i)->ToProbeBox();
if (!pb)
continue;
//check whether one or more probe boxes are defined
l_MultiBox = (pb->GetQtyPrimitives()>1);
for (size_t nb=0; nb<pb->GetQtyPrimitives(); ++nb)
{
CSPrimitives* prim = pb->GetPrimitive(nb);
if (prim!=NULL)
{
double bnd[6] = {0,0,0,0,0,0};
prim->GetBoundBox(bnd,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];
ProcessIntegral* proc = NULL;
if (pb->GetProbeType()==0)
{
CSPrimBox* box = prim->ToBox();
if (!(box) || box->GetDimension()!=1)
{
cerr << "openEMS::SetupProcessing: Error: Probe primitive type or dimension not suitable ... skipping probe " << pb->GetName() << endl;
continue;
}
// use the direction and coordinates of the box
for (int n=0;n<3;++n)
{
start[n] = box->GetCoord(2*n);
stop[n] = box->GetCoord(2*n+1);
}
ProcessVoltage* procVolt = new ProcessVoltage(NewEngineInterface());
proc=procVolt;
}
else if (pb->GetProbeType()==1)
{
ProcessCurrent* procCurr = new ProcessCurrent(NewEngineInterface());
proc=procCurr;
}
else if (pb->GetProbeType()==2)
proc = new ProcessFieldProbe(NewEngineInterface(),0);
else if (pb->GetProbeType()==3)
proc = new ProcessFieldProbe(NewEngineInterface(),1);
else if ((pb->GetProbeType()==10) || (pb->GetProbeType()==11))
{
ProcessModeMatch* pmm = new ProcessModeMatch(NewEngineInterface());
pmm->SetFieldType(pb->GetProbeType()-10);
pmm->SetModeFunction(0,pb->GetAttributeValue("ModeFunctionX"));
pmm->SetModeFunction(1,pb->GetAttributeValue("ModeFunctionY"));
pmm->SetModeFunction(2,pb->GetAttributeValue("ModeFunctionZ"));
proc = pmm;
}
else
{
cerr << "openEMS::SetupFDTD: Warning: Probe type " << pb->GetProbeType() << " of property '" << pb->GetName() << "' is unknown..." << endl;
continue;
}
if (CylinderCoords)
proc->SetMeshType(Processing::CYLINDRICAL_MESH);
if ((pb->GetProbeType()==1) || (pb->GetProbeType()==3))
{
proc->SetDualTime(true);
proc->SetDualMesh(true);
}
if (pb->GetProbeType()==11)
proc->SetDualTime(true);
proc->SetProcessInterval(Nyquist/m_OverSampling);
if (pb->GetStartTime()>0 || pb->GetStopTime()>0)
proc->SetProcessStartStopTime(pb->GetStartTime(), pb->GetStopTime());
proc->AddFrequency(pb->GetFDSamples());
proc->GetNormalDir(pb->GetNormalDir());
if (l_MultiBox==false)
proc->SetName(pb->GetName());
else
proc->SetName(pb->GetName(),nb);
proc->DefineStartStopCoord(start,stop);
if (g_settings.showProbeDiscretization())
proc->ShowSnappedCoords();
proc->SetWeight(pb->GetWeighting());
PA->AddProcessing(proc);
prim->SetPrimitiveUsed(true);
}
}
}
vector<CSProperties*> DumpProps = m_CSX->GetPropertyByType(CSProperties::DUMPBOX);
for (size_t i=0; i<DumpProps.size(); ++i)
{
ProcessFields* ProcField=NULL;
//check whether one or more probe boxes are defined
l_MultiBox = (DumpProps.at(i)->GetQtyPrimitives()>1);
for (size_t nb=0; nb<DumpProps.at(i)->GetQtyPrimitives(); ++nb)
{
CSPrimitives* prim = DumpProps.at(i)->GetPrimitive(nb);
if (prim!=NULL)
{
double bnd[6] = {0,0,0,0,0,0};
prim->GetBoundBox(bnd,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];
CSPropDumpBox* db = DumpProps.at(i)->ToDumpBox();
if (db)
{
if ((db->GetDumpType()>=0) && (db->GetDumpType()<=5))
ProcField = new ProcessFieldsTD(NewEngineInterface(db->GetMultiGridLevel()));
else if ((db->GetDumpType()>=10) && (db->GetDumpType()<=15))
ProcField = new ProcessFieldsFD(NewEngineInterface(db->GetMultiGridLevel()));
else if ( ((db->GetDumpType()>=20) && (db->GetDumpType()<=22)) || (db->GetDumpType()==29) )
{
ProcessFieldsSAR* procSAR = new ProcessFieldsSAR(NewEngineInterface(db->GetMultiGridLevel()));
ProcField = procSAR;
string method = db->GetAttributeValue("SAR_Method");
if (!method.empty())
procSAR->SetSARAveragingMethod(method);
// use (center)-cell based conductivity only
procSAR->SetUseCellConductivity(true);
}
else
cerr << "openEMS::SetupFDTD: unknown dump box type... skipping!" << endl;
if (ProcField)
{
ProcField->SetEnable(Enable_Dumps);
ProcField->SetProcessInterval(Nyquist/m_OverSampling);
if (db->GetStopTime()>0 || db->GetStartTime()>0)
ProcField->SetProcessStartStopTime(db->GetStartTime(), db->GetStopTime());
if ((db->GetDumpType()==1) || (db->GetDumpType()==11))
{
ProcField->SetDualTime(true);
//make dualMesh the default mesh for h-field dumps, maybe overwritten by interpolation type (node-interpolation)
ProcField->SetDualMesh(true);
}
if (db->GetDumpType()>=10)
{
ProcField->AddFrequency(db->GetFDSamples());
ProcField->SetDumpType((ProcessFields::DumpType)(db->GetDumpType()-10));
}
else
ProcField->SetDumpType((ProcessFields::DumpType)db->GetDumpType());
if (db->GetDumpType()==20)
ProcField->SetDumpType(ProcessFields::SAR_LOCAL_DUMP);
if (db->GetDumpType()==21)
ProcField->SetDumpType(ProcessFields::SAR_1G_DUMP);
if (db->GetDumpType()==22)
ProcField->SetDumpType(ProcessFields::SAR_10G_DUMP);
if (db->GetDumpType()==29)
ProcField->SetDumpType(ProcessFields::SAR_RAW_DATA);
//SetupMaterialStorages() has previewed storage needs... refresh here to prevent cleanup!!!
if ( ProcField->NeedPermittivity() && Enable_Dumps)
FDTD_Op->SetMaterialStoreFlags(0,true);
if ( ProcField->NeedConductivity() && Enable_Dumps)
FDTD_Op->SetMaterialStoreFlags(1,true);
if ( ProcField->NeedPermeability() && Enable_Dumps)
FDTD_Op->SetMaterialStoreFlags(2,true);
ProcField->SetDumpMode((Engine_Interface_Base::InterpolationType)db->GetDumpMode());
ProcField->SetFileType((ProcessFields::FileType)db->GetFileType());
if (CylinderCoords)
ProcField->SetMeshType(Processing::CYLINDRICAL_MESH);
if (db->GetSubSampling())
for (int n=0; n<3; ++n)
ProcField->SetSubSampling(db->GetSubSampling(n),n);
if (db->GetOptResolution())
for (int n=0; n<3; ++n)
ProcField->SetOptResolution(db->GetOptResolution(n),n);
if (l_MultiBox==false)
ProcField->SetName(db->GetName());
else
ProcField->SetName(db->GetName(),nb);
ProcField->SetFileName(ProcField->GetName());
ProcField->DefineStartStopCoord(start,stop);
if (g_settings.showProbeDiscretization())
ProcField->ShowSnappedCoords();
PA->AddProcessing(ProcField);
prim->SetPrimitiveUsed(true);
}
}
}
}
}
return true;
}
bool openEMS::SetupMaterialStorages()
{
vector<CSProperties*> DumpProps = m_CSX->GetPropertyByType(CSProperties::DUMPBOX);
for (size_t i=0; i<DumpProps.size(); ++i)
{
CSPropDumpBox* db = DumpProps.at(i)->ToDumpBox();
if (!db)
continue;
if (db->GetQtyPrimitives()==0)
continue;
//check for current density dump types
if ( ((db->GetDumpType()==2) || (db->GetDumpType()==12) || // current density storage
(db->GetDumpType()==20) || (db->GetDumpType()==21) || (db->GetDumpType()==22)) && // SAR dump types
Enable_Dumps )
FDTD_Op->SetMaterialStoreFlags(1,true); //tell operator to store kappa material data
if ( ((db->GetDumpType()==4) || (db->GetDumpType()==14)) || Enable_Dumps) // electric flux density storage
FDTD_Op->SetMaterialStoreFlags(0,true); //tell operator to store epsR material data
if ( ((db->GetDumpType()==5) || (db->GetDumpType()==15)) || Enable_Dumps) // magnetic flux density storage
FDTD_Op->SetMaterialStoreFlags(2,true); //tell operator to store mueR material data
}
return true;
}
void openEMS::SetupCylinderMultiGrid(std::string val)
{
m_CC_MultiGrid.clear();
m_CC_MultiGrid = SplitString2Double(val,',');
}
bool openEMS::SetupOperator()
{
if (CylinderCoords)
{
if (m_CC_MultiGrid.size()>0)
{
FDTD_Op = Operator_CylinderMultiGrid::New(m_CC_MultiGrid, m_engine_numThreads);
if (FDTD_Op==NULL)
FDTD_Op = Operator_Cylinder::New(m_engine_numThreads);
}
else
FDTD_Op = Operator_Cylinder::New(m_engine_numThreads);
}
else if (m_engine == EngineType_SSE)
{
FDTD_Op = Operator_sse::New();
}
else if (m_engine == EngineType_SSE_Compressed)
{
FDTD_Op = Operator_SSE_Compressed::New();
}
else if (m_engine == EngineType_Multithreaded)
{
FDTD_Op = Operator_Multithread::New(m_engine_numThreads);
}
else
{
FDTD_Op = Operator::New();
}
return true;
}
void openEMS::Set_BC_Type(int idx, int type)
{
if ((idx<0) || (idx>5))
return;
m_BC_type[idx] = type;
}
int openEMS::Get_BC_Type(int idx)
{
if ((idx<0) || (idx>5))
return -1;
return m_BC_type[idx];
}
void openEMS::Set_BC_PML(int idx, unsigned int size)
{
if ((idx<0) || (idx>5))
return;
m_BC_type[idx] = 3;
m_PML_size[idx] = size;
}
int openEMS::Get_PML_Size(int idx)
{
if ((idx<0) || (idx>5))
return -1;
if (m_BC_type[idx]!=3)
return -1; // return -1 if BC was *not* a PML
return m_PML_size[idx];
}
void openEMS::Set_Mur_PhaseVel(int idx, double val)
{
if ((idx<0) || (idx>5))
return;
m_Mur_v_ph[idx] = val;
}
bool openEMS::ParseFDTDSetup(std::string file)
{
Reset();
if (g_settings.GetVerboseLevel()>0)
cout << "Read openEMS xml file: " << file << " ..." << endl;
TiXmlDocument doc(file);
if (!doc.LoadFile())
{
cerr << "openEMS: Error File-Loading failed!!! File: " << file << endl;
exit(-1);
}
if (g_settings.GetVerboseLevel()>0)
cout << "Read openEMS Settings..." << endl;
TiXmlElement* openEMSxml = doc.FirstChildElement("openEMS");
if (openEMSxml==NULL)
{
cerr << "Can't read openEMS ... " << endl;
exit(-1);
}
TiXmlElement* FDTD_Opts = openEMSxml->FirstChildElement("FDTD");
if (FDTD_Opts==NULL)
{
cerr << "Can't read openEMS FDTD Settings... " << endl;
exit(-1);
}
if (g_settings.GetVerboseLevel()>0)
cout << "Read Geometry..." << endl;
ContinuousStructure* csx = new ContinuousStructure();
string EC(csx->ReadFromXML(openEMSxml));
if (EC.empty()==false)
cerr << EC << endl;
this->SetCSX(csx);
return this->Parse_XML_FDTDSetup(FDTD_Opts);
}
bool openEMS::Parse_XML_FDTDSetup(TiXmlElement* FDTD_Opts)
{
double dhelp=0;
FDTD_Opts->QueryDoubleAttribute("NumberOfTimesteps",&dhelp);
if (dhelp<0)
this->SetNumberOfTimeSteps(0);
else
this->SetNumberOfTimeSteps((unsigned int)dhelp);
int ihelp = 0;
FDTD_Opts->QueryIntAttribute("CylinderCoords",&ihelp);
if (ihelp==1)
{
this->SetCylinderCoords(true);
const char* cchelp = FDTD_Opts->Attribute("MultiGrid");
if (cchelp!=NULL)
this->SetupCylinderMultiGrid(string(cchelp));
}
dhelp = 0;
FDTD_Opts->QueryDoubleAttribute("MaxTime",&dhelp);
if (dhelp>0)
this->SetMaxTime(dhelp);
dhelp = 0;
FDTD_Opts->QueryDoubleAttribute("endCriteria",&dhelp);
if (dhelp==0)
this->SetEndCriteria(1e-6);
else
this->SetEndCriteria(dhelp);
ihelp = 0;
FDTD_Opts->QueryIntAttribute("OverSampling",&ihelp);
if (ihelp>1)
this->SetOverSampling(ihelp);
// check for cell constant material averaging
if (FDTD_Opts->QueryIntAttribute("CellConstantMaterial",&ihelp)==TIXML_SUCCESS)
this->SetCellConstantMaterial(ihelp==1);
TiXmlElement* BC = FDTD_Opts->FirstChildElement("BoundaryCond");
if (BC==NULL)
{
cerr << "Can't read openEMS boundary cond Settings... " << endl;
exit(-3);
}
// const char* tmp = BC->Attribute("PML_Grading");
// string pml_gradFunc;
// if (tmp)
// pml_gradFunc = string(tmp);
string bound_names[] = {"xmin","xmax","ymin","ymax","zmin","zmax"};
string s_bc;
for (int n=0; n<6; ++n)
{
int EC = BC->QueryIntAttribute(bound_names[n].c_str(),&ihelp);
if (EC==TIXML_SUCCESS)
{
this->Set_BC_Type(n, ihelp);
continue;
}
if (EC==TIXML_WRONG_TYPE)
{
const char* tmp = BC->Attribute(bound_names[n].c_str());
if (tmp)
s_bc = string(tmp);
else
cerr << "openEMS::SetupBoundaryConditions: Warning, boundary condition for \"" << bound_names[n] << "\" unknown... set to PEC " << endl;
if (s_bc=="PEC")
this->Set_BC_Type(n, 0);
else if (s_bc=="PMC")
this->Set_BC_Type(n, 1);
else if (s_bc=="MUR")
this->Set_BC_Type(n, 2);
else if (strncmp(s_bc.c_str(),"PML_=",4)==0)
this->Set_BC_PML(n, atoi(s_bc.c_str()+4));
else
cerr << "openEMS::SetupBoundaryConditions: Warning, boundary condition for \"" << bound_names[n] << "\" unknown... set to PEC " << endl;
}
else
cerr << "openEMS::SetupBoundaryConditions: Warning, boundary condition for \"" << bound_names[n] << "\" not found... set to PEC " << endl;
}
//read general mur phase velocity
if (BC->QueryDoubleAttribute("MUR_PhaseVelocity",&dhelp) == TIXML_SUCCESS)
for (int n=0;n<6;++n)
this->Set_Mur_PhaseVel(n, dhelp);
string mur_v_ph_names[6] = {"MUR_PhaseVelocity_xmin", "MUR_PhaseVelocity_xmax", "MUR_PhaseVelocity_ymin", "MUR_PhaseVelocity_ymax", "MUR_PhaseVelocity_zmin", "MUR_PhaseVelocity_zmax"};
for (int n=0; n<6; ++n)
if (BC->QueryDoubleAttribute(mur_v_ph_names[n].c_str(),&dhelp) == TIXML_SUCCESS)
this->Set_Mur_PhaseVel(n, dhelp);
TiXmlElement* m_Excite_Elem = FDTD_Opts->FirstChildElement("Excitation");
if (!m_Excite_Elem)
{
cerr << "Excitation::setupExcitation: Error, can't read openEMS excitation settings... " << endl;
return false;
}
Excitation* exc = this->InitExcitation();
double f0=0, fc=0, f_max=0;
ihelp = -1;
m_Excite_Elem->QueryIntAttribute("Type",&ihelp);
switch (ihelp)
{
case Excitation::GaissianPulse:
m_Excite_Elem->QueryDoubleAttribute("f0",&f0);
m_Excite_Elem->QueryDoubleAttribute("fc",&fc);
exc->SetupGaussianPulse(f0, fc);
break;
case Excitation::Sinusoidal: // sinusoidal excite
m_Excite_Elem->QueryDoubleAttribute("f0",&f0);
exc->SetupSinusoidal(f0);
break;
case Excitation::DiracPulse:
FDTD_Opts->QueryDoubleAttribute("f_max",&f_max);
exc->SetupDiracPulse(f_max);
break;
case Excitation::Step:
FDTD_Opts->QueryDoubleAttribute("f_max",&f_max);
exc->SetupStepExcite(f_max);
break;
case Excitation::CustomExcite:
m_Excite_Elem->QueryDoubleAttribute("f0",&f0);
FDTD_Opts->QueryDoubleAttribute("f_max",&f_max);
exc->SetupCustomExcite(m_Excite_Elem->Attribute("Function"), f0, f_max);
break;
}
if (FDTD_Opts->QueryIntAttribute("TimeStepMethod",&ihelp)==TIXML_SUCCESS)
this->SetTimeStepMethod(ihelp);
if (FDTD_Opts->QueryDoubleAttribute("TimeStep",&dhelp)==TIXML_SUCCESS)
this->SetTimeStep(dhelp);
if (FDTD_Opts->QueryDoubleAttribute("TimeStepFactor",&dhelp)==TIXML_SUCCESS)
this->SetTimeStepFactor(dhelp);
return true;
}
void openEMS::SetGaussExcite(double f0, double fc)
{
this->InitExcitation();
m_Exc->SetupGaussianPulse(f0, fc);
}
void openEMS::SetSinusExcite(double f0)
{
this->InitExcitation();
m_Exc->SetupSinusoidal(f0);
}
void openEMS::SetDiracExcite(double f_max)
{
this->InitExcitation();
m_Exc->SetupDiracPulse(f_max);
}
void openEMS::SetStepExcite(double f_max)
{
this->InitExcitation();
m_Exc->SetupStepExcite(f_max);
}
void openEMS::SetCustomExcite(std::string str, double f0, double fmax)
{
this->InitExcitation();
m_Exc->SetupCustomExcite(str, f0, fmax);
}
Excitation* openEMS::InitExcitation()
{
delete m_Exc;
m_Exc = new Excitation();
return m_Exc;
}
void openEMS::SetCSX(ContinuousStructure* csx)
{
delete m_CSX;
m_CSX = csx;
}
int openEMS::SetupFDTD()
{
timeval startTime;
gettimeofday(&startTime,NULL);
Signal::SetupHandlerForSIGINT(SIGNAL_EXIT_FORCE);
if (m_CSX==NULL)
{
cerr << "openEMS::SetupFDTD: Error: CSXCAD is not set!" << endl;
Signal::SetupHandlerForSIGINT(SIGNAL_ORIGINAL);
return 3;
}
if (m_CSX==NULL)
{
cerr << "openEMS::SetupFDTD: Error: CSXCAD is not set!" << endl;
Signal::SetupHandlerForSIGINT(SIGNAL_ORIGINAL);
return 3;
}
std::string ec = m_CSX->Update();