00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef AMROC_RELATIVE_ERROR_H
00010 #define AMROC_RELATIVE_ERROR_H
00011
00018 #include "StdCriterion.h"
00019
00020 #define VizServer 0
00021
00022 template <class VectorType, class FixupType, class FlagType, int dim> class AMRSolverBase;
00023
00030 template <class VectorType, class FixupType, class FlagType, int dim>
00031 class RelativeError :
00032 public StdCriterion<VectorType,FlagType,dim> {
00033 typedef typename VectorType::InternalDataType DataType;
00034 typedef StdCriterion<VectorType,FlagType,dim> base;
00035 public:
00036 typedef AMRSolverBase<VectorType,FixupType,FlagType,dim> solver_type;
00037 typedef typename base::vec_grid_fct_type vec_grid_fct_type;
00038 typedef typename base::grid_fct_type grid_fct_type;
00039 typedef typename base::flag_fct_type flag_fct_type;
00040 typedef typename base::max_vector_type max_vector_type;
00041
00042 RelativeError(solver_type& solver) : base(), _Solver(solver) {
00043 base::SetShadowCriterion(true);
00044 base::SetUpdateShadow(true);
00045 Order = std::pow(2.0,static_cast<DataType>(solver.NMethodOrder())+1.0) - 2.0;
00046 Maxv = static_cast<DataType>(0.0);
00047 Tol = static_cast<DataType>(0.0);
00048 Sc = static_cast<DataType>(0.0);
00049 ScTol = static_cast<DataType>(0.0);
00050 }
00051
00052 virtual ~RelativeError() {}
00053
00054 virtual void register_at(ControlDevice& Ctrl) {}
00055 virtual void register_at(ControlDevice& Ctrl, const std::string& prefix) {
00056 base::LocCtrl = Ctrl.getSubDevice(prefix+"RelativeError");
00057 char Name[16];
00058 for (int d=0; d<max_vector_type::Length(); d++) {
00059 std::sprintf(Name,"Max(%d)",d+1);
00060 RegisterAt(base::LocCtrl,Name,Maxv(d));
00061 std::sprintf(Name,"Tol(%d)",d+1);
00062 RegisterAt(base::LocCtrl,Name,Tol(d));
00063 std::sprintf(Name,"ScTol(%d)",d+1);
00064 RegisterAt(base::LocCtrl,Name,ScTol(d));
00065 std::sprintf(Name,"MaxLev(%d)",d+1);
00066 RegisterAt(base::LocCtrl,Name,base::_MaxLevel[d]);
00067 }
00068 RegisterAt(base::LocCtrl,"Output",base::_Output);
00069 }
00070
00071 virtual void update() {
00072 Sc = ScTol*Maxv;
00073 int d=max_vector_type::Length();
00074 for (; d>0; d--)
00075 if (Tol(d-1)>static_cast<DataType>(0.0) &&
00076 Sc(d-1)>static_cast<DataType>(0.0))
00077 break;
00078 if (d>0) {
00079 base::SetNcnt(d);
00080 base::SetIsUsed(true);
00081 }
00082 else
00083 base::SetIsUsed(false);
00084 }
00085
00086 virtual bool SetFlags(vec_grid_fct_type& u, grid_fct_type& work, flag_fct_type& flags,
00087 const int& cnt, const int& Time, const int& Level, const double& t,
00088 const FlagType& FlagValue) {
00089 if (Tol(cnt)<=0.0 || Sc(cnt)<=0.0) return false;
00090 int TStep = TimeStep(work,Level);
00091 if (Time==0 || (base::MaxLevel(cnt)<Level && base::MaxLevel(cnt)>=0)) {
00092 if (base::OutputFlags()) {
00093 forall(work,Time,Level,c)
00094 work(Time+TStep,Level,c) = static_cast<DataType>(0.0);
00095 end_forall
00096 }
00097 return true;
00098 }
00099
00100 forall(u,Time,Level,c)
00101 equals_from(work(Time,Level,c), u(Time,Level,c), cnt);
00102 equals_from(work(Time+TStep,Level,c), u(Time+TStep,Level,c), cnt);
00103 work(Time+TStep,Level,c).minus(work(Time,Level,c));
00104 base::Abs(work(Time+TStep,Level,c));
00105 end_forall
00106
00107 if (base::OutputFlags() && Solver_().LastOutputTime()==Time && Level==0) {
00108 DataType Maxv = MaxVal(work, Time, 0);
00109 if (MY_PROC == VizServer)
00110 std::cout << " Max(" << cnt+1 << ") on L0=" << Maxv << " ";
00111 }
00112
00113 forall(u,Time,Level,c)
00114 base::Abs(work(Time,Level,c));
00115 base::Max(work(Time,Level,c),Sc(cnt));
00116 work(Time+TStep,Level,c).divide(work(Time,Level,c));
00117 work(Time+TStep,Level,c).divide(Order);
00118 end_forall
00119
00120 base::FlagByValue(work, flags, Time, Level, Tol(cnt), FlagValue);
00121 return true;
00122 }
00123
00124 virtual void OutputName(char* name, int cnt) { std::sprintf(name,"relerr_%d",cnt+1); }
00125 inline solver_type& Solver_() { return _Solver; }
00126 inline const solver_type& Solver_() const { return _Solver; }
00127
00128 solver_type& _Solver;
00129 max_vector_type Tol, ScTol, Sc, Maxv;
00130 DataType Order;
00131 };
00132
00133 #endif