DUNE-ACFEM (2.5.1)

marking.hh
1#ifndef MARKING_HH
2#define MARKING_HH
3
4#include "../estimators/estimatorinterface.hh"
5
6namespace Dune {
7
8namespace ACFem {
9
10template<class Estimator>
11class MarkingStrategy
12{
13 public:
14 typedef EstimatorInterface<Estimator> EstimatorType;
15 protected:
16 typedef typename EstimatorType::DiscreteFunctionSpaceType
17 DiscreteFunctionSpaceType;
18 typedef typename EstimatorType::ElementType ElementType;
19 typedef typename DiscreteFunctionSpaceType::IteratorType IteratorType;
20
21 const EstimatorType& estimator_;
22 const std::string name_;
23 public:
24 MarkingStrategy(const EstimatorType& estimator, const std::string& name = "adaptation")
25 : estimator_(estimator), name_(name)
26 {}
27
29 bool mark (const double tolerance) const
30 {
31 // possible strategies
32 enum Strategy { none = 0, maximum = 1, equitol = 2, equiv = 3, uniform = 4 };
33 static const std::string strategyNames []
34 = { "none", "maximum", "equitolerance", "equidistribution", "uniform" };
35 Strategy strategy = (Strategy) Dune::Fem::Parameter::getEnum(name_ + "." + "markingStrategy", strategyNames);
36
37 double localTol2 = Dune::Fem::Parameter::getValue<double>(name_ + ".refineTolerance", 0);
38 double coarseTol2 = Dune::Fem::Parameter::getValue<double>(name_ + ".coarsenTolerance", std::numeric_limits<double>::max());
39 double coarseTolUnif = Dune::Fem::Parameter::getValue<double>(name_ + ".coarsenToleranceUniform", 0.05);
40
41 int refineBisections = 1;
42 int coarseBisections = 1;
43
44 localTol2 *= localTol2;
45 coarseTol2 *= coarseTol2;
46
47 switch(strategy) {
48 case maximum: {
49 double maxError2 = 0;
50 // sum up local estimators
51 const typename EstimatorType::IteratorType endEstimator = estimator_.end();
52 for (typename EstimatorType::IteratorType it = estimator_.begin();
53 it != endEstimator; ++it) {
54 maxError2 = std::max(*it, maxError2);
55 }
56
57 // get global maxError
58 maxError2 = estimator_.grid().comm().max(maxError2);
59 localTol2 *= maxError2;
60 coarseTol2 *= maxError2;
61
62 if (Dune::Fem::Parameter::verbose()) {
63 std::cerr << "max: " << maxError2 << std::endl;
64 std::cerr << "local: " << localTol2 << std::endl;
65 std::cerr << "coarse: " << coarseTol2 << std::endl;
66 }
67 break;
68 }
69
70 case equitol: { // try to equidistribute the given tolerance
71 // sum up local estimators
72 size_t indSize = estimator_.size();
73
74 // get global number of elements
75 estimator_.grid().comm().sum(&indSize, 1);
76
77 double avg2 = tolerance * tolerance / indSize;
78
79 localTol2 *= avg2;
80 coarseTol2 *= avg2;
81
82 if (Dune::Fem::Parameter::verbose()) {
83 std::cerr << "squared mean tolerance: " << avg2 << std::endl;
84 std::cerr << "local: " << localTol2 << std::endl;
85 std::cerr << "coarse: " << coarseTol2 << std::endl;
86 }
87 break;
88 }
89
90 case equiv: { //independent form given tolerance
91 double sumError2 = 0;
92 // sum up local estimators
93 size_t indSize = estimator_.size();
94 for (size_t i = 0; i < indSize; ++i) {
95 sumError2 += estimator_[i];
96 }
97 // get global sum of number of elements and local error sum
98 double buffer[2] = { (double)indSize , sumError2 };
99 estimator_.grid().comm().sum(buffer, 2);
100
101 sumError2 = buffer[1];
102 indSize = buffer[0];
103
104 double avg2 = sumError2 / indSize;
105
106 localTol2 *= avg2;
107 coarseTol2 *= avg2;
108
109 if (Dune::Fem::Parameter::verbose()) {
110 std::cerr << "estimate: " << std::sqrt(sumError2) << std::endl;
111 std::cerr << "squared mean: " << avg2 << std::endl;
112 std::cerr << "local: " << localTol2 << std::endl;
113 std::cerr << "coarse: " << coarseTol2 << std::endl;
114 }
115 break;
116 }
117
118 case uniform: {
119 double sumError2 = 0;
120 // sum up local estimators
121 size_t indSize = estimator_.size();
122 for (size_t i=0; i < indSize; ++i) {
123 sumError2 += estimator_[i];
124 }
125 // get global sum of number of elements and local error sum
126 double buffer[2] = { (double)indSize , sumError2 };
127 estimator_.grid().comm().sum(buffer, 2);
128
129 sumError2 = buffer[1];
130 indSize = buffer[0];
131
132 coarseTolUnif *= coarseTolUnif;
133
134 refineBisections = coarseBisections = Dune::DGFGridInfo<typename Estimator::GridType>::refineStepsForHalf();
135
136 // if global sum > tol, then refine all
137 if (sumError2 > tolerance*tolerance ) {
138 localTol2 = -1;
139 coarseTol2 = -1;
140 }
141 // if global sum < tol*coarsenToleranceUniform (default 0.05), then coarse all
142 else if (sumError2 < tolerance*tolerance*coarseTolUnif ) {
145 }
146 // else do nothing
147 else {
149 coarseTol2 = -1;
150 }
151 break;
152 }
153
154 case none: {
155 // settings to do nothing
157 coarseTol2 = -1;
158 break;
159 }
160 default:
161 break;
162 }
163
164 int marked = 0;
165 int notmarked = 0;
166 int coarseMarked = 0;
167 // loop over all elements
168 const IteratorType end = estimator_.space().end();
169 for (IteratorType it = estimator_.space().begin(); it != end; ++it) {
170 const ElementType &entity = *it;
171 // check local error indicator
172 if (estimator_[entity] > localTol2) {
173 // mark entity for refinement
174 estimator_.grid().mark(refineBisections, entity);
175 //std::cerr << "refineBisections: " << refineBisections << std::endl;
176 // grid was marked
177 ++marked;
178 } else if (estimator_[entity] < coarseTol2) {
179 estimator_.grid().mark(-coarseBisections, entity);
180 ++coarseMarked;
181 } else {
182 ++notmarked;
183 }
184 }
185
186 // get globally marked
187 int comarray[] = { marked, coarseMarked, notmarked };
188 estimator_.grid().comm().sum(comarray, 3);
189 marked = comarray[0];
190 coarseMarked = comarray[1];
191 notmarked = comarray[2];
192
193 if (Dune::Fem::Parameter::verbose()) {
194 std::cout << "Marked Elements: " << marked << std::endl;
195 std::cout << "Coarsen Marked: " << coarseMarked << std::endl;
196 std::cout << "Elements not marked: " << notmarked << std::endl;
197 }
198
199 return bool(marked + coarseMarked);
200 }
201
202};
203
204} // ACFem
205} // Dune
206
207#endif
LocalFunctionWrapper< LocalMaxAdapter< GridFunction1, GridFunction2 >, typename GridFunction1::GridPartType > max(const Fem::Function< typename GridFunction1::FunctionSpaceType, GridFunction1 > &f1, const Fem::Function< typename GridFunction2::FunctionSpaceType, GridFunction2 > &f2, const std::string &name="")
Pointwise maximum of two given functions.
Definition: maxfunction.hh:121
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:28, 2025)