Overture  Version 25
Oges.h
Go to the documentation of this file.
1 #ifndef OGES_H
2 #define OGES_H "Oges.h"
3 
4 #include "Overture.h"
5 
6 //===========================================================================
7 // Overlapping Grid Equation Solver
8 //
9 //===========================================================================
10 
11 #include "OGFunction.h"
12 #include "OgesParameters.h"
13 #include "MultigridCompositeGrid.h"
14 
15 #include "ListOfIntSerialArray.h"
16 #include "ListOfFloatSerialArray.h"
18 
19 #ifdef OV_USE_DOUBLE
21 #else
23 #endif
24 
26 class EquationSolver;
27 
28 class Oges
29 {
30  public:
31 
32 
34  {
39 
40 
41  Oges();
42  Oges( CompositeGrid & cg );
43  Oges( MappedGrid & mg );
44  Oges(const Oges & X);
45  Oges & operator=( const Oges & x );
46  virtual ~Oges();
47 
48  // define a predefined equation
52  const RealArray & bcData,
53  RealArray & constantCoeff=Overture::nullRealArray(),
54  realCompositeGridFunction *variableCoeff=NULL );
55 
56  // supply matrix coefficients and boundary conditions
58  const IntegerArray & boundaryConditions,
59  const RealArray & bcData );
60 
61 
62  void determineErrors(realCompositeGridFunction & u, // is this needed?
63  OGFunction & exactSolution,
64  int & printOptions );
65 
66  aString getErrorMessage( const int errorNumber ); // is this needed?
67 
68  int get( OgesParameters::OptionEnum option, int & value ) const;
69  int get( OgesParameters::OptionEnum option, real & value ) const;
70 
71  int getCompatibilityConstraint() const;
73  real getMaximumResidual() const;
74 
75  int initialize( );
76  bool isSolverIterative() const; // TRUE if the solver chosen is an iterative method
77  bool canSolveInPlace() const;
78 
79  // return the matrix in compressed row format
81  // compute y=A*x
82  int matrixVectorMultiply( int n, real *x, real *y );
83  // compute r=A*x-b
84  int computeResidual( int n, real *x, real *b, real *r );
85  // assign the values of the grid function u into the vector x
86  int assignVector( int n, real *x, realCompositeGridFunction & u );
87  // store the vector x into the grid function u
88  int storeVector( int n, real *x, realCompositeGridFunction & u );
89 
90  // output any relevant statistics
91  int printStatistics( FILE *file = stdout ) const;
92 
93  // ----------------Functions to set parameters -----------------------------
94  int set( OgesParameters::SolverEnum option );
95  int set( OgesParameters::SolverMethodEnum option );
96  int set( OgesParameters::MatrixOrderingEnum option );
97  int set( OgesParameters::PreconditionerEnum option );
98 
99  int set( OgesParameters::OptionEnum option, int value=0 );
100  int set( OgesParameters::OptionEnum option, float value );
101  int set( OgesParameters::OptionEnum option, double value );
102 
103  int setOgesParameters( const OgesParameters & opar );
104 
105  // assign values to rhs for the the extra equations
107 
108  // return solution values from the extra equations
109  int getExtraEquationValues( const realCompositeGridFunction & u, real *value );
110 
111  // evaluate the dot product of an extra equation times u
112  int evaluateExtraEquation( const realCompositeGridFunction & u, real & value, int extraEquation=0 );
113 
114  // evaluate the dot product of an extra equation times u and return the sum of the coefficients of the
115  // extra equation (i.e. the dot product with the "1" vector)
116  int evaluateExtraEquation( const realCompositeGridFunction & u, real & value,
117  real & sumOfExtraEquationCoefficients, int extraEquation=0 );
118 
119  virtual real sizeOf( FILE *file=NULL ) const ; // return number of bytes allocated by Oges, print info to a file
120 
121  // data base IO
122  virtual int get( const GenericDataBase & dir, const aString & name); // get from a database file
123  virtual int put( GenericDataBase & dir, const aString & name) const; // put to a database file
124 
125  int outputSparseMatrix( const aString & fileName );
126 
127  int writeMatrixToFile( aString fileName );
129  int writePetscMatrixToFile( aString filename,
132 
133  void reference(const Oges &);
134 
135  // Supply the coefficient matrix (and optionnally boundary conditions, needed buy multigrid for e.g.):
137  const IntegerArray & boundaryConditions=Overture::nullIntArray(),
138  const RealArray & bcData=Overture::nullRealArray() );
140  const IntegerArray & boundaryConditions=Overture::nullIntArray(),
141  const RealArray & bcData=Overture::nullRealArray() );
142 
143  // only solve the equations on some grids:
144  int setGridsToUse( const IntegerArray & gridsToUse );
145  bool activeGrid( int grid ) const; // return true if this grid is used
146  const IntegerArray & getUseThisGrid() const; // useThisGrid(grid)=true if the grid is active
147 
148  // supply command line arguments used by some solvers such as PETSc
149  int setCommandLineArguments( int argc, char **argv );
150 
151  void setGrid( CompositeGrid & cg, bool outOfDate=true );
152  void setGrid( MappedGrid & mg, bool outOfDate=true );
153 
154  // Set the MultigridCompositeGrid to use: (for use with Ogmg)
155  void set( MultigridCompositeGrid & mgcg );
156 
157  // Set the name of the composite grid (for info in files)
158  void setGridName( const aString & name );
159 
160  // Set the name of this instance of Ogmg (for info in debug files)
161  void setSolverName( const aString & name );
162 
165 
167  int updateToMatchGrid( MappedGrid & mg );
168 
169  int update( GenericGraphicsInterface & gi, CompositeGrid & cg ); // update parameters interactively
170 
171 
172 
173  public:
174  // Here are the data and functions used by the EquationSolver classes
175 
176  int ndia;
177  int ndja;
178  int nda;
179 
180  RealArray sol,rhs; // solution and rhs stored as a single long vector
181  IntegerArray ia,ja; // rows and columns stored in a sparse format
182  RealArray a; // matrix entries stored in a sparse format
183 
184 
185  int buildEquationSolvers(OgesParameters::SolverEnum solver); // call this to build a sparse solver.
186 
188  SparseStorageFormatEnum storageFormat,
189  bool allocateSpace = TRUE,
190  bool factorMatrixInPlace = FALSE );
191 
195 
196 
197 
198  public:
199 
201  OgesParameters parameters; // This object holds parameters for Oges
202  MultigridCompositeGrid mgcg; // for Ogmg, holds multigrid hierarchy
203 
204  realCompositeGridFunction coeff; // holds discrete coefficients
205 
206  intArray *classify; // holds classify arrays if we destroy the coeff grid function;
207 
208  realCompositeGridFunction uLinearized; // linearized u for nonlinear problems
209 
210  ListOfRealSerialArray ul,fl; // These are referenced to the user's u and f
211 
212  // extra equation info, such as a compatibility constraint
216  realCompositeGridFunction rightNullVector; // right null vector used as a compatibility constraint
217 
218  int solvingSparseSubset; // set to true if we are solving equations on a sparse subset of the grid (e.g. interface)
219 
220  int solverJob; // job
221  int initialized; // logical
222  bool shouldBeInitialized; // TRUE is initialize() should be called
223  int numberOfGrids; // local copy
225  int numberOfComponents; // same as in coeff[0].sparse->numberOfComponents
227 
228  int refactor;
229  int reorder;
232 
233  // int matrixHasChanged; // true if the matrix has changed.
234 
235  int numberOfEquations; // neq
236  int numberOfNonzerosBound; // nqs : bound on number of nonzeros
237  int numberOfNonzeros; // nze
238 
239 
242 
243  // Convert an Equation Number to a point on a grid (Inverse of equationNo)
244  void equationToIndex( const int eqnNo0, int & n, int & i1, int & i2, int & i3, int & grid );
245 
246 
247  int numberOfIterations; // number of iterations used by iterative solvers
248 
249  aString gridName; // name of the composite grid (used to name gridCheckFile)
250  aString solverName; // name of this instance of Ogmg
251 
252  int argc; // copy of command line arguments for PETSc
253  char **argv;
254 
256  {
258  };
259  // Here is where we keep the objects that interface to various solvers: yale, harwell, slap, petsc..
261 
262 // -----------Here are protected member functions-------------------
263  protected:
264 
266  RealArray constantCoefficients; // for second order constant coefficients predefined equation
267  IntegerArray boundaryConditions; // bc's for predefined equations
268  RealArray bcData; // data for bc's such as the coeff's of a mixed BC
269 
270 
271  IntegerArray gridEquationBase; // gridEquationBase(grid) = first eqn number on a grid.
272 
273  bool useAllGrids; // false if we only solve on a subset of grids.
274  IntegerArray useThisGrid; // only solve on this list of grids.
275 
276 
277  void setup();
278  void findExtraEquations();
279  void makeRightNullVector();
280  void generateMatrixError( const int nda, const int ieqn );
281  void generateMatrix( int & errorNumber );
282 
284 
285  // --------Utility functions:-------------
286 
287  inline int arraySize(const int grid, const int axis );
288  inline int arrayDims(const int grid, const int side, const int axis );
289 
290  // Return the equation number for given indices
291  inline int equationNo( const int n, const int i1, const int i2, const int i3, const int grid );
292 
293  IntegerDistributedArray equationNo(const int n, const Index & I1, const Index & I2, const Index & I3,
294  const int grid );
295 
296  public:
297  static int debug;
298 
299  public:
300 
301  int printObsoleteMessage(const aString & routineName, int option =0 );
302 
303 // ************************************************************************************************
304 // ----------all the remaining stuff in the class is obsolete-----------------------------------------
305 // ************************************************************************************************
306 
308 
309  enum coefficientTypes // coefficients can be supplied in continous or discrete form
310  {
313  };
314 
315  public:
316  // enumerators for available solvers
317 
318  enum solvers
319  {
320  yale=1,
322  bcg=3,
323  sor=4,
326  };
327 
329  {
332  GMRes=2,
334  };
335 
337  {
338  none=0,
342  };
343 
344 
345  void setConjugateGradientType( const conjugateGradientTypes conjugateGradientType );
347  const conjugateGradientPreconditioners conjugateGradientPreconditioner);
348  void setConjugateGradientNumberOfIterations( const int conjugateGradientNumberOfIterations);
350  const int conjugateGradientNumberOfSaveVectors );
351  void setConjugateGradientTolerance( const real conjugateGradientTolerance );
352  void setCompatibilityConstraint( const bool trueOrFalse );
353  void setEvaluateJacobian( const int EvaluateJacobian );
354  void setFillinRatio( const real fillinRatio );
355  void setFillinRatio2( const real fillinRatio2 );
356  void setFixupRightHandSide( const bool trueOrFalse );
357  void setHarwellTolerance( const real harwellTolerance); // tolerance for harwell pivoting
358  void setIterativeImprovement( const int trueOrFalse );
359 
360  void setNumberOfComponents( const int numberOfComponents ); // **** this is needed or get from SparseRep!
361 
362  void setNullVectorScaling(const real & scale );
363  void setMatrixCutoff( const real matrixCutoff );
364 
365  void setOrderOfAccuracy( const int order ); // **** this is needed or get from SparseRep!
366 
369  void setRefactor( const int refactor );
370  void setReorder( const int reorder );
371  void setSolverJob( const int solverJob );
372  void setSolverType( const solvers solverType );
373  void setSorNumberOfIterations( const int sorNumberOfIterations );
374  void setSorTolerance( const real sorTolerance );
375  void setSorOmega( const real sorOmega );
376 // void setSparseFormat( const int sparseFormat );
377  void setTranspose( const int transpose );
378  void setZeroRatio( const real zeroRatio );
379 
381 
382 
383 // solvers solverType; // solver
385 
390 
391 // --------------------------------------------------------------------------------
392 // --------------------------------------------------------------------------------
393 // --------------------------------------------------------------------------------
394 
395 
396 };
397 
398 
399 inline int Oges::
400 arraySize(const int grid, const int axis )
401 {
402  return cg[grid].dimension(End,axis)-cg[grid].dimension(Start,axis)+1;
403 }
404 
405 inline int Oges::
406 arrayDims(const int grid, const int side, const int axis )
407 {
408  return cg[grid].dimension(side,axis);
409 }
410 
411 
412 inline int Oges::
413 equationNo( const int n, const int i1, const int i2, const int i3,
414  const int grid )
415 //=============================================================================
416  // Return the equation number for given indices
417  // n : component number ( n=0,1,..,numberOfComponents-1 )
418  // i1,i2,i3 : grid indices
419  // grid : component grid number (grid=0,1,2..,numberOfCompoentGrids-1)
420  //=============================================================================
421 {
422  return n+1+ numberOfComponents*(i1-cg[grid].dimension(Start,axis1)+
423  (cg[grid].dimension(End,axis1)-cg[grid].dimension(Start,axis1)+1)*(i2-cg[grid].dimension(Start,axis2)+
424  (cg[grid].dimension(End,axis2)-cg[grid].dimension(Start,axis2)+1)*(i3-cg[grid].dimension(Start,axis3)
425  ))) + gridEquationBase(grid);
426 }
427 
428 
429 #endif // OGES_H
430