CG  Version 25
Maxwell.h
Go to the documentation of this file.
1 #ifndef MAXWELL_H
2 #define MAXWELL_H
3 
4 #include "Overture.h"
5 #include "ArraySimple.h"
6 #include "UnstructuredMapping.h"
7 #include "InterfaceInfo.h"
8 
9 // #include OV_STD_INCLUDE(map)
10 #define KK_DEBUG
11 #include "DBase.hh"
12 using namespace DBase;
13 
14 
15 class GL_GraphicsInterface;
16 class GUIState;
17 class MappedGridOperators;
18 class OGFunction;
19 class Ogshow;
20 class DialogData;
21 class ShowFileReader;
23 class Oges;
24 
25 class Maxwell
26 {
27  public:
28 
30  {
40  box,
42  perturbedSquare, // square with random perturbations
43  perturbedBox, // box with random perturbations
45  unknown
46  };
47 
49  {
54  defaultUnstructured
55  };
56 
57  // This next enum should match bcDefineFortranInclude.h
59  {
60  periodic=0,
66  interfaceBoundaryCondition, // for the interface between two regions with different properties
67  abcEM2, // absorbing BC, Engquist-Majda order 2
68  abcPML, // perfectly matched layer
69  abc3, // future absorbing BC
70  abc4, // future absorbing BC
71  abc5, // future absorbing BC
72  rbcNonLocal, // radiation BC, non-local
73  rbcLocal, // radiation BC, local
74  numberOfBCNames
75  };
76  static aString bcName[numberOfBCNames];
77 
79  {
80  useAllPeriodicBoundaryConditions=0,
83  useGeneralBoundaryConditions
84  };
85 
86  // This next enum should match icDefineFortranInclude.h
88  {
89  defaultInitialCondition=0,
98  gaussianIntegralInitialCondition, // from Tom Hagstrom
101  numberOfInitialConditionNames
102  };
103 
104  // This next enum should match forcingDefineFortranInclude.h
106  {
114  numberOfForcingNames
115  };
116 
118  {
121  pulseTwilightZone
122  } twilightZoneOption;
123 
124 
125  // Here are known solutions
127  {
140  eigenfunctionsOfASphereKnownSolution // not implemented yet
141  } knownSolutionOption;
142 
143 
144 
145  // kkc enum used in getCGField, {EH}Field is mainly used by the unstructured algorithms
147  {
155  H001
156  };
157 
159  {
160  defaultMethod=0,
164  dsiMatVec, // Matrix-Vector DSI implementation
165  nfdtd, // non-orthogonal FDTD
166  sosup // second-order system upwind scheme
167  };
168 
169 
171  {
172  defaultTimeStepping=0,
176  modifiedEquationTimeStepping
177  } timeSteppingMethod;
178 
179 
180  Maxwell();
181  ~Maxwell();
182 
183 
184 
185  int addDissipation( int current, real t, real dt, realMappedGridFunction *fields, const Range & C );
186 
187  void addFilter( int current, real t, real dt );
188 
189  static int addPrefix(const aString label[], const aString & prefix, aString cmd[], const int maxCommands);
190 
191  bool adjustBoundsForPML( MappedGrid & mg, Index Iv[3], int extra=0 );
192 
193  void advanceC( int current, real t, real dt, realMappedGridFunction *fields );
194 
195  void advanceDSI( int current, real t, real dt); // kkc method that uses cg and check grid types (for hybrid grids)
196 
197  void advanceFDTD( int numberOfStepsTaken, int current, real t, real dt );
198 
199  // new version
200  void advanceNew( int current, real t, real dt, realMappedGridFunction *fields );
201 
202  void advanceNFDTD( int numberOfStepsTaken, int current, real t, real dt );
203 
204  void advanceSOSUP( int numberOfStepsTaken, int current, real t, real dt );
205 
206  void advanceUnstructuredDSI( int current, real t, real dt, realMappedGridFunction *fields );
207 
208  void advanceUnstructuredDSIMV( int current, real t, real dt, realMappedGridFunction *fields );
209 
210  void assignBoundaryConditions( int option, int grid, real t, real dt, realMappedGridFunction & u,
211  realMappedGridFunction & uOld, int current );
212 
213  void assignInitialConditions(int current, real t, real dt);
214 
215  void assignInterfaceBoundaryConditions( int current, real t, real dt );
216 
217 
218  int buildRunTimeDialog();
219 
220  int buildVariableDissipation();
221 
222 
223  void checkArrays(const aString & label);
224 
225  void computeDissipation( int current, real t, real dt );
226 
227  int computeIntensity(int current, real t, real dt, int stepNumber, real nextTimeToPlot);
228 
229  void computeNumberOfStepsAndAdjustTheTimeStep(const real & t,
230  const real & tFinal,
231  const real & nextTimeToPlot,
232  int & numberOfSubSteps,
233  real & dtNew,
234  const bool & adjustTimeStep =true );
235 
236  int computeTimeStep();
237 
238  // Define material regions and bodies that are defined by a mask
239  int defineRegionsAndBodies();
240 
241  void displayBoundaryConditions(FILE *file = stdout);
242 
243  realCompositeGridFunction& getAugmentedSolution(int current, realCompositeGridFunction & v, const real t);
244 
245  bool getBoundsForPML( MappedGrid & mg, Index Iv[3], int extra=0 );
246 
247  void getChargeDensity( int current, real t, realCompositeGridFunction &u, int component=0 );
248 
249  void getChargeDensity( real t, realMappedGridFunction & u, int component =0 );
250 
251  void getEnergy( int current, real t, real dt );
252 
253  void getErrors( int current, real t, real dt );
254 
255  void getField( real x, real y, real t, real *eField );
256 
257  int getForcing( int current, int grid, realArray & u , real t, real dt, int option=0 );
258 
259  void getMaxDivergence( const int current, real t,
260  realCompositeGridFunction *pu=NULL, int component=0,
261  realCompositeGridFunction *pDensity=NULL, int rhoComponent=0,
262  bool computeMaxNorms= true );
263 
264  static real getMaxValue(real value, int processor=-1); // get max over all processors
265  static int getMaxValue(int value, int processor=-1);
266 
267  static real getMinValue(real value, int processor=-1); // get min over all processors
268  static int getMinValue(int value, int processor=-1);
269 
270  int getValuesFDTD(int option, int *iparam, int current, real t, real dt, realCompositeGridFunction *v= NULL );
271 
272  void initializeKnownSolution();
273 
274  void initializeInterfaces();
275 
276  int initializePlaneMaterialInterface();
277 
278  int initializeRadiationBoundaryConditions();
279 
280  int interactiveUpdate(GL_GraphicsInterface &gi );
281 
282  void outputHeader();
283 
284  int outputResults( int current, real t, real dt );
285 
286  int outputResultsAfterEachTimeStep( int current, real t, real dt, int stepNumber, real nextTimeToPlot );
287 
288  int plot(int current, real t, real dt );
289 
290  int printMemoryUsage(FILE *file = stdout );
291 
292  int printStatistics(FILE *file = stdout);
293 
294  // project the fields to satisfy div( eps E ) = rho
295  int project( int numberOfStepsTaken, int current, real t, real dt );
296 
297  // project the interpolation points to satisfy div( eps E ) = rho
298  int projectInterpolationPoints( int numberOfStepsTaken, int current, real t, real dt );
299 
300  void saveShow( int current, real t, real dt );
301 
302  int saveParametersToShowFile();
303 
304  int setBoundaryCondition( aString & answer, GL_GraphicsInterface & gi, IntegerArray & originalBoundaryCondition );
305 
306  int setupGrids();
307  int setupGridFunctions();
308 
309  int setupUserDefinedForcing();
310 
311  int setupUserDefinedInitialConditions();
312 
313  void smoothDivergence(realCompositeGridFunction & u, const int numberOfSmooths );
314 
315  int solve(GL_GraphicsInterface &gi );
316 
317  int updateProjectionEquation();
318 
319  bool usingPMLBoundaryConditions() const;
320 
321  int updateShowFile(const aString & command= nullString, DialogData *interface =NULL );
322 
323 
324  int userDefinedForcing( realArray & f, int iparam[], real rparam[] );
325 
326  void userDefinedForcingCleanup();
327 
328  int userDefinedInitialConditions(int current, real t, real dt );
329 
330  void userDefinedInitialConditionsCleanup();
331 
332 protected:
333  int buildTimeSteppingOptionsDialog(DialogData & dialog );
334  int buildForcingOptionsDialog(DialogData & dialog );
335  int buildPlotOptionsDialog(DialogData & dialog );
336  int buildInputOutputOptionsDialog(DialogData & dialog );
337  int buildPdeParametersDialog(DialogData & dialog);
338 
339  int buildParametersDialog(DialogData & dialog ); // parameters that can be changed at run time.
340 
341  int saveSequenceInfo( real t0, RealArray & sequenceData );
342  int saveSequencesToShowFile();
343 
344 public: // should be protected:
345 
346  // The database is the new place to store parameters
347  mutable DataBase dbase;
348 
350  aString methodName;
354 
355  int ex,ey,ez,hx,hy,hz; // component numbers for Ex, Ey, Ez, Hx, ...
356  int ext,eyt,ezt,hxt,hyt,hzt; // component numbers for Ext, Eyt, Ezt, Hxt, ...
357  int rc; // component number for rho (in TZ functions)
358  int epsc,muc,sigmaEc,sigmaHc; // components for eps, mu, sigmaE and sigmaH in TZ functions
360  int ex10,ey10,ex01,ey01,hz11,numberOfComponentsCurvilinearGrid;
361 
362  real frequency;
363 
364  real eps,mu,c;
365  RealArray epsGrid,muGrid,cGrid,sigmaEGrid,sigmaHGrid; // holds variable coefficients that are constant on each grid
368  int materialInterfaceOption, interfaceEquationsOption;
370 
371  int numberOfMaterialRegions; // for variable coefficients -- number of piecewise constant regions
372  IntegerArray media; // for variable coefficients
373  RealArray epsv, muv, sigmaEv, sigmaHv; // for variable coefficients
374  int maskBodies; // mask bodies for the Yee scheme
375  intSerialArray *pBodyMask;
376  bool useTwilightZoneMaterials; // if true define eps, mu, .. using the twilight-zone functions
377 
378  std::vector<InterfaceInfo> interfaceInfo; // holds info and work-space for interfaces
379 
380  real kx,ky,kz; // plane wave wave-numbers
381  real pwc[6]; // plane wave coefficients (E and H)
382  enum
383  {
384  numberOfPlaneMaterialInterfaceCoefficients=33
385  };
386  real pmc[numberOfPlaneMaterialInterfaceCoefficients]; // plane material interface coefficients
387 
388  int nx[3];
389  real deltaT;
390  real xab[2][3];
391 
392  IntegerArray adjustFarFieldBoundariesForIncidentField; // subtract out the incident field before apply NRBC's
393 
394  real betaGaussianPlaneWave,x0GaussianPlaneWave,y0GaussianPlaneWave,z0GaussianPlaneWave;
395  real gaussianSourceParameters[5]; // gamma,omega,x0,y0,z0
396 
397  enum { maxNumberOfGaussianPulses=20};
399  real gaussianPulseParameters[maxNumberOfGaussianPulses][6]; // beta scale, exponent, x0,y0,z0
400 
401  enum { maxNumberOfGaussianChargeSources=10};
403  real gaussianChargeSourceParameters[maxNumberOfGaussianChargeSources][9]; // a,beta,p, x0,y0,z0, v0,v1,v2
404 
405  real cfl;
406  real tFinal,tPlot;
408  RealArray dxMinMax; // holds min and max grid spacings
409  real divEMax,gradEMax,divHMax,gradHMax;
412 
414  real artificialDissipation, artificialDissipationCurvilinear;
417 
418  // parameters for the high order filter:
419  bool applyFilter; // true : apply the high order filter
420  int orderOfFilter; // this means use default order
421  int filterFrequency; // apply filter every this many steps
422  int numberOfFilterIterations; // number of iterations in the filter
423  real filterCoefficient; // coefficient in the filter
424 
425  // parameters for new divergence cleaning method
428 
429  bool useChargeDensity; // if true, this problem includes a charge density
430  realCompositeGridFunction *pRho; // pointer to the charge density
431  realCompositeGridFunction *pPhi,*pF; // pointer to phi and f for projection
432  Oges *poisson; // Poisson solver for projection
433  bool projectFields; // if true, project the fields to have the correct divergence
434  int frequencyToProjectFields; // apply the project every this many steps
436  int numberOfInitialProjectionSteps; // always project this first number of steps
438 
439  int numberOfProjectionIterations; // number of iterations in the projection solve
441  bool useConservativeDivergence; // evaluate the conservative form of the divergence
443  bool projectInterpolation; // if true, project interp. pts so that div(E)=0
444 
445  bool solveForElectricField,solveForMagneticField; // in 3D we can solve for either or both of E and H
446 
449  bool plotDivergence, plotErrors, plotDissipation, plotScatteredField, plotTotalField, plotRho, plotEnergyDensity;
450  bool plotIntensity; // plot time averaged intensity for time periodic problems
451  int intensityOption; // 0=compute from time average, 1=compute from just two solutions
452  real omegaTimeHarmonic; // the intensity computation needs to know the frequency in time, omegaTimeHarmonic = omega/(2 pi)
453  real intensityAveragingInterval; // average intensity over this many periods
454 
455  bool plotHarmonicElectricFieldComponents; // plot Er and Ei assuming : E(x,t) = Er(x)*cos(w*t) + Ei(x)*sin(w*t)
456 
461  RealArray maximumError, solutionNorm;
462  int errorNorm; // set to 1 or 2 to compute L1 and L2 error norms
463 
464  real totalEnergy,initialTotalEnergy;
465 
466  real radiusForCheckingErrors; // if >0 only check errors in a disk (sphere) of this radius
467 
468  RealArray initialConditionBoundingBox; // limit initial conditions to lie in this box
469  real boundingBoxDecayExponent; // initial condition has a smooth transition outside the bounding box
470 
473 
474  real chevronFrequency, chevronAmplitude;
475  real cylinderRadius, cylinderAxisStart,cylinderAxisEnd; // for eigenfunctions of the cylinder
476 
477  realMappedGridFunction *fields;
478  realMappedGridFunction *dissipation,*e_dissipation;
479  realMappedGridFunction *errp;
480  MappedGrid *mgp;
481  MappedGridOperators *op;
482 
483  int numberLinesForPML; // width of the PML in grid lines
484  int pmlPower;
486  int pmlErrorOffset; // only check errors within this many lines of the pml
487 
488  RealArray *vpml; // for PML
489 
494  int numberOfVariableDissipationSmooths; // number of times to smooth the variable dissipation function
495 
496  int degreeSpace,degreeSpaceX,degreeSpaceY,degreeSpaceZ, degreeTime; // For TZ polynomial
497  real omega[4]; // For trig poynomial: fx,fy,fz,ft;
498 
499  real initialConditionParameters[10]; // holds parameters for various initial conditions
501 
502  aString probeFileName; // save the probes in this file
503  ArraySimple<real> probes; // output solution at these points
504  ArraySimple<int> probeGridLocation; // holds closest grid and grid point
505  int frequencyToSaveProbes; // save probe data every this many steps
506 
507  FILE *probeFile; // save probe data to this file
508 
509 
510  // for plane material interface
511  real normalPlaneMaterialInterface[3], x0PlaneMaterialInterface[3];
512 
513  // For radiation boundaryConditions:
514  int radbcGrid[2], radbcSide[2], radbcAxis[2];
516 
517  CompositeGrid *cgp;
518  CompositeGridOperators *cgop;
520  realCompositeGridFunction *cgfields;
521  realCompositeGridFunction *cgdissipation,*e_cgdissipation;
522  realCompositeGridFunction *cgerrp;
523  realCompositeGridFunction *variableDissipation;
524  realCompositeGridFunction *knownSolution; // for holding a known solution that may be expensive to recompute
525 
526  Interpolant *pinterpolant;
527 
528  realCompositeGridFunction *dsi_cgfieldsE0;
529  realCompositeGridFunction *dsi_cgfieldsE1;
530  realCompositeGridFunction *dsi_cgfieldsH;
531 
532  realCompositeGridFunction *pIntensity; // holds the time averaged intensity
533  realCompositeGridFunction *pHarmonicElectricField; // holds the time of Er and Ei
534 
535  realCompositeGridFunction &getCGField(Maxwell::FieldEnum f, int tn) ;
536 
537  ArraySimple<ArraySimple<int> > ulinksE,ulinksH;
538  ArraySimple<ArraySimple<UnstructuredMappingAdjacencyIterator> > ulinks;
539 
540  //kkc sparse matrix (CSR) arrays for dsi Mat-Vec implementation (should be an Overture sparse-rep?)
541  void setupDSICoefficients();
542  void reconstructDSIField( real t, FieldEnum field, realMappedGridFunction &from, realMappedGridFunction &to );
543  bool reconstructDSIAtEntities( real t, FieldEnum field, IntegerArray &entities, realMappedGridFunction &from, RealArray &to);
545 
546  enum DSIBCOption {
547  defaultDSIBC=0x0,
548  forceExtrap=0x1,
549  zeroBC=forceExtrap<<1,
550  curlHBC=zeroBC<<1,
551  curlcurlHBC=curlHBC<<1,
552  curlcurlcurlHBC=curlcurlHBC<<1,
553  forcedBC = curlHBC|curlcurlHBC|curlcurlcurlHBC
554  };
555 
556  void applyDSIBC( realMappedGridFunction &gf, real t, bool isEField, bool isProjection=true, int bcopt = defaultDSIBC );
557  void applyDSIForcing( realMappedGridFunction &gf, real t, real dt,bool isEField, bool isProjection=true );
558  int getCenters( MappedGrid &mg, UnstructuredMapping::EntityTypeEnum cent, realArray &xe );
559 
560 
561 
562  ArraySimple<real> Ecoeff, Hcoeff;
563  ArraySimple<int> Eindex, Eoffset, Hindex, Hoffset;
564  ArraySimple<int> Dindex, Doffset;
565  ArraySimple<real> Dcoeff, dispCoeff,E_dispCoeff; // Dcoeff are the non-zeros in the curl-curl operator, dispCoeff is the dissipation scaling
566  ArraySimple< ArraySimple<real> > REcoeff, RHcoeff;
567  ArraySimple< ArraySimple<int> > REindex, RHindex, SEindex, SHindex;
568  // ArraySimple<real> REcoeff, RHcoeff;
569  // ArraySimple<int> REindex, REoffset, RHindex, RHoffset;
570  // ArraySimple<real> REcoeff, RHcoeff;
571  // ArraySimple<int> REindex, REoffset, RHindex, RHoffset;
572 
573  ArraySimple<real> A,As;
574  ArraySimple<int> Aindex,Aoffset,Asindex,Asoffset;
575 
576  ArraySimple<int> CCindex, CCoffset;
577  ArraySimple<real> CCcoeff;
578 
579  realArray faceAreaNormals, edgeAreaNormals;
580 
581  // These next variables are for higher order time stepping
582  int numberOfTimeLevels; // keep this many time levels of u
583 
584  int myid; // processor number
585  int numberOfFunctions; // number of f=du/dt
587  realArray *fn;
588  realCompositeGridFunction *cgfn;
589 
590  GenericGraphicsInterface *gip;
591  GraphicsParameters psp;
592 
593  GUIState *runTimeDialog;
594  DialogData *pPlotOptionsDialog, *pParametersDialog;
595 
596  aString movieFileName;
598  int plotChoices, plotOptions;
599  int debug;
601  int totalNumberOfArrays; // for coutning A++ array
602 
603  aString nameOfGridFile;
604  FILE *logFile;
605  FILE *debugFile, *pDebugFile; // pDebugFile = debug file for each processor.
606  FILE *checkFile;
607 
609  OGFunction *tz;
610 
611  Ogshow *show; // Show file
612  ShowFileReader *referenceShowFileReader; // for comparing to a reference solution
613 
614  // for sequences saved in the show file
615  int sequenceCount,numberOfSequences;
616  RealArray timeSequence,sequence;
617 
618  bool useStreamMode,saveGridInShowFile;
619  int frequencyToSaveInShowFile, showFileFrameForGrid;
620  bool saveDivergenceInShowFile, saveErrorsInShowFile;
621 
622 
624  {
625  totalTime=0,
649  maximumNumberOfTimings // number of entries in this list
650  };
651  RealArray timing; // for timings, cpu time for some function
652  aString timingName[maximumNumberOfTimings]; // name of the function being timed
653 
655 };
656 
657 #endif