HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
sparse_grids_simulation.h
Go to the documentation of this file.
1 
6 #ifndef _SPARSE_GRIDS_SIM_H_
7 #define _SPARSE_GRIDS_SIM_H_
8 
9 #include <stdio.h>
10 #include <cstdlib>
11 #include <vector>
12 #include <string.h>
13 #include <string>
14 #include <utility>
15 #include <std_vec_ops.h>
16 #include <simulation.h>
17 
18 #define _SPARSEGRIDS_SIM_INP_FNAME_ "sparse_grids.inp"
19 
20 typedef std::vector<int> GridDimensions;
21 typedef std::vector<int> ProcDistribution;
22 typedef std::pair<double, GridDimensions> SGCTElem;
23 
24 #define _coeff_ first
25 #define _dim_ second
26 
39 {
40  public:
41 
44  {
45  m_is_defined = false;
46 
47  m_nsims_sg = -1;
48  m_ndims = -1;
49  m_rank = -1;
50  m_nproc = -1;
51 
52  m_sim_fg = NULL;
53  m_sims_sg.clear();
54 
55  m_n_fg = -1;
56  m_imin = -1;
57 
58  m_interp_order = 6;
59 
60  m_is_periodic.clear();
61  m_combination.clear();
62  }
63 
66  {
67  int err;
68  /* clean up full grid object */
70  delete m_sim_fg;
71  /* clean up and delete sparse grids objects */
72  err = Cleanup((void*) m_sims_sg.data(), m_nsims_sg);
73  if (err) {
74  printf( "Error: CleanUp() returned with status %d on process %d.\n",
75  err, m_rank );
76  }
77  m_sims_sg.clear();
78  /* done */
79  return;
80  }
81 
83  int define(int, int);
84 
86  inline int ReadInputs()
87  {
88  int retval = ::ReadInputs( (void*) m_sim_fg,
89  1,
90  m_rank );
91  return retval;
92  }
93 
95  int Initialize();
96 
98  int InitialSolution();
99 
101  inline int InitializeBoundaries()
102  {
103  int retval = ::InitializeBoundaries( (void*) m_sims_sg.data(),
104  m_nsims_sg );
105  if (m_nsims_sg > 0) {
106  m_is_periodic.resize(m_ndims);
107  for (int d=0; d<m_ndims; d++) {
108  m_is_periodic[d] = ( m_sims_sg[0].solver.isPeriodic[d] == 1 ? true : false);
109  m_sim_fg->solver.isPeriodic[d] = m_sims_sg[0].solver.isPeriodic[d];
110  }
111  }
112  return retval;
113  }
114 
117  {
118  int retval = ::InitializeImmersedBoundaries( (void*) m_sims_sg.data(),
119  m_nsims_sg );
120  return retval;
121  }
122 
124  inline int InitializePhysics()
125  {
126  int retval = ::InitializePhysics( (void*) m_sims_sg.data(),
127  m_nsims_sg );
128  return retval;
129  }
130 
133  {
134  for (int ns = 0; ns < m_sims_sg.size(); ns++) {
135  int retval = ::InitializePhysicsData( (void*) &(m_sims_sg[ns]),
136  -1,
137  m_nsims_sg,
139  if (retval) {
140  fprintf(stderr, "Error in SparseGridsSimulation::InitializePhysicsData()\n");
141  fprintf(stderr, " InitializePhysicsData returned with error code %d on rank %d.\n",
142  retval, m_sims_sg[ns].mpi.rank);
143  return retval;
144  }
145  }
146  return 0;
147  }
148 
150  inline int InitializeSolvers()
151  {
152  int retval = ::InitializeSolvers( (void*) m_sims_sg.data(),
153  m_nsims_sg );
155 
156  /* some modifications to output filename roots */
157  for (int i=0; i<m_nsims_sg; i++) {
158  strcpy(m_sims_sg[i].solver.op_fname_root, "op_sg");
159  strcpy(m_sims_sg[i].solver.aux_op_fname_root, "ts0_sg");
160  }
161  strcpy(m_sim_fg->solver.op_fname_root, "op_fg");
162  strcpy(m_sim_fg->solver.aux_op_fname_root, "ts0_fg");
163  return retval;
164  }
165 
167  int InitializationWrapup();
168 
170  int Solve();
171 
173  void WriteErrors(double, double);
174 
176  inline bool isDefined() const { return m_is_defined; }
177 
178 #ifndef serial
179 
180  inline int mpiCommDup()
181  {
182  if (!m_sim_fg) {
183  fprintf(stderr, "Error in SparseGridsSimulation::mpiCommDup()\n");
184  fprintf(stderr, " m_sim_fg is not allocated on rank %d!\n", m_rank);
185  return 1;
186  }
187  MPI_Comm_dup(MPI_COMM_WORLD, &(m_sim_fg->mpi.world));
188  return 0;
189  }
190 #endif
191 
192 #ifdef with_petsc
193 
194  inline void usePetscTS(PetscBool a_flag)
195  {
196  m_sim_fg->solver.use_petscTS = a_flag;
197  }
198 
200  inline int SolvePETSc()
201  {
202  int retval = ::SolvePETSc( (void*) m_sims_sg.data(),
203  m_nsims_sg,
204  m_rank,
205  m_nproc );
206  return retval;
207  }
208 #endif
209 
210  protected:
211 
215  int m_ndims;
216  int m_rank,
217  m_nproc;
221 
222  std::vector<bool> m_is_periodic;
226 
229 
231  std::vector<SimulationObject> m_sims_sg;
233  int m_n_fg;
234  int m_imin;
236  std::vector<SGCTElem> m_combination;
237  std::vector<ProcDistribution> m_iprocs;
240  int SanityChecks();
241 
245 
248  const GridDimensions&);
249 
252  void GetCTGridSizes(const int, std::vector<GridDimensions>&);
253 
256 
259 
262 
265  const GridDimensions&,
266  const ProcDistribution&,
267  const SimulationObject&,
268  const int,
269  const int );
270 
272  void OutputSolution(double);
273 
276 
278  void interpolate( SimulationObject* const,
279  const SimulationObject* const);
280 
282  void interpolate( const GridDimensions&,
283  double** const,
284  const SimulationObject* const);
285 
287  void interpolateGrid( SimulationObject* const,
288  const SimulationObject* const);
289 
291  void coarsenGrid1D( const GridDimensions&,
292  const GridDimensions&,
293  const double* const,
294  double* const,
295  int );
296 
298  void refineGrid1D( const GridDimensions&,
299  const GridDimensions&,
300  const double* const,
301  double* const,
302  int );
303 
305  void fillGhostCells( const GridDimensions&,
306  const int,
307  double* const,
308  const int );
309 
311  void CalculateError();
312 
314  void computeError( SimulationObject&, double* const);
315 
317  inline bool isPowerOfTwo(int x)
318  {
319  if (x == 0) return false;
320 
321  while (x > 1) {
322  if (x%2 != 0) return false;
323  x /= 2;
324  }
325  return true;
326  }
327 
329  inline double cfunc(int a, int b)
330  {
331  return ( ( (double)factorial(a) ) / ( (double) (factorial(b)*factorial(a-b)) ) );
332  }
333 
335  inline int factorial(int a)
336  {
337  int retval = 1.0;
338  for (int i=1; i<=a; i++) retval *= i;
339  return retval;
340  }
341 
343  inline void allocateGridArrays( const GridDimensions& a_dim,
344  double** const a_x,
345  const int a_ngpt = 0)
346  {
347  GridDimensions dim_wghosts = a_dim;
348  for (int i=0; i<dim_wghosts.size(); i++) {
349  dim_wghosts[i] += (2*a_ngpt);
350  }
351  long size_x = StdVecOps::sum(dim_wghosts);
352  (*a_x) = (double*) calloc (size_x, sizeof(double));
353  for (int i=0; i<size_x; i++) (*a_x)[i] = 0.0;
354  return;
355  }
356 
358  inline void allocateDataArrays( const GridDimensions& a_dim,
359  const int a_nvars,
360  double** const a_u,
361  const int a_ngpt = 0)
362  {
363  GridDimensions dim_wghosts = a_dim;
364  for (int i=0; i<dim_wghosts.size(); i++) {
365  dim_wghosts[i] += (2*a_ngpt);
366  }
367  long size_u = a_nvars*StdVecOps::product(dim_wghosts);
368  (*a_u) = (double*) calloc (size_u, sizeof(double));
369  for (int i=0; i<size_u; i++) (*a_u)[i] = 0.0;
370  return;
371  }
372 
373  private:
374 
375 };
376 
377 #endif
Base class for simulation and declarations for C functions.
void GetCTGridSizes(const int, std::vector< GridDimensions > &)
int InitializeBarebones(SimulationObject *)
void refineGrid1D(const GridDimensions &, const GridDimensions &, const double *const, double *const, int)
Structure defining a simulation.
Class describing sparse grids simulations.
std::vector< int > GridDimensions
long sum(const std::vector< int > &a_iv)
Definition: std_vec_ops.h:18
void CombinationTechnique(SimulationObject *const)
int InitializeSolversBarebones(SimulationObject *)
void interpolateGrid(SimulationObject *const, const SimulationObject *const)
void allocateDataArrays(const GridDimensions &a_dim, const int a_nvars, double **const a_u, const int a_ngpt=0)
int use_petscTS
Definition: hypar.h:395
char aux_op_fname_root[_MAX_STRING_SIZE_]
Definition: hypar.h:208
void WriteErrors(double, double)
void usePetscTS(PetscBool a_flag)
void fillGhostCells(const GridDimensions &, const int, double *const, const int)
std::vector< SGCTElem > m_combination
int ComputeProcessorDistribution(ProcDistribution &, const GridDimensions &)
MPI_Comm world
void allocateGridArrays(const GridDimensions &a_dim, double **const a_x, const int a_ngpt=0)
std::vector< SimulationObject > m_sims_sg
char op_fname_root[_MAX_STRING_SIZE_]
Definition: hypar.h:206
std::vector< bool > m_is_periodic
Contains some vector ops.
void computeError(SimulationObject &, double *const)
Base class for a simulation.
Definition: simulation.h:46
int Cleanup(void *, int)
Definition: Cleanup.c:39
std::vector< int > ProcDistribution
void interpolate(SimulationObject *const, const SimulationObject *const)
int SetSolverParameters(SimulationObject &, const GridDimensions &, const ProcDistribution &, const SimulationObject &, const int, const int)
std::pair< double, GridDimensions > SGCTElem
int CleanupBarebones(SimulationObject *)
double cfunc(int a, int b)
std::vector< ProcDistribution > m_iprocs
void coarsenGrid1D(const GridDimensions &, const GridDimensions &, const double *const, double *const, int)
long product(const std::vector< int > &a_iv)
Definition: std_vec_ops.h:27
int * dim_global
Definition: hypar.h:33
int * isPeriodic
Definition: hypar.h:162