00001 #ifdef CH_LANG_CC 00002 /* 00003 * _______ __ 00004 * / ___/ / ___ __ _ / / ___ 00005 * / /__/ _ \/ _ \/ V \/ _ \/ _ \ 00006 * \___/_//_/\___/_/_/_/_.__/\___/ 00007 * Please refer to Copyright.txt, in Chombo's root directory. 00008 */ 00009 #endif 00010 00011 #ifndef _AMR_H_ 00012 #define _AMR_H_ 00013 00014 #include <iostream> 00015 #include <string> 00016 #include <ctime> 00017 00018 #include "REAL.H" 00019 #include "Vector.H" 00020 #include "AMRLevel.H" 00021 #include "AMRLevelFactory.H" 00022 #include "BRMeshRefine.H" 00023 #include "RefCountedPtr.H" 00024 #include "ProblemDomain.H" 00025 #include "Box.H" 00026 #include "CH_HDF5.H" 00027 #include "Scheduler.H" 00028 #include "NamespaceHeader.H" 00029 00030 /// Framework for Berger-Oliger timestepping for AMR 00031 /** 00032 This class is a framework for Berger-Oliger timestepping for 00033 adaptive mesh refinement of time-dependent problems. It is 00034 applicable to both hyperbolic and parabolic problems. It 00035 represents a hierarchy of levels of refinement as a collection of 00036 AMRLevel objects. 00037 00038 The usage pattern is this: 00039 00040 <ol> 00041 <li>Call define() to define the stuff that does not change throughout 00042 the run (maxlevel, refinement ratios, domain, and operator). 00043 <li>Modify any parameters you like (blocking factor, max grid size, etc.) 00044 using parameter-setting functions. 00045 <li>Call any one of the three setup functions (setupForRestart(), 00046 setupForNewAMRRun(), or setupForFixedHierarchyRun()) 00047 so AMR can set up all its internal data structures. 00048 <li>Call run() to run the calculation. 00049 <li>Call conclude() to output (to stdout) how many cells were updated 00050 and so forth. 00051 </ol> 00052 */ 00053 class AMR 00054 { 00055 00056 public: 00057 //hook to do output of stuff by timestep in objects that do not know AMR 00058 static int s_step; 00059 /// 00060 /** 00061 Default constructor. Use must subsequently call a define() function 00062 and a setup function. 00063 */ 00064 AMR(); 00065 00066 /// 00067 /** 00068 Destructor. 00069 */ 00070 virtual ~AMR(); 00071 00072 /// 00073 /** 00074 Defines this object. User must call a setup function before running. 00075 00076 Arguments: 00077 00078 <ul> 00079 <li>a_max_level (not modified): the maximum level allowed, where the 00080 base level is zero. There will be a total of a_max_level+1 00081 levels, since level zero and level a_max_level will both 00082 exist. 00083 <li>a_ref_ratios (not modified): refinement ratios. There must be at 00084 least a_max_level+1 elements, or an error will result. Element 00085 zero is the base level. 00086 <li>a_prob_domain (not modified): problem domain on the base level. 00087 <li>a_amrlevelFact (not modified): pointer to a physics class factory 00088 object. The object it points to is used to construct the 00089 collection of AMRLevel objects in this AMR as objects of the 00090 physics class type. It can be destructed after this define() 00091 returns. 00092 </ul> 00093 */ 00094 void define(int a_max_level, 00095 const Vector<int>& a_ref_ratios, 00096 const Box& a_prob_domain, 00097 const AMRLevelFactory* const a_amrLevelFact); 00098 00099 /// 00100 /** 00101 Defines this object. User must call a setup function before running. 00102 00103 Arguments: 00104 00105 <ul> 00106 <li>a_max_level (not modified): the maximum level allowed, where the 00107 base level is zero. There will be a total of a_max_level+1 00108 levels, since level zero and level a_max_level will both 00109 exist. 00110 <li>a_ref_ratios (not modified): refinement ratios. There must be at 00111 least a_max_level+1 elements, or an error will result. Element 00112 zero is the base level. 00113 <li>a_prob_domain (not modified): problem domain on the base level. 00114 <li>a_amrlevelFact (not modified): pointer to a physics class factory 00115 object. The object it points to is used to construct the 00116 collection of AMRLevel objects in this AMR as objects of the 00117 physics class type. It can be destructed after this define() 00118 returns. 00119 </ul> 00120 */ 00121 void define(int a_max_level, 00122 const Vector<int>& a_ref_ratios, 00123 const ProblemDomain& a_prob_domain, 00124 const AMRLevelFactory* const a_amrLevelFact); 00125 00126 #ifdef CH_USE_HDF5 00127 /// 00128 /** 00129 Sets up this object from checkpointed data. User must have 00130 previously called define(). 00131 00132 Need to call this function or setupForNewAMRRun() or 00133 setupforFixedHierarchyRun() before you run. 00134 */ 00135 void setupForRestart(HDF5Handle& a_handle); 00136 #endif 00137 00138 /// 00139 /** 00140 Sets up this object for cold start. User must have previously 00141 called define(). 00142 00143 Need to call this function or setupForRestart() or 00144 setupforFixedHierarchyRun() before you run. 00145 */ 00146 void setupForNewAMRRun(); 00147 00148 /** 00149 This function sets the hierarchy and sets m_regrid_intervals to -1 00150 (turns off regridding). If you want to keep regridding 00151 on, call regridIntervals() after this call. 00152 */ 00153 void setupForFixedHierarchyRun(const Vector<Vector<Box> >& a_amr_grids, 00154 int a_proper_nest = 1); 00155 00156 /// 00157 /** 00158 Runs the calculation. User must have previously called 00159 both the define() function and a setup function. 00160 */ 00161 void run(Real a_max_time, int a_max_step); 00162 00163 /// 00164 /** 00165 You should call this last. It writes the last 00166 checkpoint file and tells you how many cells you updated and 00167 all that. 00168 */ 00169 void conclude() const; 00170 00171 /** 00172 \name Parameter-setting functions 00173 */ 00174 00175 /**@{*/ 00176 00177 /// 00178 /** 00179 Sets the checkpoint file prefix. 00180 00181 Should be called after define() 00182 and before setup. 00183 */ 00184 void checkpointPrefix(const std::string& a_checkpointfile_prefix); 00185 00186 //! Tells AMR to write plot files after every \a a_plot_interval steps. 00187 void plotInterval(int a_plot_interval); 00188 00189 00190 //! Tells AMR to check each level for steady state and stop if we get there 00191 void checkForSteadyState(bool a_steadyState); 00192 00193 //! Tells AMR to write plot files after every \a a_plot_period time units. 00194 void plotPeriod(Real a_plot_period); 00195 00196 //! Sets up a schedule for periodically-called functions. 00197 void schedule(RefCountedPtr<Scheduler> a_scheduler); 00198 00199 /// 00200 /** 00201 Sets the interval to write checkpoint files, in terms of the base level 00202 time step. 00203 */ 00204 void checkpointInterval(int a_checkpoint_interval); 00205 00206 /// 00207 /** 00208 Set the maximum grid size. Should be called after define() 00209 and before setup. 00210 */ 00211 void maxGridSize(int a_max_grid_size); 00212 00213 /// 00214 /** 00215 Set the maximum grid size for level 0 grids. 00216 00217 Defaults to m_max_grid_size, so should be called after maxGridSize() 00218 and before setup. 00219 */ 00220 void maxBaseGridSize(int a_max_base_grid_size); 00221 00222 /// 00223 /** 00224 Set the factor by which the current dt must exceed the new (max) dt 00225 for time subcycling to occur (i.e., reduction of the current dt by 00226 powers of 2). 00227 */ 00228 void dtToleranceFactor(Real a_dt_tolerance_factor); 00229 00230 /// 00231 /** 00232 Set the MeshRefine instance. Should be called before define() 00233 if you want a_mesh_refine_ptr defined. 00234 */ 00235 void setMeshRefine(RefCountedPtr<MeshRefine> a_mesh_refine_ptr); 00236 00237 /// 00238 /** 00239 Set the fill ratio for MeshRefine. Should be called after define() 00240 and before setup. 00241 */ 00242 void fillRatio(Real a_fillRat); 00243 00244 /// 00245 /** 00246 Set the blocking factor for MeshRefine. Should be called after define() 00247 and before setup. 00248 */ 00249 void blockFactor(int a_blockFactor); 00250 00251 /// 00252 /** 00253 Set the buffering for MeshRefine. Should be called after define() 00254 and before setup. 00255 */ 00256 void gridBufferSize(int a_grid_buffer_size); 00257 00258 /// 00259 /** 00260 Sets verbosity level to a_verbosity. 00261 00262 -inf to 0: print nothing.<br> 00263 1: prints a message every coarse time step.<br> 00264 2: also prints a message when regridding and writing files.<br> 00265 3: also prints function trace, and some more inf during file I/O.<br> 00266 4 to inf: also prints list of boxes and processor maps during regrid.<br> 00267 00268 This should be OK to call any time after define and before run. 00269 */ 00270 void verbosity (int a_verbosity); 00271 00272 /// 00273 /** 00274 Sets the regridding intervals. This should be OK to call any time 00275 after define() and before run. 00276 */ 00277 void regridIntervals(const Vector<int>& a_regridIntervals); 00278 00279 /// 00280 /** Set maximum factor by which a timestep can grow. */ 00281 void maxDtGrow(Real a_dtGrowFactor); 00282 00283 /// 00284 /** Set amount by which two times may differ and still be considered 00285 equal (used to determine whether we've reached stop time yet) 00286 In practice, this is multiplied by the current base-level timestep, 00287 so we consider ourselves finished if (stopTime - time) < timeEps*dt0 00288 */ 00289 void timeEps(Real a_timeEps); 00290 00291 /// 00292 /** Set a fixed timestep. This must be called before calling 00293 setupForFixedHierarchyRun(), setupForNewAMRRun(), or setupForRestart(). 00294 Note also that fixedDt is not currently saved in checkpoint files 00295 */ 00296 void fixedDt(Real a_dt); 00297 00298 /// 00299 /** 00300 Sets the initial time before starting compututation; this must be 00301 called before calling setupForFixedHierarchyRun() or setupForNewAMRRun(). 00302 Note that this will be over-ridden by m_checkpointfile if 00303 restarting using setupForRestart(). 00304 */ 00305 void initialTime(Real a_initialTime); 00306 00307 /// 00308 /** 00309 Turn subcycling in time off or on. Default is true (use cycling). 00310 */ 00311 void useSubcyclingInTime(bool a_useSubcycling); 00312 00313 /// 00314 /** 00315 Sets the plot file prefix. 00316 00317 Should be called after define() 00318 and before setup. 00319 */ 00320 void plotPrefix(const std::string& a_plotfile_prefix); 00321 00322 /**@}*/ 00323 00324 /** 00325 \name Access functions 00326 */ 00327 00328 /**@{*/ 00329 00330 /// 00331 /** 00332 Has a define() function been called? Lots of these 00333 functions will assert fail if not. 00334 */ 00335 bool isDefined() const; 00336 00337 /// 00338 /** 00339 Has a setup function been called? If not, 00340 you can't call run(). 00341 */ 00342 bool isSetUp() const; 00343 00344 /// 00345 /** 00346 Returns the maximum grid size. 00347 */ 00348 int maxGridSize() const; 00349 00350 /// 00351 /** 00352 Returns the maximum grid size for level 0 grids. 00353 */ 00354 int maxBaseGridSize() const; 00355 00356 /// 00357 /** 00358 Returns current verbosity level. 00359 00360 -inf to 0: print nothing.<br> 00361 1: prints a message every coarse time step.<br> 00362 2: also prints a message when regridding and writing files.<br> 00363 3: also prints function trace, and some more inf during file I/O.<br> 00364 4 to inf: also prints list of boxes and processor maps during regrid.<br> 00365 */ 00366 int verbosity () const; 00367 00368 /// 00369 /** Returns current maximum factor by which a timestep can grow. */ 00370 Real maxDtGrow() const; 00371 00372 /// 00373 /** Returns amount by which two times may differ and still be considered 00374 equal (used to determine whether we've reached stop time yet) 00375 */ 00376 Real timeEps() const; 00377 00378 /// 00379 /** Returns the fixed timestep. */ 00380 Real fixedDt() const; 00381 00382 /// 00383 /** 00384 Returns a vector of all the AMR levels 00385 */ 00386 Vector<AMRLevel*> getAMRLevels(); 00387 00388 /// 00389 /** 00390 Returns the current time 00391 */ 00392 Real getCurrentTime() const; 00393 00394 #ifdef CH_USE_TIMER 00395 /// 00396 /** 00397 Returns the Chombo timer, and resets it if the arg is not NULL. 00398 */ 00399 Chombo::Timer * timer(Chombo::Timer *a_timer = NULL ); 00400 #endif 00401 00402 /**@}*/ 00403 00404 protected: 00405 00406 bool m_useSubcycling; 00407 // advance by dt on this level and all finer levels and return the 00408 // number of steps left - given the number of steps left on entry. 00409 int timeStep(int a_level, int a_stepsLeft, bool a_coarseTimeBoundary); 00410 00411 // internal use only 00412 void clearMemory(); 00413 00414 // make new grids. 00415 void regrid(int a_base_level); 00416 00417 // make initial grids. 00418 // not an accurate name, since there are no grids to re. 00419 void initialGrid(); 00420 00421 void writePlotFile() const; 00422 00423 void writeCheckpointFile() const; 00424 00425 // computes maximum stable time step given the maximum stable time 00426 // step on the individual levels. 00427 void assignDt(); 00428 00429 // whether regridding should be done now. 00430 bool needToRegrid(int a_level, int a_numStepsLeft) const; 00431 00432 virtual void makeBaseLevelMesh (Vector<Box>& a_grids) const; 00433 00434 void setDefaultValues(); 00435 00436 int m_blockFactor; 00437 Real m_fillRatio; 00438 int m_max_level; 00439 int m_finest_level_old; 00440 int m_finest_level; 00441 int m_checkpoint_interval; 00442 int m_plot_interval; 00443 Real m_plot_period; 00444 Real m_next_plot_time; 00445 int m_max_grid_size; 00446 int m_max_base_grid_size; 00447 Real m_dt_tolerance_factor; 00448 Real m_fixedDt; 00449 bool m_checkForSteadyState; 00450 bool m_isDefined; 00451 bool m_isSetUp; 00452 Vector<AMRLevel*> m_amrlevels; 00453 Vector<int> m_ref_ratios; 00454 Vector<int> m_reduction_factor; 00455 Vector<int> m_regrid_intervals; 00456 00457 Real m_dt_base; 00458 // New (maximum) dt 00459 Vector<Real> m_dt_new; 00460 // Current dt 00461 Vector<Real> m_dt_cur; 00462 Real m_maxDtGrow; 00463 Real m_time_eps; 00464 00465 RefCountedPtr<MeshRefine> m_mesh_refine_ptr; 00466 bool m_use_meshrefine; 00467 00468 Vector<Vector<Box> > m_amr_grids; 00469 00470 int m_cur_step; 00471 int m_restart_step; 00472 int m_lastcheck_step; 00473 00474 Real m_cur_time; 00475 Vector<int> m_steps_since_regrid; 00476 Vector<long long> m_cell_updates; 00477 00478 std::string m_plotfile_prefix; 00479 std::string m_checkpointfile_prefix; 00480 00481 int m_verbosity; 00482 00483 RefCountedPtr<Scheduler> m_scheduler; 00484 #ifdef CH_USE_TIMER 00485 Chombo::Timer *m_timer; //assumes the application manages the memory 00486 #endif 00487 }; 00488 00489 #include "NamespaceFooter.H" 00490 #endif