SheafSystem  0.0.0.0
variance_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/variance_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 // VARIANCE_BOUND_REFINEMENT_POLICY FACET
34 // ===========================================================
35 
36 // PUBLIC MEMBER FUNCTIONS
37 
40  sec_vd_value_type xvariance_ub)
41  : field_refinement_policy(xrefinement_depth_ub),
42  _variance_ub(xvariance_ub)
43 {
44 
45  // Preconditions:
46 
47 
48  // Body:
49 
50 
51  // Postconditions:
52 
53  ensure(invariant());
54  ensure(refinement_depth_ub() == xrefinement_depth_ub);
55 
56 
57  // Exit:
58 
59  return;
60 }
61 
62 
65  : field_refinement_policy(xother),
67 {
68  // Preconditions:
69 
70 
71  // Body:
72 
73  // Postconditions:
74 
75  ensure(invariant());
76 
77  // Exit:
78 
79  return;
80 }
81 
82 
85 {
86  // Preconditions:
87 
88 
89  // Body:
90 
91  // Nothing to do.
92 
93  // Postconditions:
94 
95  // Exit:
96 
97  return;
98 }
99 
102 variance_ub() const
103 {
104  return _variance_ub;
105 }
106 
107 void
110 {
111  // Preconditions:
112 
113  require(xub > 0.0);
114 
115  // Body:
116 
117  _variance_ub = xub;
118 
119  // Postconditions:
120 
121  ensure(variance_ub() == xub);
122 
123  // Exit:
124 
125  return;
126 }
127 
128 
129 // PRIVATE MEMBER FUNCTIONS
130 
131 bool
132 fields::variance_bound_refinement_policy::
133 _has_prototype = make_prototype();
134 
135 bool
136 fields::variance_bound_refinement_policy::
137 make_prototype()
138 {
139  bool result = true;
140 
141  // Preconditions:
142 
143 
144  // Body:
145 
148 
149  policy_factory().insert_prototype(lproto);
150 
151  // Postconditions:
152 
153  ensure(policy_factory().contains_prototype(static_class_name()));
154 
155  // Exit:
156 
157  return result;
158 }
159 
160 
161 // ===========================================================
162 // FIELD_REFINEMENT_POLICY FACET
163 // ===========================================================
164 
165 // PUBLIC MEMBER FUNCTIONS
166 
167 bool
169 should_refine(field_refinement_buffer& xbuffer, size_type xrefinement_depth) const
170 {
171  bool result = false;
172 
173  // Preconditions:
174 
175  // Body:
176 
177  result = (xrefinement_depth < _refinement_depth_ub);
178 
179 
180 #ifdef DIAGNOSTIC_OUTPUT
181 
182  cout << " zone_id: " << xbuffer.zone_id
183  << " refinement_depth: " << xrefinement_depth;
184 #endif
185 
186  if(result)
187  {
188  // Compute the average of the dofs.
189 
190  size_type ldp = xbuffer.target.dp();
191  size_type ldofs_ct = 0;
192  size_type ldisc_ct = xbuffer.prop_disc_ids.ct();
193  size_type ldofs_ub = ldisc_ct*ldp;
194 
195  xbuffer.prop_value.set_ct(ldp);
196  xbuffer.prop_value.assign(0.0);
197 
198  size_type k = 0;
199  for(size_type i=0; i<ldisc_ct; ++i)
200  {
201  for(size_type j=0; j<ldp; ++j)
202  {
203  assertion(k < ldofs_ub);
204  xbuffer.prop_value[j] += xbuffer.prop_dofs[k++];
205  }
206  }
207 
208  sec_vd_value_type lprop_norm = 0.0;
209 
210  for(size_type j=0; j<ldp; ++j)
211  {
212  xbuffer.prop_value[j] /= ldisc_ct;
213  lprop_norm += abs(xbuffer.prop_value[j]);
214  }
215 
216  // Compute the variance using the L1 norm.
217 
218  sec_vd_value_type lvariance_norm = 0.0;
219 
220  k = 0;
221  for(size_type i=0; i<ldisc_ct; ++i)
222  {
223  for(size_type j=0; j<ldp; ++j)
224  {
225  lvariance_norm += abs(xbuffer.prop_value[j] - xbuffer.prop_dofs[k++]);
226  }
227  }
228 
229  // Check the difference.
231 
232  if(lprop_norm != 0.0)
233  {
234  result = (lvariance_norm > (ldp*lprop_norm*_variance_ub));
235  }
236  else
237  {
238  result = (lvariance_norm > (ldp*_variance_ub));
239  }
240 
241 #ifdef DIAGNOSTIC_OUTPUT
242  cout << " prop: " << xbuffer.prop_value[0]
243  << " prop norm: " << lprop_norm
244  << " variance norm: " << lvariance_norm;
245 #endif
246 
247  }
248 
249 #ifdef DIAGNOSTIC_OUTPUT
250  cout << " result: " << boolalpha << result << noboolalpha << endl;
251 #endif
252 
253  // Postconditions:
254 
255 
256  // Exit:
257 
258  return result;
259 }
260 
261 const std::string&
263 class_name() const
264 {
265 
266  // Preconditions:
267 
268 
269  // Body:
270 
271  const string& result = static_class_name();
272 
273  // Postconditions:
274 
275 
276  // Exit:
277 
278  return result;
279 }
280 
281 const std::string&
284 {
285 
286  // Preconditions:
287 
288 
289  // Body:
290 
291  static const string result("variance_bound_refinement_policy");
292 
293  // Postconditions:
294 
295 
296  // Exit:
297 
298  return result;
299 }
300 
301 
302 // ===========================================================
303 // ANY FACET
304 // ===========================================================
305 
306 // PUBLIC MEMBER FUNCTIONS
307 
308 bool
310 is_ancestor_of(const any* other) const
311 {
312 
313  // Preconditions:
314 
315  require(other != 0);
316 
317  // Body:
318 
319  // True if other conforms to this
320 
321  bool result = dynamic_cast<const variance_bound_refinement_policy*>(other) != 0;
322 
323  // Postconditions:
324 
325  return result;
326 }
327 
330 clone() const
331 {
333 
334  // Preconditions:
335 
336  // Body:
337 
338  result = new variance_bound_refinement_policy(*this);
339 
340  // Postconditions:
341 
342  ensure(result != 0);
343  ensure(is_same_type(result));
344 
345  // Exit:
346 
347  return result;
348 }
349 
354 {
355 
356  // Preconditions:
357 
358  require(is_ancestor_of(&xother));
359 
360  // Body:
361 
362  not_implemented();
363 
364  // Postconditions:
365 
366  ensure(invariant());
367 
368  // Exit:
369 
370  return *this;
371 }
372 
376 {
377 
378  // Preconditions:
379 
380 
381  // Body:
382 
384  _variance_ub = xother._variance_ub;
385 
386  // Postconditions:
387 
388  ensure(invariant());
389 
390  // Exit
391 
392  return *this;
393 }
394 
395 bool
397 invariant() const
398 {
399  bool result = true;
400 
401  if(invariant_check())
402  {
403  // Prevent recursive calls to invariant
404 
406 
407  // Must satisfy base class invariant
408 
409  invariance(any::invariant());
410 
411  // Invariances for this class:
412 
413  // Finished, turn invariant checking back on.
414 
416  }
417 
418  // Exit
419 
420  return result;
421 }
422 
423 
424 // ===========================================================
425 // NON-MEMBER FUNCTIONS
426 // ===========================================================
427 
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.
size_type ct() const
The number of items currently in use.
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.
variance_bound_refinement_policy(size_type xrefinement_level_ub=2, sec_vd_value_type xvariance_ub=1.0e-10)
Creates an instance with refinement bound xrefinement_ub and variance bound xvariance_ub.
const block< scoped_index > & prop_disc_ids
A buffer for gathering the property discretization members for the current zone.
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.
virtual variance_bound_refinement_policy & operator=(const field_refinement_policy &xother)
Assignment operator.
A policy that determines a zone should be refined if the variance using the L1 norm of any discretiza...
Abstract base class with useful features for all objects.
Definition: any.h:39
sec_vd_value_type variance_ub() const
The upper bound on the variance of the dofs of a cell.
static const std::string & static_class_name()
The class name of this class.
void assign(const_reference_type xitem)
Sets the values of all items to xitem.
scoped_index zone_id
The id of the current zone.
void set_ct(size_type xct)
Sets ct() == xct.
virtual variance_bound_refinement_policy * clone() const
Virtual constructor, makes a new instance of the same type as this.
void put_variance_ub(sec_vd_value_type xub)
Sets variance_ub to xub.
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.
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.
virtual bool should_refine(field_refinement_buffer &xbuffer, size_type xrefinement_depth) const
True if the zone specified by xzone_id should be refined.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual bool is_ancestor_of(const any *other) const
Conformance test; true if other conforms to this.
int dp() const
The dimension of the property (dependent variable) space.
Definition: field_vd.cc:278
sec_vd_value_type _variance_ub
The upper bound on the variance.
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
block< sec_vd_dof_type > prop_dofs
A buffer for gathering property dofs.
virtual const std::string & class_name() const
The class name of this.