SheafSystem  0.0.0.0
zone_centered_error_bound_refinement_policy.cc
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #include "SheafSystem/zone_centered_error_bound_refinement_policy.h"
22 
23 #include "SheafSystem/assert_contract.h"
24 #include "SheafSystem/factory.h"
25 #include "SheafSystem/field_refinement_buffer.h"
26 #include "SheafSystem/std_cmath.h"
27 #include "SheafSystem/field_vd.h"
28 
29 using namespace std;
30 using namespace fields; // Workaround for MS C++ bug.
31 
32 // ===========================================================
33 // ZONE_CENTERED_ERROR_BOUND_REFINEMENT_POLICY FACET
34 // ===========================================================
35 
36 // PUBLIC MEMBER FUNCTIONS
37 
40  sec_vd_value_type xerror_ub)
41  : field_refinement_policy(xrefinement_depth_ub),
42  _error_ub(xerror_ub)
43 {
44 
45  // Preconditions:
46 
47 
48  // Body:
49 
50  // Postconditions:
51 
52  ensure(invariant());
53  ensure(refinement_depth_ub() == xrefinement_depth_ub);
54  ensure(error_ub() == xerror_ub);
55 
56  // Exit:
57 
58  return;
59 }
60 
63  : field_refinement_policy(xother),
64  _error_ub(xother._error_ub)
65 {
66 
67  // Preconditions:
68 
69 
70  // Body:
71 
73  _error_ub = xother._error_ub;
74 
75  // Postconditions:
76 
77  ensure(invariant());
78 
79  // Exit:
80 
81  return;
82 }
83 
86 {
87  // Preconditions:
88 
89 
90  // Body:
91 
92  // Nothing to do.
93 
94  // Postconditions:
95 
96  // Exit:
97 
98  return;
99 }
100 
103 error_ub() const
104 {
105  return _error_ub;
106 }
107 
108 void
111 {
112  // Preconditions:
113 
114  require(xub > 0.0);
115 
116  // Body:
117 
118  _error_ub = xub;
119 
120  // Postconditions:
121 
122  ensure(error_ub() == xub);
123 
124  // Exit:
125 
126  return;
127 }
128 
129 
130 // PRIVATE MEMBER FUNCTIONS
131 
132 bool
133 fields::zone_centered_error_bound_refinement_policy::
134 _has_prototype = make_prototype();
135 
136 bool
137 fields::zone_centered_error_bound_refinement_policy::
138 make_prototype()
139 {
140  bool result = true;
141 
142  // Preconditions:
143 
144 
145  // Body:
146 
149 
150  policy_factory().insert_prototype(lproto);
151 
152  // Postconditions:
153 
154  ensure(policy_factory().contains_prototype(static_class_name()));
155 
156  // Exit:
157 
158  return result;
159 }
160 
161 
162 // ===========================================================
163 // FIELD_REFINEMENT_POLICY FACET
164 // ===========================================================
165 
166 // PUBLIC MEMBER FUNCTIONS
167 
168 bool
170 should_refine(field_refinement_buffer& xbuffer, size_type xrefinement_depth) const
171 {
172  bool result;
173 
174  // Preconditions:
175 
176  require(xbuffer.source.state_is_read_accessible());
177  require(xbuffer.target.state_is_read_accessible());
178  require(xbuffer.target.same_evaluation());
179 
180  // Body:
181 
182  result = (xrefinement_depth < _refinement_depth_ub);
183 
184 #ifdef DIAGNOSTIC_OUTPUT
185 
186  cout << " zone_id: " << xbuffer.zone_id
187  << " refinement_depth: " << xrefinement_depth;
188 #endif
189 
190  if(result)
191  {
192  // Evaluate the coordinates, property and source property
193  // at the center of the zone.
194 
195  xbuffer.evaluate_at_center();
196 
197  // Compute the error using the L1 norm.
198 
199  sec_vd_value_type lerror_norm = 0.0;
200  sec_vd_value_type lprop_norm = 0.0;
201 
202  size_type ldp = xbuffer.target.dp();
203  for(size_type i=0; i<ldp; ++i)
204  {
205  sec_vd_value_type lsrc_val = xbuffer.source_prop_value[i];
206  lprop_norm += abs(lsrc_val);
207  lerror_norm += abs(xbuffer.prop_value[i] - lsrc_val);
208  }
209 
210  // Check the difference.
212 
213  if(lprop_norm != 0.0)
214  {
215  result = (lerror_norm > (ldp*lprop_norm*_error_ub));
216  }
217  else
218  {
219  result = (lerror_norm > (ldp*_error_ub));
220  }
221 
222 #ifdef DIAGNOSTIC_OUTPUT
223  cout << " prop: " << xbuffer.prop_value[0]
224  << " src: " << xbuffer.source_prop_value[0]
225  << " prop norm: " << lprop_norm
226  << " error norm: " << lerror_norm;
227 #endif
228 
229  }
230 
231 #ifdef DIAGNOSTIC_OUTPUT
232  cout << " result: " << boolalpha << result << noboolalpha << endl;
233 #endif
234 
235  // Postconditions:
236 
237  ensure( result ? xrefinement_depth < refinement_depth_ub() : true);
238 
239  // Exit:
240 
241  return result;
242 }
243 
244 const std::string&
246 class_name() const
247 {
248  // Preconditions:
249 
250  // Body:
251 
252  const string& result = static_class_name();
253 
254  // Postconditions:
255 
256  // Exit:
257 
258  return result;
259 }
260 
261 const std::string&
264 {
265  // Preconditions:
266 
267  // Body:
268 
269  static const string result("zone_centered_error_bound_refinement_policy");
270 
271  // Postconditions:
272 
273  // Exit:
274 
275  return result;
276 }
277 
278 
279 // ===========================================================
280 // ANY FACET
281 // ===========================================================
282 
283 // PUBLIC MEMBER FUNCTIONS
284 
285 bool
287 is_ancestor_of(const any* other) const
288 {
289 
290  // Preconditions:
291 
292  require(other != 0);
293 
294  // Body:
295 
296  // True if other conforms to this
297 
298  bool result = dynamic_cast<const zone_centered_error_bound_refinement_policy*>(other) != 0;
299 
300  // Postconditions:
301 
302  return result;
303 }
304 
307 clone() const
308 {
310 
311  // Preconditions:
312 
313  // Body:
314 
316 
317  // Postconditions:
318 
319  ensure(result != 0);
320  ensure(is_same_type(result));
321 
322  // Exit:
323 
324  return result;
325 }
326 
331 {
332 
333  // Preconditions:
334 
335  require(is_ancestor_of(&xother));
336 
337  // Body:
338 
339  not_implemented();
340 
341  // Postconditions:
342 
343  ensure(invariant());
344 
345  // Exit:
346 
347  return *this;
348 }
349 
353 {
354 
355  // Preconditions:
356 
357 
358  // Body:
359 
360  not_implemented();
361 
362  // Postconditions:
363 
364  ensure(invariant());
365 
366  // Exit
367 
368  return *this;
369 }
370 
371 bool
373 invariant() const
374 {
375  bool result = true;
376 
377  if(invariant_check())
378  {
379  // Prevent recursive calls to invariant
380 
382 
383  // Must satisfy base class invariant
384 
385  invariance(any::invariant());
386 
387  // Invariances for this class:
388 
389  // Finished, turn invariant checking back on.
390 
392  }
393 
394  // Exit
395 
396  return result;
397 }
398 
399 
400 // ===========================================================
401 // NON-MEMBER FUNCTIONS
402 // ===========================================================
403 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
static factory< field_refinement_policy > & policy_factory()
A factory for making policy objects.
size_type _refinement_depth_ub
The upper bound and the refinement level of a cell.
virtual bool should_refine(field_refinement_buffer &xbuffer, size_type xrefinement_depth) const
True if the zone specified by xzone_id should be refined.
A buffer for data which is used by both a local_field_refiner object and its associated field_refinem...
Namespace for fields component of sheaf system.
virtual zone_centered_error_bound_refinement_policy & operator=(const field_refinement_policy &xother)
Assignment operator.
virtual zone_centered_error_bound_refinement_policy * clone() const
Virtual constructor, makes a new instance of the same type as this.
size_type refinement_depth_ub() const
The upper bound on the refinement depth of a cell. A cell will not be refined if its refinement depth...
STL namespace.
Abstract base class with useful features for all objects.
Definition: any.h:39
scoped_index zone_id
The id of the current zone.
unsigned long size_type
An unsigned integral type used to represent sizes and capacities.
Definition: sheaf.h:52
An abstract policy that determines the conditions under which a zone should be refined.
sec_vd_value_type error_ub() const
The upper bound on the error at the center of a cell.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
field_vd & target
The field being refined.
block< sec_vd_value_type > prop_value
A buffer for computing target property at a point.
block< sec_vd_value_type > source_prop_value
A buffer for computing source property at a point.
A policy that determines a zone should be refined if the error with respect to the source field at th...
bool same_evaluation() const
True if the coordinates and property sections both use the same evaluation subposet.
Definition: field_vd.cc:822
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
void evaluate_at_center()
Evaluates the property, the coordinates, and the source property at the center of the zone...
const field_vd & source
The reference field.
bool state_is_read_accessible() const
True if this is attached and if the coordinates and property are accessible for read or access contro...
Definition: field_vd.cc:1380
int dp() const
The dimension of the property (dependent variable) space.
Definition: field_vd.cc:278
static const std::string & static_class_name()
The class name of this class.
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
vd_value_type sec_vd_value_type
The type of component in the value of a section at a point.
Definition: fiber_bundle.h:73
bool is_same_type(const any *other) const
True if other is the same type as this.
Definition: any.cc:79
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
virtual const std::string & class_name() const
The class name of this.
zone_centered_error_bound_refinement_policy(size_type xrefinement_depth_ub=2, sec_vd_value_type xerror_ub=0.01)
Creates an instance with refinement bound xrefinement_ub and error bound xerror_ub.