vtf-logo

clawpack/applications/scaling/2d/Ramp/src/Problem.h

00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2002 Ralf Deiterding
00004 // Brandenburgische Universitaet Cottbus
00005 
00006 #ifndef AMROC_PROBLEM_H
00007 #define AMROC_PROBLEM_H
00008 
00009 #include "euler2.h"
00010 
00011 #undef  NAUX
00012 #define NAUX      4
00013 
00014 #include "ClpProblem.h"
00015 
00016 #define OWN_FLAGGING
00017 #define OWN_AMRSOLVER
00018 #include "ClpStdProblem.h"
00019 
00020 class ScaledGradientSpecific : public ScaledGradient<VectorType,FlagType,DIM> {
00021   typedef ScaledGradient<VectorType,FlagType,DIM> base;
00022   typedef VectorType::InternalDataType DataType;
00023 public:
00024   typedef base::grid_fct_type grid_fct_type;
00025   typedef base::flag_fct_type flag_fct_type;
00026 
00027   virtual bool FlagByScaledGradient(grid_fct_type& work, flag_fct_type& flags,
00028                                     const int& Time, const int& Level, 
00029                                     const DataType& TolVal, const FlagType& FlagValue) { 
00030     if (TolVal <= 0.0) return false;
00031     int TStep = TimeStep(work,Level);
00032     forall(work,Time,Level,c)
00033       work(Time+TStep,Level,c) = 0.0;
00034     end_forall 
00035     int East[2]  = { 1, 0 }; int West[2]  = { -1, 0 };
00036     int North[2] = { 0, 1 }; int South[2] = { 0, -1 };
00037     base::Difference(work, Time, Level, East, West);
00038     base::Difference(work, Time, Level, North, South);      
00039     base::FlagByValue(work, flags, Time, Level, TolVal, FlagValue);
00040     return true;
00041   }
00042 };
00043 
00044 class FlaggingSpecific : 
00045   public AMRFlagging<VectorType,FixupType,FlagType,DIM> {
00046   typedef AMRFlagging<VectorType,FixupType,FlagType,DIM> base;
00047   typedef base::solver_type solver_type;
00048 public:
00049   FlaggingSpecific(solver_type& solver) : base(solver) {
00050       base::AddCriterion(new ScaledGradientSpecific());
00051       base::AddCriterion(new AbsoluteError<VectorType,FixupType,FlagType,DIM>(solver));
00052     }
00053   ~FlaggingSpecific() { DeleteAllCriterions(); }
00054 };
00055 
00056 class SolverSpecific : 
00057   public AMRSolver<VectorType,FixupType,FlagType,DIM> {
00058   typedef AMRSolver<VectorType,FixupType,FlagType,DIM> base;
00059 public:
00060   SolverSpecific(IntegratorSpecific& integ, 
00061                  base::initial_condition_type& init,
00062                  base::boundary_conditions_type& bc) :
00063     AMRSolver<VectorType,FixupType,FlagType,DIM>(integ, init, bc) {
00064     SetLevelTransfer(new F77LevelTransfer<VectorType,DIM>(f_prolong, f_restrict));
00065 #ifdef f_flgout
00066     SetFileOutput(new F77FileOutput<VectorType,DIM>(f_flgout)); 
00067 #else   
00068     SetFileOutput(new FileOutput<VectorType,DIM>()); 
00069 #endif
00070     SetFixup(new FixupSpecific(integ));
00071     SetFlagging(new FlaggingSpecific(*this)); 
00072     last[0]=0.; last[1]=0.; last[2]=0.;
00073     TraceDumpEvery = 1;
00074   }  
00075 
00076   ~SolverSpecific() {
00077     delete _LevelTransfer;
00078     delete _Flagging;
00079     delete _Fixup;
00080     delete _FileOutput;
00081   }
00082 
00083   virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00084     base::register_at(Ctrl,prefix);
00085     RegisterAt(base::LocCtrl,"TraceDumpEvery",TraceDumpEvery);
00086   } 
00087   virtual void register_at(ControlDevice& Ctrl) {
00088     base::register_at(Ctrl);
00089   }
00090 
00091   virtual void Initialize(double& t, double& dt) {
00092     base::Initialize(t,dt);
00093     TraceDump(0);
00094   }
00095 
00096   virtual void Advance(double& t, double& dt) {
00097     base::Advance(t,dt);
00098     int TimeZero  = CurrentTime(base::GH(),0);
00099     TimeZero /= RefinedBy(base::GH(),MaxLevel(base::GH()));
00100     if (TimeZero%TraceDumpEvery==0 && TraceDumpEvery>0) 
00101       TraceDump(TimeZero/TraceDumpEvery);
00102   }
00103 
00104   void TraceDump(int time) {
00105     int me = MY_PROC; 
00106 #ifdef TIMING_AMR
00107     double stats[3], current[3];
00108     timing.collect_timing(comm_service::comm(),Timing::INTEGRATION_MAIN,current);
00109     timing.collect_timing(comm_service::comm(),Timing::INTEGRATION_ESTIMATE,stats);
00110     if (me == VizServer) {
00111       current[0]+=stats[0]; current[1]+=stats[1]; current[2]+=stats[2];
00112     }
00113     timing.collect_timing(comm_service::comm(),Timing::INTEGRATION_SHADOW,stats);
00114     if (me == VizServer) {
00115       current[0]+=stats[0]; current[1]+=stats[1]; current[2]+=stats[2];
00116     }
00117     timing.collect_timing(comm_service::comm(),Timing::SOURCE_INTEGRATION,stats);
00118     if (me == VizServer) {
00119       current[0]+=stats[0]; current[1]+=stats[1]; current[2]+=stats[2];
00120     }
00121     if (me == VizServer) {
00122       stats[0] = current[0]-last[0]; stats[1] = current[1]-last[1]; stats[2] = current[2]-last[2]; 
00123       std::ofstream ofst("balance.txt", std::ios::out | std::ios::app);
00124       char str[128];
00125       std::sprintf(str, "\t%3.9f    %2.4f    %2.4f    %2.4f", base::t[0], 
00126                    (stats[2]>0. ? stats[0]/stats[2] : 1.),
00127                    (stats[2]>0. ? stats[1]/stats[2] : 1.),
00128                    (stats[2]>0. ? (stats[0]-stats[1])/stats[2] : 1.));
00129       ofst << time << str << std::endl;
00130       ofst.close();
00131     }
00132 #endif
00133 
00134     if (me == VizServer) {
00135       int id=0;
00136       std::ofstream ofs("trace.txt", std::ios::out | std::ios::app);
00137       for (register int l=0; l<=FineLevel(base::GH()); l++)
00138         for (GridBox *gb=base::GH().ggbl(l)->first();gb;gb=base::GH().ggbl(l)->next())
00139           dumpBox(gb->gbBBox(), l, gb->gbOwner(), time, id++, ofs);
00140       ofs.close();
00141     }
00142   }
00143 
00144   void dumpBox(const BBox &bb, int l, int proc, int time, int id, std::ofstream &ofs) {
00145     ofs << time << "\t" << proc << "\t" << l << "\t" << id <<"\t";
00146     ofs << bb.extents(0) <<"\t";
00147     if (bb.rank>1) ofs << bb.extents(1) <<"\t";
00148     else ofs << "-1\t";
00149     if (bb.rank>2) ofs << bb.extents(2) <<"\t";
00150     else ofs << "-1\t";
00151     ofs << bb.lower(0) <<"\t";
00152     if (bb.rank>1) ofs << bb.lower(1) <<"\t";
00153     else ofs << "-1\t";
00154     if (bb.rank>2) ofs << bb.lower(2) <<"\t";
00155     else ofs << "-1\t";
00156     ofs << bb.upper(0) <<"\t";
00157     if (bb.rank>1) ofs << bb.upper(1) <<"\t";
00158     else ofs << "-1\t";
00159     if (bb.rank>2) ofs << bb.upper(2) <<"\t";
00160     else ofs << "-1\t";
00161     ofs << std::endl;
00162   }
00163 
00164 protected:
00165   double last[3];
00166   int TraceDumpEvery;