SheafSystem  0.0.0.0
field_refiner.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 
18 //
21 // A map from one sec_rep_space to another.
22 //
23 
24 #include "SheafSystem/field_refiner.h"
25 
26 #include "SheafSystem/assert_contract.h"
27 #include "SheafSystem/base_space_member.h"
28 #include "SheafSystem/base_space_poset.h"
29 #include "SheafSystem/depth_bound_refinement_policy.h"
30 #include "SheafSystem/error_message.h"
31 #include "SheafSystem/factory.h"
32 #include "SheafSystem/field_eval_iterator.h"
33 #include "SheafSystem/field_refinement_buffer.h"
34 #include "SheafSystem/field_refinement_policy.h"
35 #include "SheafSystem/local_field_refiner.h"
36 #include "SheafSystem/field_refiner_family.h"
37 #include "SheafSystem/section_evaluator.h"
38 #include "SheafSystem/sec_ed_invertible.h"
39 #include "SheafSystem/sec_vd.h"
40 #include "SheafSystem/variance_bound_refinement_policy.h"
41 #include "SheafSystem/field_vd.h"
42 #include "SheafSystem/zone_centered_error_bound_refinement_policy.h"
43 
44 using namespace std;
45 using namespace fields; // Workaround for MS C++ bug.
46 
47 // #define DIAGNOSTIC_OUTPUT 1
48 
49 // ===========================================================
50 // ANY FACET
51 // ===========================================================
52 
53 // PUBLIC MEMBER FUNCTIONS
54 
57  : _refiners(0)
58 {
59  // Preconditions:
60 
61  // Body:
62 
63  not_implemented();
64 
65  // Postconditions:
66 
67  // Exit:
68 
69  return;
70 }
71 
74 {
75  // Preconditions:
76 
77  // Body:
78 
79  not_implemented();
80 
81  // Postconditions:
82 
83  ensure(invariant());
84 
85  // Exit:
86 }
87 
90 {
91  // Preconditions:
92 
93  // Body:
94 
95  // Postconditions:
96 
97  // Exit:
98 }
99 
100 
101 bool
103 invariant() const
104 {
105  bool result = true;
106 
107  // Preconditions:
108 
109  // Body:
110 
111  invariance(any::invariant());
112 
113  if(invariant_check())
114  {
116 
117  // Finished, turn invariant checking back on.
118 
120  }
121 
122  // Postconditions:
123 
124  // Exit
125 
126  return result;
127 }
128 
129 
130 // ===========================================================
131 // FIELD_REFINER FACET
132 // ===========================================================
133 
134 // PUBLIC MEMBER FUNCTIONS
135 
137 field_refiner(const poset& xbase_space,
138  const std::string& xrefiner_family_name,
139  const std::string& xrefinement_policy_name,
140  bool xauto_access)
141 {
142  // Preconditions:
143 
144  require(xauto_access || xbase_space.state_is_read_accessible());
145  require(field_refiner_family::family_factory().contains_prototype(xrefiner_family_name));
146  require(field_refinement_policy::policy_factory().contains_prototype(xrefinement_policy_name));
147 
148  // Body:
149 
150  if(xauto_access)
151  {
152  xbase_space.get_read_access();
153  }
154 
155 
156  _refiners = field_refiner_family::new_family(xrefiner_family_name);
157 
158  field_refinement_policy* lpolicy =
159  field_refinement_policy::new_policy(xrefinement_policy_name);
160 
161 
164 
165  _refiners->initialize(dynamic_cast<const base_space_poset&>(xbase_space),
166  *lpolicy);
167 
168  if(xauto_access)
169  {
170  xbase_space.release_access();
171  }
172 
173  // Postconditions:
174 
175  ensure(invariant());
176  ensure(refiners().is_initialized());
177  ensure(refiners().class_name() == xrefiner_family_name);
178 
179  // Exit:
180 
181  return;
182 }
183 
187 {
188  return *_refiners;
189 }
190 
193 policy() const
194 {
195  // Preconditions:
196 
197  // Body:
198 
199  field_refinement_policy& result = _refiners->policy();
200 
201  // Postconditions:
202 
203 
204  // Exit:
205 
206  return result;
207 }
208 
209 void
211 refine(field_vd& xtarget, bool xauto_access)
212 {
213 
214 #ifdef DIAGNOSTIC_OUTPUT
215  post_information_message("Entering refine(field_vd&, bool)");
216 #endif
217 
218  // Preconditions:
219 
220  require(xauto_access || xtarget.base_space().state_is_read_write_accessible());
221  require(xauto_access || xtarget.state_is_read_write_accessible());
222 
223  define_old_variable(int old_xtarget_access_request_depth =
224  xtarget.access_request_depth());
225 
226 
227  if(xauto_access)
228  {
229  xtarget.get_read_write_access(true, true);
230  }
231 
232  require(xtarget.same_evaluation());
233 
234  // Body:
235 
236  // Put target base space in jim edit mode.
237 
238  xtarget.base_space().host()->begin_jim_edit_mode(false);
239 
240  // Create an evaluation iterator for the target.
241 
242  field_eval_iterator litr(xtarget);
243 
244  // Initialize the buffer shared by this, the refiner and the refinement policy;
245  // uses discretization buffers from iterator.
246 
247  field_refinement_buffer lbuffer(xtarget,
251 
252  // Traverse the target; refining as needed.
253 
254  while(!litr.is_done())
255  {
256  // Get the refiner to match the cell type.
257 
258  local_field_refiner* lrefiner = _refiners->member(litr.type_id());
259 
260  assertion(lrefiner != 0);
261 
262  int ldepth = litr.refinement_depth();
263 
264  // Initialize some members in the refinement buffer.
265 
266  lbuffer.zone_id = litr.index();
267  lbuffer.coord_evaluator = &litr.coordinate_evaluator();
268  lbuffer.prop_evaluator = &litr.property_evaluator();
269  lbuffer.depth = ldepth;
270 
271  // Gather the coordinate dofs for this zone.
272 
273  lbuffer.gather_coordinate_dofs();
274 
275  if(ldepth > 0)
276  {
277  // Evaluate any property dofs for this zone
278  // that have not been previously evaluated.
279 
280  lbuffer.refine_prop_dofs(lrefiner);
281  }
282 
283  // Gather the property dofs for this zone.
284 
285  lbuffer.gather_property_dofs();
286 
287  // Test the refinement policy.
288 
289  if(lrefiner->policy().should_refine(lbuffer, ldepth))
290  {
291  // Policy says refine; do it.
292 
293  lrefiner->refine(lbuffer);
294 
295  // Save the current zone in the parent buffers.
296 
297  lbuffer.save_parent();
298 
299  // Repeat the traversal of the down set of the current
300  // zone in case we need to further refine the zones we
301  // just added. Arg == true tells iterator we've just
302  // refined this zone.
303 
304  litr.repeat(true);
305  }
306  else
307  {
308  // Just continue to the next zone.
309 
310  litr.next();
311  }
312  }
313 
314  // Finished with jim edit mode.
315 
316  xtarget.base_space().host()->end_jim_edit_mode(true, false);
317 
318  xtarget.coordinates().schema().force_cache_update();
319  xtarget.property().schema().force_cache_update();
320 
321  if(xauto_access)
322  {
323  xtarget.release_access();
324  }
325 
326  // Postconditions:
327 
328  ensure(xtarget.access_request_depth() == old_xtarget_access_request_depth);
329 
330  // Exit
331 
332  return;
333 }
334 
335 void
337 refine(const sec_ed& xcoordinates, const sec_vd& xproperty, bool xauto_access)
338 {
339 
340 #ifdef DIAGNOSTIC_OUTPUT
341  post_information_message("Entering refine(sec_ed&, sec_vd&, bool");
342 #endif
343 
344  // Preconditions:
345 
346  require(precondition_of(refine(field_vd(xcoordinates, xproperty, xauto_access), xauto_access)));
347 
348  // Body:
349 
350  field_vd ltarget(xcoordinates, xproperty, xauto_access);
351 
352  refine(ltarget, xauto_access);
353 
354  // Postconditions:
355 
356  ensure(postcondition_of(refine(field_vd(xcoordinates, xproperty, xauto_access), xauto_access)));
357 
358  // Exit
359 
360  return;
361 }
362 
363 // ===========================================================
364 // NONMEMBER FUNCTIONS
365 // ===========================================================
366 
virtual bool invariant() const
Class invariant, intended to be redefined in each descendant. See below for template for invariant in...
Definition: any.cc:153
virtual void get_read_write_access(bool xbase_access, bool xrelease_read_only_access)
Get read write access to the coordinates and property sections, which implies read access to the base...
Definition: field_vd.cc:1550
static factory< field_refinement_policy > & policy_factory()
A factory for making policy objects.
static field_refiner_family * new_family(const std::string &xname)
Creates an instance of the refiner family associated with name xname.
virtual bool invariant() const
Class invariant.
void repeat(bool xwas_eval_member)
Repeats the iteration over the down set of index(). If index() refers to a zone that has just been re...
void next()
Makes this the next member of the subset.
static factory< field_refiner_family > & family_factory()
A factory for making field_refiner_family objects.
section_evaluator & property_evaluator() const
The property field evaluator associated with the current evaluation member.
static field_refinement_policy * new_policy(const std::string &xname)
Creates an instance of the policy with class name xname.
A family of compatible local_field_refiners, one for each member of some family of cell types; a map ...
bool state_is_read_write_accessible(bool xbase_access=false) const
True if this is attached and if coordinates(), property() and (if xbase access) base_space() are acce...
Definition: field_vd.cc:1428
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.
field_refinement_policy & policy() const
The refinement policy for this family.
bool state_is_read_accessible() const
True if this is attached and if the state is accessible for read or access control is disabled...
pod_index_type type_id() const
The type id of the current member.
An iterator which concurrently traverses the schema of both the coordinates and property sections of ...
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.
size_type refinement_depth() const
The refinement depth; the number of evaluation members the current evaluation member is contained in...
virtual ~field_refiner()
Destructor.
void refine(field_refinement_buffer &xbuffer)
Refines the zone specified by xbuffer.zone_id one level and evaluates the coordinates section on the ...
const block< scoped_index > & property_discretization_members() const
The property discretization members in the downset of the current evaluation member.
const field_refinement_policy & policy() const
The refinement policy for this refiner.
A section of a fiber bundle with a d-dimensional Euclidean vector space fiber.
Definition: sec_ed.h:47
bool state_is_read_write_accessible() const
True if this is attached and if the state is accessible for read and write or access control is disab...
A client handle for a mutable partially ordered set.
Definition: poset.h:40
poset * host() const
The poset which this is a handle to a member of.
local_field_refiner * member(pod_index_type xtype_id) const
The evaluator associated with cell type xtype_id. Note that the result may be void.
virtual bool should_refine(field_refinement_buffer &xbuffer, size_type xrefinement_depth) const =0
True if the zone specified by xzone_id should be refined.
int access_request_depth() const
The number of times access has been requested and granted without being released. ...
Definition: field_vd.cc:1489
section_evaluator & coordinate_evaluator() const
The coordinate field evaluator associated with the current evaluation member.
An abstract policy that determines the conditions under which a zone should be refined.
A vd-valued property as a function of global coordinates.
Definition: field_vd.h:69
sec_vd & property() const
The dependent variable of this field.
Definition: field_vd.cc:357
virtual void release_access(bool xall=false) const
Release access. If xall is true, release all levels of access. Otherwise, release one level of access...
Definition: field_vd.cc:1594
const block< scoped_index > & coordinate_discretization_members() const
The coordinate discretization members in the downset of the current evaluation member.
virtual void initialize(const base_space_poset &xbase_space, const field_refinement_policy &xpolicy)=0
Initializes this to contain members for base space xbase_space and using policy xpolicy.
void disable_invariant_check() const
Disable invariant check. Intended for preventing recursive calls to invariant and for suppressing inv...
Definition: any.h:97
virtual void release_access(bool xall=false) const
Release access. If xall is true, release all levels of access. Otherwise, release one level of access...
base_space_member & base_space() const
The base space of this field.
Definition: field_vd.cc:373
void force_cache_update()
Forces update of the attribute cache.
field_refiner()
Default constructor; disabled.
A section of a fiber bundle with a d-dimensional vector space fiber.
Definition: sec_vd.h:54
bool is_done() const
True if iteration finished.
field_refinement_policy & policy() const
The refinement policy of the refiners.
bool same_evaluation() const
True if the coordinates and property sections both use the same evaluation subposet.
Definition: field_vd.cc:822
field_refiner_family & refiners()
The family of refiners for the range.
An abstract refiner for a field over a local region (primitive cell) in the base space.
bool invariant_check() const
True if invariant checking is enabled.
Definition: any.h:79
virtual section_space_schema_member & schema()
The restricted schema for this (mutable version).
sec_ed_invertible & coordinates() const
The independent variable of this field.
Definition: field_vd.cc:339
virtual void end_jim_edit_mode(bool xensure_lattice_invariant=true, bool xauto_access=true)
Prevent editing of jims and jim order relation.
Definition: poset.cc:253
virtual void get_read_access() const
Get read access to the state associated with this.
Refines a given field and its base space using a given refinement policy and refiner family...
Definition: field_refiner.h:61
void refine(field_vd &xtarget, bool xauto_access)
Refines field xtarget.
void enable_invariant_check() const
Enable invariant checking.
Definition: any.h:87
const scoped_index & index() const
The index of the current member of the iteration.
virtual void begin_jim_edit_mode(bool xauto_access=true)
Allow editing of jims and jim order relation.
Definition: poset.cc:230