Overture  Version 25
Ogmg.h
Go to the documentation of this file.
1 #ifndef OGMG_H
2 #define OGMG_H "Ogmg.h"
3 
4 #include "Overture.h"
5 #include "Oges.h"
7 #include "display.h"
8 #include "Ogmg.h"
9 #include "OgmgParameters.h"
11 #include "GraphicsParameters.h"
12 #include "Interpolate.h"
13 #include "MultigridCompositeGrid.h"
14 
15 
16 Index IndexBB(int base, int bound, int stride=1 );
17 Index IndexBB(Index I, const int stride );
18 
19 class TridiagonalSolver; // forward declaration
20 class OGFunction;
21 class InterpolationData;
22 
23 //-------------------------------------------------------------------------------------------------------
24 // Overlapping Grid Multigrid Solver
25 //-------------------------------------------------------------------------------------------------------
26 class Ogmg
27 {
28  public:
29 
30 
31 // enum BoundaryConditionEnum // these are the numbers defining values in the bc array
32 // {
33 // dirichlet=1,
34 // neumann=2,
35 // mixed=3,
36 // equation,
37 // extrapolation,
38 // combination
39 // };
40 
42  {
46  };
47 
49  {
51  };
52 
53 
55  {
68  };
69 
70  enum
71  {
72  allGrids=-99,
73  allLevels=-100
74  };
75 
76 
77  Ogmg();
79  ~Ogmg();
80 
81 
82  // set and get parameters for Ogmg using the next object
84 
85  void displaySmoothers(const aString & label, FILE *file=stdout);
86 
87  CompositeGrid & getCompositeGrid(){ return multigridCompositeGrid(); } // return Ogmg's grid containing the multigrid levels
88 
89  FILE *getInfoFile(){return infoFile;} // Ogmg's info file
90  FILE *getCheckFile(){return checkFile;} // Ogmg's check file
91 
93  void computeDefect(int level){ defect(level); } // determine defect on a level (for plotting etc)
94 
95  realCompositeGridFunction & getRhs() { return fMG;} // RHS GF
96 
97  real getMaximumResidual() const;
98 
99  // Return the "mean" value of a grid function. Use a particular
100  // definition of mean.
102 
103  int getNumberOfIterations() const;
104 
105  // return the order of extrapolation to use for a given order of accuracy and level
106  int getOrderOfExtrapolation( const int level ) const;
107 
108  void set( GenericGraphicsInterface *ps );
109 
110  // Set the MultigridCompositeGrid to use:
111  void set( MultigridCompositeGrid & mgcg );
112 
113  // Set the name of the composite grid (for info in files)
114  void setGridName( const aString & name );
115 
116  // Set the name of this instance of Ogmg (for info in debug files)
117  void setSolverName( const aString & name );
118 
119  // set parameters equal to another parameter object.
121 
122  int setOption(OptionEnum option, bool trueOrFalse );
123  // int set( OptionEnum option, int value, real rvalue );
124  // int get( OptionEnum option, int & value, real & rvalue ) const;
125 
126  int setOrderOfAccuracy(const int & orderOfAccuracy);
127 
128  // Supply the coefficients (for all multigrid levels), optionally supply BC's
130  const IntegerArray & boundaryConditions=Overture::nullIntArray(),
131  const RealArray & bcData=Overture::nullRealArray() );
132 
133 
134  // Set the boundary conditions for corners and edges:
136 
137  // Choose a predefined equation to solve.
139  const IntegerArray & boundaryConditions,
140  const RealArray & bcData,
142  realCompositeGridFunction *variableCoeff=NULL );
143 
144  virtual real sizeOf( FILE *file=NULL ) const ; // return number of bytes allocated by Oges, print info to a file
145 
146  int chooseBestSmoother();
147 
148  int update( GenericGraphicsInterface & gi ); // update parameters interactively
149  int update( GenericGraphicsInterface & gi, CompositeGrid & cg ); // update parameters interactively
150  void updateToMatchGrid( CompositeGrid & mg );
151 
152  // solve
154 
155  void printStatistics(FILE *file=stdout) const;
156 
157  int smoothTest(GenericGraphicsInterface & ps, int plotOption);
158 
159  int coarseToFineTest();
160 
161  int fineToCoarseTest();
162 
163  int bcTest();
164 
165  // test the accuracy of the coarse grid equations
166  int coarseGridSolverTest( int plotOption=0 );
167 
168  // int get( const GenericDataBase & dir, const aString & name);
169  // int put( GenericDataBase & dir, const aString & name) const;
170 
171  static int debug;
172  static OGFunction *pExactSolution; // for testing
173  static aString infoFileCaption[5]; // for captions on the convergence table in the info file.
174 
175 // protected:
176  public: // do this for now
177 
178  enum Timing
179  {
205  numberOfThingsToTime // counts the number of elements in this list
206  };
207 
209  {
210  canInterpolateQuality1=0, // best quality, interpolates to correct order
211  canInterpolateQuality2, // 2nd best best quality, interpolates to correct order minus 1
212  canInterpolateQuality3, // 3rd best quality, interpolates to correct order minus 2
213  canInterpolateWithExtrapolation, // bad quality -- should replace by a better quality result
214  canInterpolateQualityBad, // bad quality -- should replace by a better quality result
215  canInterpolateQualityVeryBad, // worst quality-- should replace by a better quality result
216  canNotInterpolate // unable to interpolate at all
217  };
218 
219 
221  {
223  sparse=1,
228  };
229 
230  Oges directSolver; // direct solver for coarsest level
231  Oges *ogesSmoother; // Oges smoother.
232 
233  MultigridCompositeGrid multigridCompositeGrid; // this object keeps track of the multigrid hierachy and can be shared amongst different solvers
234 
235  aString gridName; // name of the composite grid (used to name gridCheckFile)
236  aString solverName; // name of this instance of Ogmg
237 
238  realCompositeGridFunction uMG,fMG; // local reference copies
240  realCompositeGridFunction cMG; // coefficients stored here
241  realCompositeGridFunction rightNullVector; // right null vector used for singular problems.
242 
243 
246 
247  realCompositeGridFunction *v; // for singular problems.
248 
250 
251  bool useForcingAsBoundaryConditionOnAllLevels; // set to true for testing coarse to fine
252 
253  RealArray alpha; // compatibility value for singular problems.
254  RealArray workUnits; // number of work units used per level (one cycle only)
256  RealArray equationCoefficients; // for predefined equations
257  realCompositeGridFunction *varCoeff; // for predefined equations.
258 
260  {
265  };
266  RealArray cycleResults; // holds some results for outputing for matlab and documentation etc.
267 
269  IntegerArray lineSmoothIsInitialized; // (axis,grid,level)
270 
271  // The boundary condition type:
272  // equation: means the interior equation is applied on the boundary and there is some
273  // equation on the ghost point (which may be explicitly set if bcSupplied==true, or
274  // which may only be defined by the values in the coefficient matrix.
275  // extrapolation : means there is a dirichlet like condition applied on the boundary
276  IntegerArray boundaryCondition; // values are one of equation, extrapolation, or combination
277 
278  // In some cases the boundary conditions are explicitly set (e.g. predefined or user defined)
279  bool bcSupplied; // true if the booundary conditions are explicitly set
280  bool bcDataSupplied; // true if extra bc data has been provided (e.g. mixed BC coeff's)
281  IntegerArray bc; // values are dirichlet, neumann, mixed or extrapolation
283 
284  int subSmoothReferenceGrid; // reference grid (uses 1 sub-smooth) for variable sub-smooths
285  RealArray defectRatio; // holds ratios of defects for auto-subSmooth
286 
287  IntegerArray isConstantCoefficients; // isConstantCoefficients(grid)
288 
289  IntegerArray active; // active(grid) = false if we do not need to solve on a grid.
290 
292 
293  TridiagonalSolver ****tridiagonalSolver; // [level][grid][axis]
294 
295  CompositeGridOperators *operatorsForExtraLevels; // array of operators for extra levels.
297 
300 
301  int myid; // processor number
302 
303  // numberOfInstances counts the number of instances of Ogmg (so we can have different names for debug files)
304  static int numberOfInstances;
305  FILE *debugFile; // debug file
306  FILE *pDebugFile; // pDebugFile = debug file for each processor.
307  FILE *infoFile;
308  FILE *checkFile;
309  FILE *gridCheckFile; // check file for the multigrid levels constructed by Ogmg (to compare serial/paralle results)
310 
311  bool initialized; // TRUE if initialized with a grid.
313 
315 
318  int numberOfSolves; // total number of solves
319  int totalNumberOfCycles; // total number of cycles over all solves
320  int numberOfCycles; // number of cycles in last solve
321  int numberOfIterations; // number for the last call to solve
323  int levelZero; // base level (for use with full multigrid)
324  int *iterationCount; // counts of iterations per level, for use with an F-cycle
325 
330  real tm[numberOfThingsToTime]; // for timings
331  int timerGranularity; // granularity of the timer (to avoid overhead in calling getCPU)
332  int totalNumberOfCoarseGridIterations; // counts iterations used to solve coarse grid equations
333 
335 
336  Interpolate interp; // AMR interpolation for fine-to-coarse and coarse-to-fine
337 
338  char buff[100];
339 
340  int *nipn, *ndipn, **ipn, numberOfIBSArrays; // for IBS -- could be shared amongst solvers with the same CompositeGrid.
341  static real bogusRealArgument1,bogusRealArgument2; // for a default args
342 
344  int applyFinalConditions();
345 
346  void assignBoundaryConditionCoefficients( realMappedGridFunction & coeff, int grid, int level,
347  int sideToCheck=-1, int axisToCheck=-1 );
348  void checkParameters();
349 
350  void init();
351 
352  void setup(CompositeGrid & mg );
353  void setMean(realCompositeGridFunction & u, const real meanValue, int level);
355  real l2Norm(const realMappedGridFunction & e );
357  real maxNorm(const realMappedGridFunction & e );
358 
360 
363 
364  int createNullVector();
365  int saveLeftNullVector();
366  int readLeftNullVector();
367  real rightNullVectorDotU( const int & level, const RealCompositeGridFunction & u );
368 
369  // build coarse grid levels:
371  // .. parallel version:
373 
374  // build the predefined equations
376  int buildPredefinedCoefficientMatrix( int level, bool buildRectangular, bool buildCurvilinear );
377 
378  int buildPredefinedVariableCoefficients( RealCompositeGridFunction & coeff, const int level );
379 
380  int cycle(const int & level, const int & iteration, real & maximumDefect, const int & numberOfCycleIterations ); // cycle at level l
381 
383  getGhostLineBoundaryCondition( int bc, int ghostLine, int grid, int level,
384  int & orderOfExtrapolation, aString *bcName=NULL ) const;
385 
386  int setBoundaryConditions(const IntegerArray & boundaryConditions,
387  const RealArray & bcData=Overture::nullRealArray() );
388 
389  void smooth(const int & level, int numberOfSmoothingSteps, int cycleNumber );
390  void smoothJacobi(const int & level, const int & grid, int smootherChoice = 0);
391  void smoothGaussSeidel(const int & level, const int & grid);
392  void smoothRedBlack(const int & level, const int & grid);
393  void smoothLine(const int & level, const int & grid, const int & direction, bool useZebra=true,
394  const int smoothBoundarySide = -1 );
395  void alternatingLineSmooth(const int & level, const int & grid, bool useZebra=true);
396 
397  void applyOgesSmoother(const int level, const int grid);
398 
399  void smoothBoundary(int level,
400  int grid,
401  int bcOption[6],
402  int numberOfLayers=1,
403  int numberOfIterations=1 );
404 
405  void smoothInterpolationNeighbours(int level, int grid );
406 
407  void computeDefectRatios( int level );
408 
409  bool useEquationOnGhostLineForDirichletBC(MappedGrid & mg, int level);
410  bool useEquationOnGhostLineForNeumannBC(MappedGrid & mg, int level);
411 
412  void defect(const int & level);
413  void defect(const int & level, const int & grid);
414 
415  void fineToCoarse(const int & level, bool transferForcing = false);
416  void fineToCoarse(const int & level, const int & grid, bool transferForcing = false );
417 
418  void coarseToFine(const int & level);
419  void coarseToFine(const int & level, const int & grid);
420 
421  // these can be used to compute an approximate norm (fast)
422  real defectMaximumNorm(const int & level, int approximationStride=1 ); // max-norm
423  real defectNorm(const int & level, const int & grid, int option=0, int approximationStride=8 );
424 
425  real getDefect(const int & level,
426  const int & grid,
427  realArray & f, // could be const, except reshape needed
428  realArray & u, // could be const, except reshape needed
429  const Index & I1,
430  const Index & I2,
431  const Index & I3,
432  realArray & defect,
433  const int lineSmoothOption = -1,
434  const int defectOption = 0,
435  real & defectL2Norm=bogusRealArgument1, real & defectMaxNorm=bogusRealArgument2 );
436 
437  void evaluateTheDefectFormula(const int & level,
438  const int & grid,
439  const realArray & c,
440  const realArray & u,
441  const realArray & f,
442  realArray & defect,
443  MappedGrid & mg,
444  const Index & I1,
445  const Index & I2,
446  const Index & I3,
447  const Index & I1u,
448  const Index & I2u,
449  const Index & I3u,
450  const int lineSmoothOption);
451 
452  int fullMultigrid();
453 
454  int interpolate(realCompositeGridFunction & u, const int & grid =-1, int level=-1 );
455 
456  // here is the new way:
457 // int assignBoundaryConditions(const int & level,
458 // const int & grid,
459 // RealMappedGridFunction & u,
460 // RealMappedGridFunction & f );
461 
462  int applyBoundaryConditions(const int & level,
463  const int & grid,
467 
468  // form coarse grid operator by averaging the fine grid operator
469  int operatorAveraging(RealCompositeGridFunction & coeff, const int & level);
471  RealMappedGridFunction & coeffCoarse,
472  const IntegerArray & coarseningRatio,
473  int grid =0,
474  int level =0 );
475 
476  int averageCoefficients(Index & I1, Index & I2, Index & I3,
477  Index & I1p, Index & I2p, Index & I3p,
478  Index & J1, Index & J2, Index & J3,
479  TransferTypesEnum option[3],
480  const realSerialArray & cFine,
481  realSerialArray & cCoarse, int ipar[] );
482 
483  int markGhostPoints( CompositeGrid & cg );
484 
485 
486  int getInterpolationCoordinates( CompositeGrid & cg0, // finer grid
487  CompositeGrid & cg1, // new coarser grid
488  const IntegerArray & ib, // check these points...
489  const int grid, // ..on this grid
490  const IntegerArray & gridsToCheck, // ..from these grids
491  realSerialArray & rb, // return these values
492  const bool isRectangular,
493  int iv0[3], real dx0[3], real xab0[2][3], // these are used by Macros!
494  int iv1[3], real dx1[3], real xab1[2][3] );
495 
496 
497 // parallel version:
498  int getInterpolationCoordinates( CompositeGrid & cg0, // finer grid
499  CompositeGrid & cg1, // new coarser grid
500  const IntegerArray & ib, // check these points...
501  const int grid, // ..on this grid
502  const IntegerArray & gridsToCheck, // ..from these grids
503  realSerialArray & rb, // return these values
504  const bool isRectangular,
505  int iv0[3], real dx0[3], real xab0[2][3], // these are used by Macros!
506  int iv1[3], real dx1[3], real xab1[2][3],
507  InterpolationData & ipd );
508 
509 
510  int getInterpolationCoordinatesNew( CompositeGrid & cg0, // finer grid
511  CompositeGrid & cg1, // new coarser grid
512  const IntegerArray & ib, // check these points...
513  const RealArray & xa, // (x-coord's of the interp. pts)
514  const int grid, // ..on this grid
515  const IntegerArray & gridsToCheck, // ..from these grids
516  realSerialArray & rb, // return these values
517  const bool isRectangular,
518  int iv0[3], real dx0[3], real xab0[2][3], // these are used by Macros!
519  int iv1[3], real dx1[3], real xab1[2][3],
520  InterpolationData & ipd,
521  IntegerArray & ia0, // list of all interp points
522  realSerialArray & donorDist // distance to donor
523  );
524 
525  // old "new" parallel Version
526  int getInterpolationCoordinatesNewOld( CompositeGrid & cg0, // finer grid
527  CompositeGrid & cg1, // new coarser grid
528  const IntegerArray & ib, // check these points...
529  const RealArray & xa,
530  const int grid, // ..on this grid
531  const IntegerArray & gridsToCheck, // ..from these grids
532  realSerialArray & rb, // return these values
533  const bool isRectangular,
534  int iv0[3], real dx0[3], real xab0[2][3], // these are used by Macros!
535  int iv1[3], real dx1[3], real xab1[2][3],
536  InterpolationData & ipd );
537 
538 
539  // old "scalar" version
540  int getInterpolationCoordinates(CompositeGrid & cg0, // finer grid
541  CompositeGrid & cg1, // new coarser grid
542  int i, // check this point
543  int grid,
544  int iv[],
545  int jv[],
546  realSerialArray & r,
547  bool isRectangular,
548  int iv0[3], real dx0[3], real xab0[2][3], int iv1[3], real dx[3], real xab[2][3] );
549 
551  CompositeGrid & cg1,
552  int i,
553  int iv[3],
554  int grid,
555  int l,
556  intSerialArray & inverseGrid,
557  intSerialArray & interpoleeGrid,
558  intSerialArray & interpoleeLocation,
559  intSerialArray & interpolationPoint,
560  intSerialArray & variableInterpolationWidth,
561  realSerialArray & interpolationCoordinates,
562  realSerialArray & inverseCoordinates );
563 
564  int checkForBetterQualityInterpolation( realSerialArray & x, int gridI, InterpolationQualityEnum & interpolationQuality,
565  CompositeGrid & cg0,
566  CompositeGrid & cg1,
567  int i,
568  int iv[3],
569  int grid,
570  int l,
571  intSerialArray & inverseGrid,
572  intSerialArray & interpoleeGrid,
573  intSerialArray & interpoleeLocation,
574  intSerialArray & interpolationPoint,
575  intSerialArray & variableInterpolationWidth,
576  realSerialArray & interpolationCoordinates,
577  realSerialArray & inverseCoordinates );
578 
579  // output results to a matlab file:
580  int outputCycleInfo();
581 
582  // output some results at every cycle
583  int outputResults(const int & level, const int & iteration, real & maximumDefect, real & defectNew, real & defectOld);
585 
586  int addAdjustmentForSingularProblem(int level, int iteration );
587  int removeAdjustmentForSingularProblem(int level, int iteration );
588  int getSingularParameter(int level);
589  int computeLeftNullVector();
590 
591 };
592 
593 
594 #endif