Overture  Version 25
Mapping.h
Go to the documentation of this file.
1 #ifndef MAPPING_H
2 #define MAPPING_H "Mapping.h"
3 
4 //-----------------------------------------------------------------
5 // Class Mapping : Class to define a mapping for overlapping grids
6 //
7 // Define a mapping from the parameter space (domain) into
8 // physical space (range)
9 //
10 //-----------------------------------------------------------------
11 
12 #include <assert.h>
13 #include "GenericDataBase.h"
14 #include "wdhdefs.h" // some useful defines and constants
15 #include "mathutil.h" // define max, min, etc
16 #include "Bound.h" // defines the Bound type
17 #include "MappingP.h" // defines MappingParameters
18 
19 #include "OvertureInit.h"
20 
21 #include "aString.H" // Livermore aString Library
22 #include "ReferenceCounting.h"
23 #include "ArraySimple.h"
24 #include "PlotIt.h"
25 
26 // --- forward declarations ---
27 class MappingRC;
28 class MappingInformation;
30 class BoundingBox;
33 class ExactGlobalInverse;
34 class DistributedInverse;
35 
36 class MappingItem // structure to store the pointers to the mappings in the LinkedList
37 {
38  public:
42  MappingItem( Mapping* value ){ val=value; next=NULL; }
44 };
45 
46 // Linked List of Mappings for makeMapping
48 {
49  public:
54  void add( Mapping *val );
55  int remove( Mapping *val );
56 };
57 
58 class Mapping : public ReferenceCounting
59 {
60 
61  public:
62 
64 
65  // Here are enumerators for the possible spaces for the domain and range
67  {
68  parameterSpace, // bounds are [0,1]
69  cartesianSpace // default (-infinity,infinity)
70  };
71 
72  // Here are enumerators for the coordinate systems that we can use (also in MappingP.h)
73  // Derivatives of mappings can be returned in these different systems
75  {
76  cartesian, // x,y,z
77  spherical, // phi/pi, theta/2pi, r
78  cylindrical, // theta/2pi, z, r
79  polar, // r, theta/tpi, z
80  toroidal, // theta1/tpi, theta2/tpi, theta3/tpi
81  numberOfCoordinateSystems // keeps a count of the number of enums in this list
82  };
83 
84  // Here are enumerators for the types of coordinate singularity a mapping may have
86  {
87  noCoordinateSingularity, // no coordinate singularity
88  polarSingularity // grid lines go to a point along the side
89  };
90 
91 
92  // Here are enumerators for the types of Mapping coordinate systems, these are
93  // used to optimize the computation of difference approximations to functions
94  // defined on grids derived from this mapping
96  {
97  rectangular, // rectangular mapping
98  conformal, // conformal : metric tensor is diagonal and ...
99  orthogonal, // orthogonal mapping : metric tensor is diagonal
100  general // general transformation : no special properties
101  };
102 
103  // Here are the enumerators for isPeriodic
105  {
107  derivativePeriodic, // Derivative is periodic but not the function
108  functionPeriodic // Function is periodic
109  };
110 
112  {
115  topologyIsPartiallyPeriodic // this is a "C" grid.
116  };
117 
118  // Here are enumerators for the items that we save character names for:
120  {
121  mappingName, // mapping name
122  domainName, // domain name
124  domainAxis1Name, // names for coordinate axes in domain
127  rangeAxis1Name, // names for coordinate axes in range
130  numberOfMappingItemNames // keeps a count of the number of enums in this list
131  };
132 
133  enum basicInverseOptions // options for basicInverse
134  {
138  canInvertWithGoodGuess // basicInverse requires a good guess
139  };
140 
141  Mapping( int domainDimension=3,
142  int rangeDimension=3,
147  );
148 
149  // Copy constructor is deep by default
150  Mapping( const Mapping &, const CopyType copyType=DEEP );
151 
152  // assignment with = is a deep copy
153  Mapping & operator =( const Mapping & X );
154 
155  virtual ~Mapping();
156 
157  void reference( const Mapping & map );
158  void breakReference();
159 
160  // This function is used to create a new member of the Class provided the
161  // mappingClassName is equal to the name of the class
162  virtual Mapping *make( const aString & mappingClassName );
163 
164  // Map the domain r to the range x
165  virtual void map( const realArray & r, realArray & x, realArray &xr = Overture::nullRealDistributedArray(),
167 
168  // Map the range x back to the domain r
169  virtual void inverseMap( const realArray & x, realArray & r, realArray & rx =Overture::nullRealDistributedArray(),
171 
172  // If you know the inverse of your mapping supply this next function,
173  // and set basicInverseOption=canInvert
174  // If you don't know the inverse but know how to determine if a point is not in the
175  // range (better than a bounding box) then set supply this function,
176  // and set basicInverseOption=canDetermineOutside
177  virtual void basicInverse(const realArray & x,
178  realArray & r,
181 
182  // Here are versions of map and inverseMap needed by some compilers (IBM:xlC) that don't like passing
183  // views of arrays to non-const references, as in mapping.mapC(r(I),x(I),xr(I))
184  virtual void mapC( const realArray & r, const realArray & x, const realArray &xr = Overture::nullRealDistributedArray(),
186  virtual void inverseMapC( const realArray & x, const realArray & r, const realArray & rx =Overture::nullRealDistributedArray(),
188 
189  // map a grid of points: r(0:n1,1), or r(0:n1,0:n2,2) or r((0:n1,0:n2,0:n3,3) for 1, 2 or 3d
190  virtual void mapGrid(const realArray & r,
191  realArray & x,
194 
195  // determine if one mapping (or face of) intersects another mapping (or face of)
196  virtual int intersects(Mapping & map2,
197  const int & side1=-1,
198  const int & axis1=-1,
199  const int & side2=-1,
200  const int & axis2=-1,
201  const real & tol=0. ) const;
202 
203  // inverse map a grid of points
204  virtual void inverseMapGrid(const realArray & x,
205  realArray & r,
208 
209  // ...................................................................................................
210  // Here are new versions (S) of map etc. that take serial arrays instead of distributed
211 
212  // Map the domain r to the range x
213  virtual void mapS( const RealArray & r, RealArray & x, RealArray &xr = Overture::nullRealArray(),
215 
216  // Map the range x back to the domain r
217  virtual void inverseMapS( const RealArray & x, RealArray & r, RealArray & rx =Overture::nullRealArray(),
219 
220  // If you know the inverse of your mapping supply this next function,
221  // and set basicInverseOption=canInvert
222  // If you don't know the inverse but know how to determine if a point is not in the
223  // range (better than a bounding box) then set supply this function,
224  // and set basicInverseOption=canDetermineOutside
225  virtual void basicInverseS(const RealArray & x,
226  RealArray & r,
229 
230  // Here are versions of map and inverseMap needed by some compilers (IBM:xlC) that don't like passing
231  // views of arrays to non-const references, as in mapping.mapC(r(I),x(I),xr(I))
232  virtual void mapCS( const RealArray & r, const RealArray & x, const RealArray &xr = Overture::nullRealArray(),
234  virtual void inverseMapCS( const RealArray & x, const RealArray & r, const RealArray & rx =Overture::nullRealArray(),
236 
237  // map a grid of points: r(0:n1,1), or r(0:n1,0:n2,2) or r((0:n1,0:n2,0:n3,3) for 1, 2 or 3d
238  virtual void mapGridS(const RealArray & r,
239  RealArray & x,
242 
243  // inverse map a grid of points
244  virtual void inverseMapGridS(const RealArray & x,
245  RealArray & r,
248 
249  // project points onto the Mapping, usually used when for curves or surfaces.
250  virtual int projectS( RealArray & x, MappingProjectionParameters & mpParams );
251 
252  static void openDebugFiles();
253  static void closeDebugFiles();
254 
255  // ...................................................................................................
256 
257 
258  // return size of this object
259  virtual real sizeOf(FILE *file = NULL ) const;
260 
261  virtual int update( MappingInformation & mapInfo ); // update mapping, change parameters interactively
262 
263  virtual bool updateWithCommand( MappingInformation &mapInfo, const aString &command ); // update using one command, possible interactively, return true if the command was understood
264 
265  virtual int interactiveUpdate( GenericGraphicsInterface & gi ); // calls above update
266 
267  // project points onto the Mapping, usually used when for curves or surfaces.
268  virtual int project( realArray & x, MappingProjectionParameters & mpParams );
269 
270  virtual void display( const aString & label=blankString) const;
271 
272  int checkMapping(); // Check the mapping - check derivatives and inverse, return 0 if ok
273 
274  void reinitialize(); // re-initialize a mapping that has changed (this will re-initialize the inverse)
275 
276  int determineResolution(int numberOfGridPoints[],
277  bool collapsedEdge[2][3],
278  real averageArclength[],
279  real elementDensityTolerance=.05 );
280 
281 
282  void secondOrderDerivative(const Index & I, // compute second derivatives by finite differences.
283  const realArray & r,
284  realArray & xrr,
285  const int axis,
286  const int & rAxis );
287 
288  #ifdef USE_PPP
289  void secondOrderDerivative(const Index & I, // compute second derivatives by finite differences.
290  const RealArray & r,
291  RealArray & xrr,
292  const int axis,
293  const int & rAxis );
294  #endif
295 
296  // find nearest grid points (in parallel too) that are closer than some given distance
297  int findNearestGridPoint( RealArray & x, RealArray & r, RealArray & dista, RealArray & xa );
298 
299  static real epsilon(); // here is the epsilon used by the Mappings and related classes.
300 
301  //----------------get functions-----------------------------
302  real getArcLength();
304  int getBoundaryCondition( const int side, const int axis ) const;
305  // return the bounding box from the approximate local inverse
306  virtual RealArray getBoundingBox( const int & side=-1, const int & axis=-1 ) const;
307  virtual int getBoundingBox( const IntegerArray & indexRange, const IntegerArray & gridIndexRange,
308  RealArray & xBounds, bool local=false ) const;
309  virtual int getBoundingBox( const RealArray & rBounds, RealArray & xBounds ) const;
310  const BoundingBox & getBoundingBoxTree( const int & side, const int & axis ) const;
311  virtual aString getClassName() const;
312  int getCoordinateEvaluationType( const coordinateSystem type ) const;
313  Bound getDomainBound( const int side, const int axis ) const;
314  int getDomainDimension() const;
316  Bound getDomainCoordinateSystemBound( const int side, const int axis ) const;
318  int getGridDimensions( const int axis ) const;
320  bool includeGhost=false);
322  bool includeGhost=false);
323  int getID() const;
324  int getInvertible() const;
325  periodicType getIsPeriodic( const int axis ) const;
327  aString getName( const mappingItemName item ) const;
328  int getGridIndexRange(int side, int axis );
329  int getNumberOfGhostPoints(int side, int axis );
330  real getParameter( const MappingParameters::realParameter & param ) const;
331  int getParameter( const MappingParameters::intParameter & param ) const;
332  real getPeriodVector( const int axis, const int direction ) const;
333  Bound getRangeBound( const int side, const int axis ) const;
335  Bound getRangeCoordinateSystemBound( const int side, const int axis ) const;
336  int getRangeDimension() const;
337  mappingSpace getRangeSpace() const;
338  int getShare( const int side, const int axis ) const;
339  real getSignForJacobian() const;
340  topologyEnum getTopology( const int side, const int axis ) const;
341  coordinateSingularity getTypeOfCoordinateSingularity( const int side, const int axis ) const;
342 
343  bool gridIsValid() const; // true if grid is up to date (remakeGrid==false)
344 
345  int hasACoordinateSingularity() const;
346 
347  intArray & topologyMask();
348 
349  //--------------set functions-------------------------------
350  void setArcLength(real length);
351  virtual void setBasicInverseOption( const basicInverseOptions option );
352  virtual void setBoundaryCondition( const int side, const int axis, const int bc );
353  virtual void setCoordinateEvaluationType( const coordinateSystem type, const int trueOrFalse );
354  virtual void setDomainBound( const int side, const int axis, const Bound domainBound );
356  virtual void setDomainCoordinateSystemBound( const int side, const int axis,
358  virtual void setDomainDimension( const int domainDimension );
359  virtual void setDomainSpace( const mappingSpace domainSpace );
360 
361  virtual void setGrid(realArray & grid, IntegerArray & gridIndexRange);
362 
363  virtual void setGridDimensions( const int axis, const int dim );
364  virtual void setInvertible( const int invertible );
365  void setID();
366  virtual void setIsPeriodic( const int axis, const periodicType isPeriodic );
368  virtual void setName( const mappingItemName item, const aString & name );
369  void setGridIndexRange(int side, int axis, int num );
370  void setNumberOfGhostPoints(int side, int axis, int numGhost );
371  virtual void setParameter( const MappingParameters::realParameter & param, const real & value );
372  virtual void setParameter( const MappingParameters::intParameter & param, const int & value );
373  void setPartition( Partitioning_Type & partition );
374  virtual void setPeriodVector( const int axis, const int direction,
375  const real periodVectorComponent );
376  virtual void setRangeBound( const int side, const int axis, const Bound rangeBound );
378  virtual void setRangeCoordinateSystemBound( const int side, const int axis,
380  virtual void setRangeDimension( const int rangeDimension );
381  virtual void setRangeSpace( const mappingSpace rangeSpace );
382  virtual void setShare( const int side, const int axis, const int share );
383  void setSignForJacobian( const real signForJac );
384  virtual void setTopology( const int side, const int axis, const topologyEnum topo );
385  virtual void setTypeOfCoordinateSingularity( const int side, const int axis, const coordinateSingularity );
386 
387  bool usesDistributedInverse() const; // return true if the inverse for this Mapping uses a distributed grid
388  bool usesDistributedMap() const; // return true if the map function is a distributed operation
389 
390  virtual void useRobustInverse(const bool trueOrFalse=TRUE );
391  bool usingRobustInverse() const;
392 
393  virtual int get( const GenericDataBase & dir, const aString & name); // get from a database file
394  virtual int put( GenericDataBase & dir, const aString & name) const; // put to a database file
395 
396  virtual int outside( const realArray & x ); // is x outside the grid? (false means NO or
397  // don't know)
398 
399  virtual int setNumberOfGhostLines( IndexRangeType & numberOfGhostLinesNew ); // set the number of ghost lines.
400 
401  // Specify the number of parallel ghost lines to use for this mapping (on the "grid" array)
402  void setNumberOfDistributedGhostLines( int numGhost );
403 
404  // On Parallel machines always add at least this many parallel ghost lines (on the "grid" array)
405  static void setMinimumNumberOfDistributedGhostLines( int numGhost );
406 
407  // utility routine to compute the max and min values of a grid of points
408  static int getGridMinAndMax(const realArray & u, const Range & R1, const Range & R2, const Range & R3,
409  real uMin[3], real uMax[3], bool local=false );
410  #ifdef USE_PPP
411  static int getGridMinAndMax(const RealArray & u, const Range & R1, const Range & R2, const Range & R3,
412  real uMin[3], real uMax[3], bool local=false );
413  #endif
414 
415  void periodicShift( realArray & r, const Index & I ); // useful utility routine
416  #ifdef USE_PPP
417  void periodicShift( RealArray & r, const Index & I ); // useful utility routine
418  #endif
419 
420  Index getIndex(const realArray & r, realArray & x, const realArray &xr,
421  int & base, int & bound, int & computeMap, int & computeMapDerivative );
422 
423  #ifdef USE_PPP
424  Index getIndex(const RealArray & r, RealArray & x, const RealArray &xr,
425  int & base, int & bound, int & computeMap, int & computeMapDerivative );
426  #endif
427 
428  int computeMap,computeMapDerivative,base,bound; // used by routines calling getIndex
429 
430 // #ifdef USE_PPP
431 // // Map the domain r to the range x
432 // virtual void map( const RealArray & r, RealArray & x, RealArray & xr = Overture::nullRealArray(),
433 // MappingParameters & params =Overture::nullMappingParameters());
434 
435 // #endif
436 
437 /* ----
438  // here are versions that take distributed arrays, they have a different name so that
439  // derived classes don't have to define them when they redefine the map, inverseMap, ... etc.
440  virtual void mapD( const realArray & r, realArray & x, realArray &xr = Overture::nullRealDistributedArray(),
441  MappingParameters & params =Overture::nullMappingParameters());
442  virtual void inverseMapD( const realArray & x, realArray & r, realArray & rx =Overture::nullRealDistributedArray(),
443  MappingParameters & params =Overture::nullMappingParameters());
444  virtual void basicInverseD(const realArray & x,
445  realArray & r,
446  realArray & rx =Overture::nullRealDistributedArray(),
447  MappingParameters & params =Overture::nullMappingParameters());
448  virtual void mapCD( const realArray & r, const realArray & x, const realArray &xr = Overture::nullRealDistributedArray(),
449  MappingParameters & params =Overture::nullMappingParameters());
450  virtual void inverseMapCD( const realArray & x, const realArray & r, const realArray & rx =Overture::nullRealDistributedArray(),
451  MappingParameters & params =Overture::nullMappingParameters());
452  virtual void mapGridD(const realArray & r,
453  realArray & x,
454  realArray & xr =Overture::nullRealDistributedArray(),
455  MappingParameters & params=Overture::nullMappingParameters() );
456 
457  virtual void inverseMapGridD(const realArray & x,
458  realArray & r,
459  realArray & rx =Overture::nullRealDistributedArray(),
460  MappingParameters & params=Overture::nullMappingParameters() );
461 
462  virtual int projectD( realArray & x, MappingProjectionParameters & mpParams );
463 ----- */
464 
465  protected:
466 
467  int dataBaseID; // unique identifier for this Mapping when saved in a database file.
468 
469  aString className; // Name of the Class, make private for gnu compiler
470  int domainDimension; // Dimension of the parameter space (r)
471  int rangeDimension; // Dimension of map in physical space (x)
472 
473  int bc[2][3]; // Boundary condition type
474  int share[2][3]; // code to indicate if boundaries are shared
475  periodicType isPeriodic[3]; // Is the mapping periodic 0=no, 1=derivative, 2=function
476  real periodVector[3][3]; // Period vector for isPeriodic=1, periodVector(.,axis) is
477  // the vector in the axis=axis1 or axis=axis2.. direction
478 
479  int invertible; // Is the mapping invertible;
480  basicInverseOptions basicInverseOption; // what can the basicInverse function do?
481 
482  int periodicityOfSpace; // =0,1,2,3 : number of directions in which space is periodic
484  // which coordinate systems are implemented for the mapping:
486 
488  int getMappingParametersOption( const aString & answer,
489  DialogData & dialog,
492 
493 protected:
494 
495  IndexRangeType gridIndexRange, numberOfGhostPoints; // for dimensioning grid and gridSerial
496 
497 protected:
498  realArray grid; // If this exists it can be used for plotting the mapping or the inverse
499  bool inverseIsDistributed; // if true, use a distributed grid for the inverse
500  bool mapIsDistributed; // if true, map() is a distributed operation.
501 
502  // These next are new -- allow the grid to have ghost points
504  // On Parallel machines always add at least this many ghost lines on the distributed grid
506  int numberOfDistributedGhostLines; // number of parallel ghost lines to use for this grid
507 
508 
509  bool partitionInitialized; // true when the partition has been specified.
510  Partitioning_Type partition; // partition for grid
511  #ifdef USE_PPP
512  realSerialArray gridSerial; // this grid is used for the Inverse in parallel
513  #endif
514 
515  void initializePartition(); // initialize the parallel partition for grid
516  int mappingHasChanged(); // call this function if the mapping has changed
517  void setGridIsValid(); // set remakeGrid=false
518 
519  mappingSpace domainSpace; // space of the domain
520  mappingSpace rangeSpace; // space of the range
521  coordinateSystem domainCoordinateSystem ; // domain coordinate system
522  coordinateSystem rangeCoordinateSystem ; // range coordinate system
523  mappingCoordinateSystem mappingCoordinateSystem0; // for optimizing derivatives
524 
525  Bound domainBound[3][2]; // bounds on the domain
526  Bound rangeBound[3][2]; // bounds on the range
529 
530  void setDefaultMappingBounds( const mappingSpace ms, Bound mappingBound[3][2] );
531 
532  void setDefaultCoordinateSystemBounds( const coordinateSystem cs, Bound csBound[3][2] );
533 
536 
537 public:
538  real signForJacobian; // make public for now (needed for P++)
539 
540 protected:
541  real arcLength; // holds the arcLength for curves, a negative value means it has not been computed yet.
542 
543  bool remakeGrid; // set by mappingHasChanged
544  bool remakeGridSerial; // remake the serial version of the grid
545 
546  private:
547 
548  aString namestr[numberOfMappingItemNames]; // here is where we save the names of items
549 
550 protected:
551  int validSide( const int side ) const;
552  int validAxis( const int axis ) const;
553  void mappingError( const aString & subName, const int side, const int axis ) const;
554 
555  // protected: ** should be protected, make public for "space" test program
556  public:
558 
561  mutable DistributedInverse *distributedInverse; // for inverses in parallel
562 
563  //----------------makeMapping-------------------------------------------------------------
564  public:
565 
566  static Mapping* makeMapping( const aString & mappingClassName );
567 
568 
569  static MappingLinkedList & staticMapList();
570 
571  public:
572  static int debug; // variable used for debugging
573  static FILE *debugFile, *pDebugFile;
574  static const real bogus; // Bogus value to indicate no convergence
575 
576  friend class MappingRC;
578 
579  private:
580 
581  //
582  // Virtual member functions used only through class ReferenceCounting:
583  //
585  { return operator=((Mapping &)x); }
586  virtual void reference( const ReferenceCounting& x)
587  { reference( (Mapping &) x); }
588  virtual ReferenceCounting* virtualConstructor( const CopyType ct = DEEP ) const
589  { return ::new Mapping(*this, ct); }
590 };
591 
592 // extern Mapping::LinkedList Mapping::staticMapList(); // list of Mappings for makeMapping ** remove this **
593 
594 
595 #endif // MAPPING_H
596