00001
00002
00003
00004
00005
00006 #ifndef AMROC_AMRPREADAPT_SOLVER_H
00007 #define AMROC_AMRPREADAPT_SOLVER_H
00008
00016 #include "AMRSolver.h"
00017
00018 #include <iostream>
00019 #include <string>
00020 #include <cstdio>
00021
00022 #define MAXLEVELS 5
00023
00024 template <int dim>
00025 class Patch : public controlable {
00026 public:
00027 Patch() {
00028 lb.rank = dim;
00029 ub.rank = dim;
00030 s.rank = dim;
00031 }
00032
00033 virtual ~Patch() {}
00034
00035 virtual void register_at(ControlDevice& Ctrl) {}
00036 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00037 LocCtrl = Ctrl.getSubDevice(prefix+"Patch");
00038 for (int d=0; d<dim; d++) {
00039 char text[8];
00040 std::sprintf(text,"lb(%d)",d+1);
00041 RegisterAt(LocCtrl,text,lb(d));
00042 std::sprintf(text,"ub(%d)",d+1);
00043 RegisterAt(LocCtrl,text,ub(d));
00044 std::sprintf(text,"s(%d)",d+1);
00045 RegisterAt(LocCtrl,text,s(d));
00046 }
00047 }
00048
00049 public:
00050 Coords lb, ub, s;
00051 ControlDevice LocCtrl;
00052 };
00053
00054
00062 template <class VectorType, class FixupType, class FlagType, int dim>
00063 class AMRPreAdaptSolver :
00064 public AMRSolver<VectorType,FixupType,FlagType,dim> {
00065
00066 typedef AMRSolver<VectorType,FixupType,FlagType,dim> base;
00067
00068 typedef Patch<dim> patch_type;
00069
00070 public:
00071 AMRPreAdaptSolver(typename base::integrator_type& integ,
00072 typename base::initial_condition_type& init,
00073 typename base::boundary_conditions_type& bc) :
00074 base(integ, init, bc), SwitchTime(0) {
00075 for (int c=0; c<MAXLEVELS; c++) {
00076 li[c] = 0;
00077 ls[c] = 0;
00078 PatchListInit[c] = (patch_type*) 0;
00079 PatchListSwitch[c] = (patch_type*) 0;
00080 }
00081 }
00082
00083 virtual ~AMRPreAdaptSolver() {
00084 for (int c=0; c<MAXLEVELS; c++) {
00085 if (PatchListInit[c])
00086 delete [] PatchListInit[c];
00087 if (PatchListSwitch[c])
00088 delete [] PatchListSwitch[c];
00089 }
00090 }
00091
00092 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00093 base::register_at(Ctrl,prefix);
00094 ControlDevice LocCtrlInit = base::LocCtrl.getSubDevice(prefix+"InitialRefinement");
00095 int c;
00096 for (c=0; c<MAXLEVELS; c++) {
00097 char LevelControlsName[24];
00098 std::sprintf(LevelControlsName,"PatchesOnLevel%d",c+1);
00099 RegisterAt(LocCtrlInit,LevelControlsName,li[c]);
00100 }
00101
00102 RegisterAt(base::LocCtrl,"SwitchTime",SwitchTime);
00103 ControlDevice LocCtrlSwitch = base::LocCtrl.getSubDevice(prefix+"SwitchRefinement");
00104 for (c=0; c<MAXLEVELS; c++) {
00105 char LevelControlsName[24];
00106 std::sprintf(LevelControlsName,"PatchesOnLevel%d",c+1);
00107 RegisterAt(LocCtrlSwitch,LevelControlsName,ls[c]);
00108 }
00109 }
00110 virtual void register_at(ControlDevice& Ctrl) {
00111 register_at(Ctrl, "");
00112 }
00113
00114 virtual void SetupData() {
00115 base::SetupData();
00116
00117 ControlDevice PatchCtrlInit(GetFileControlDevice("patches_init.in",""));
00118 int m;
00119 for (m=0; m<MAXLEVELS; m++) {
00120 PatchListInit[m] = new patch_type[li[m]];
00121 for (int i=0; i<li[m]; i++) {
00122 char text[5];
00123 std::sprintf(text,"%d-%d-",m+1,i+1);
00124 (PatchListInit[m])[i].register_at(PatchCtrlInit,std::string(text));
00125 }
00126 }
00127 PatchCtrlInit.update();
00128
00129 ControlDevice PatchCtrlSwitch(GetFileControlDevice("patches_switch.in",""));
00130 for (m=0; m<MAXLEVELS; m++) {
00131 PatchListSwitch[m] = new patch_type[ls[m]];
00132 for (int i=0; i<ls[m]; i++) {
00133 char text[5];
00134 std::sprintf(text,"%d-%d-",m+1,i+1);
00135 (PatchListSwitch[m])[i].register_at(PatchCtrlSwitch,std::string(text));
00136 }
00137 }
00138 PatchCtrlSwitch.update();
00139 }
00140
00141 virtual void Initialize_(const double& dt_start) {
00142 BBoxList bblist[MAXLEVELS];
00143 int m;
00144 for (m=0; m<MAXLEVELS; m++)
00145 for (int i=0; i<li[m]; i++) {
00146 std::cout << ((PatchListInit[m])[i].lb)*RefinedBy(base::GH(),MaxLevel(base::GH()))
00147 << " "
00148 << ((PatchListInit[m])[i].ub)*RefinedBy(base::GH(),MaxLevel(base::GH()))
00149 << " "
00150 << ((PatchListInit[m])[i].s)*StepSize(base::GH(),m) << std::endl;
00151
00152 bblist[m].add(BBox(((PatchListInit[m])[i].lb)*RefinedBy(base::GH(),MaxLevel(base::GH())),
00153 ((PatchListInit[m])[i].ub)*RefinedBy(base::GH(),MaxLevel(base::GH())),
00154 ((PatchListInit[m])[i].s)*StepSize(base::GH(),m)));
00155 }
00156
00157 for (m=0; m<MaxLevel(base::GH());m++)
00158 if (li[m] > 0) {
00159 Refine(base::GH(),bblist[m],m);
00160 RecomposeHierarchy(base::GH());
00161 }
00162
00163 int Store = base::RegridEvery;
00164 base::RegridEvery = 0;
00165 base::Initialize_(dt_start);
00166 base::RegridEvery = Store;
00167 }
00168
00169 virtual double Tick(int VariableTimeStepping, const double dtv[], const double cflv[],
00170 int& Rejections) {
00171 if (SwitchTime>0 &&
00172 SwitchTime*RefinedBy(base::GH(),MaxLevel(base::GH())) == CurrentTime(base::GH(),0)) {
00173 BBoxList bblist[MAXLEVELS];
00174 int m;
00175 for (m=0; m<MAXLEVELS; m++)
00176 for (int i=0; i<ls[m]; i++) {
00177 bblist[m].add(BBox(((PatchListSwitch[m])[i].lb)*RefinedBy(base::GH(),MaxLevel(base::GH())),
00178 ((PatchListSwitch[m])[i].ub)*RefinedBy(base::GH(),MaxLevel(base::GH())),
00179 ((PatchListSwitch[m])[i].s)*StepSize(base::GH(),m)));
00180 }
00181
00182 for (m=0; m<MaxLevel(base::GH());m++)
00183 if (ls[m] > 0) {
00184 Refine(base::GH(),bblist[m],m);
00185 RecomposeHierarchy(base::GH());
00186 }
00187 }
00188
00189 return (base::Tick(VariableTimeStepping, dtv, cflv, Rejections));
00190 }
00191
00192 protected:
00193 int SwitchTime;
00194 int li[MAXLEVELS];
00195 int ls[MAXLEVELS];
00196 patch_type* PatchListInit[MAXLEVELS];
00197 patch_type* PatchListSwitch[MAXLEVELS];
00198 };
00199
00200 #endif