SheafSystem  0.0.0.0
body_factory.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/body_factory.h"
22 
23 #include "SheafSystem/arg_list.h"
24 #include "SheafSystem/assert_contract.h"
25 #include "SheafSystem/array_index_space_state.h"
26 #include "SheafSystem/base_space_member.h"
27 #include "SheafSystem/block.impl.h"
28 #include "SheafSystem/body_builder.h"
29 #include "SheafSystem/fiber_bundles_namespace.h"
30 #include "SheafSystem/field_factory.h"
31 #include "SheafSystem/std_cmath.h"
32 #include "SheafSystem/std_iostream.h"
33 #include "SheafSystem/std_sstream.h"
34 #include "SheafSystem/field_vd.h"
35 #include "SheafSystem/wsv_block.impl.h"
36 
37 using namespace std;
38 using namespace fields; // Workaround for MS C++ bug.
39 
40 //#define DIAGNOSTIC_OUTPUT
41 
42 // =============================================================================
43 // BODY_FACTORY FACET
44 // =============================================================================
45 
46 // PUBLIC MEMBER FUNCTIONS
47 
51  const std::string& xname_prefix)
52 {
53  // Preconditions:
54 
55  require(xns.state_is_read_write_accessible());
56  require(!xns.contains_poset(xname_prefix+"_1d_unstructured_base_space"));
57 
58  // Body:
59 
60  // Create a 1D unstructured mesh and a scalar field
61  // that initially defines one circular body.
62 
63  wsv_block<sec_vd_value_type> llower("-1");
64  wsv_block<sec_vd_value_type> lupper("1");
65 
66  field_vd* result =
67  field_factory::new_scalar_field_1d_unstructured(xns,
68  xname_prefix+"_1d_unstructured",
69  10,
70  llower,
71  lupper,
72  one_body);
73 
74  // Build the circular body and two square bodies and output results.
75 
76  build_bodies(*result);
77 
78  // Postconditions:
79 
80  ensure(xns.contains_poset_member(xname_prefix+"_1d_unstructured_base_space/circular_body0"));
81  ensure(xns.contains_poset_member(xname_prefix+"_1d_unstructured_base_space/square_body0"));
82  ensure(xns.contains_poset_member(xname_prefix+"_1d_unstructured_base_space/square_body1"));
83 
84  // Exit:
85 
86  return result;
87 }
88 
92  const std::string& xname_prefix)
93 {
94  // Preconditions:
95 
96  require(xns.state_is_read_write_accessible());
97  require(!xns.contains_poset(xname_prefix+"_1d_uniform_base_space"));
98 
99  // Body:
100 
101  // Create a 1D uniform mesh and a scalar field that
102  // initially defines one circular body.
103 
104  wsv_block<sec_vd_value_type> llower("-1");
105  wsv_block<sec_vd_value_type> lupper("1");
106 
107  field_vd* result =
108  field_factory::new_scalar_field_1d_uniform(xns,
109  xname_prefix+"_1d_uniform",
110  10,
111  llower,
112  lupper,
113  one_body);
114 
115  // Build the circular body and two square bodies and output results.
116 
117  build_bodies(*result);
118 
119  // Postconditions:
120 
121  ensure(xns.contains_poset_member(xname_prefix+"_1d_uniform_base_space/circular_body0"));
122  ensure(xns.contains_poset_member(xname_prefix+"_1d_uniform_base_space/square_body0"));
123  ensure(xns.contains_poset_member(xname_prefix+"_1d_uniform_base_space/square_body1"));
124 
125  // Exit:
126 
127  return result;
128 }
129 
130 
134  const std::string& xname_prefix)
135 {
136  // Preconditions:
137 
138  require(xns.state_is_read_write_accessible());
139  require(!xns.contains_poset(xname_prefix+"_2d_unstructured_base_space"));
140 
141  // Body:
142 
143  // Create a 2D unstructured mesh and a scalar field
144  // that initially defines one circular body.
145 
146  wsv_block<sec_vd_value_type> llower("-1 -1");
147  wsv_block<sec_vd_value_type> lupper("1 1");
148 
149  field_vd* result =
150  field_factory::new_scalar_field_2d_unstructured(xns,
151  xname_prefix+"_2d_unstructured",
152  10,
153  10,
154  true,
155  llower,
156  lupper,
157  one_body);
158 
159  // Build the circular body and two square bodies and output results.
160 
161  build_bodies(*result);
162 
163  // Postconditions:
164 
165  ensure(xns.contains_poset_member(xname_prefix+"_2d_unstructured_base_space/circular_body0"));
166  ensure(xns.contains_poset_member(xname_prefix+"_2d_unstructured_base_space/square_body0"));
167  ensure(xns.contains_poset_member(xname_prefix+"_2d_unstructured_base_space/square_body1"));
168 
169  // Exit:
170 
171  return result;
172 }
173 
177  const std::string& xname_prefix)
178 {
179  // Preconditions:
180 
181  require(xns.state_is_read_write_accessible());
182  require(!xns.contains_poset(xname_prefix+"_2d_uniform_base_space"));
183 
184  // Body:
185 
186  // Create a 2D uniform mesh and a scalar field that
187  // initially defines one circular body.
188 
189  wsv_block<sec_vd_value_type> llower("-1 -1");
190  wsv_block<sec_vd_value_type> lupper("1 1");
191 
192  field_vd* result =
193  field_factory::new_scalar_field_2d_uniform(xns,
194  xname_prefix+"_2d_uniform",
195  10,
196  10,
197  llower,
198  lupper,
199  one_body);
200 
201  // Build the circular body and two square bodies and output results.
202 
203  build_bodies(*result);
204 
205  // Postconditions:
206 
207  ensure(xns.contains_poset_member(xname_prefix+"_2d_uniform_base_space/circular_body0"));
208  ensure(xns.contains_poset_member(xname_prefix+"_2d_uniform_base_space/square_body0"));
209  ensure(xns.contains_poset_member(xname_prefix+"_2d_uniform_base_space/square_body1"));
210 
211  // Exit:
212 
213  return result;
214 }
215 
219  const std::string& xname_prefix)
220 {
221  // Preconditions:
222 
223  require(xns.state_is_read_write_accessible());
224  require(!xns.contains_poset(xname_prefix+"_3d_unstructured_base_space"));
225 
226  // Body:
227 
228  // Create a 3D unstructured mesh and a scalar field
229  // that initially defines one circular body.
230 
231  wsv_block<sec_vd_value_type> llower("-1 -1 -1");
232  wsv_block<sec_vd_value_type> lupper("1 1 1");
233 
234  field_vd* result =
235  field_factory::new_scalar_field_3d_unstructured(xns,
236  xname_prefix+"_3d_unstructured",
237  10,
238  10,
239  10,
240  true,
241  llower,
242  lupper,
243  one_body);
244 
245  // Build the circular body and two square bodies and output results.
246 
247  build_bodies(*result);
248 
249  // Postconditions:
250 
251  ensure(xns.contains_poset_member(xname_prefix+"_3d_unstructured_base_space/circular_body0"));
252  ensure(xns.contains_poset_member(xname_prefix+"_3d_unstructured_base_space/square_body0"));
253  ensure(xns.contains_poset_member(xname_prefix+"_3d_unstructured_base_space/square_body1"));
254 
255  // Exit:
256 
257  return result;
258 }
259 
263  const std::string& xname_prefix)
264 {
265  // Preconditions:
266 
267  require(xns.state_is_read_write_accessible());
268  require(!xns.contains_poset(xname_prefix+"_3d_uniform_base_space"));
269 
270  // Body:
271 
272  // Create a 3D uniform mesh and a scalar field that
273  // initially defines one circular body.
274 
275  wsv_block<sec_vd_value_type> llower("-1 -1 -1");
276  wsv_block<sec_vd_value_type> lupper("1 1 1");
277 
278  field_vd* result =
279  field_factory::new_scalar_field_3d_uniform(xns,
280  xname_prefix+"_3d_uniform",
281  10,
282  10,
283  10,
284  llower,
285  lupper,
286  one_body);
287 
288  // Build the circular body and two square bodies and output results.
289 
290  build_bodies(*result);
291 
292  // Postconditions:
293 
294  ensure(xns.contains_poset_member(xname_prefix+"_3d_uniform_base_space/circular_body0"));
295  ensure(xns.contains_poset_member(xname_prefix+"_3d_uniform_base_space/square_body0"));
296  ensure(xns.contains_poset_member(xname_prefix+"_3d_uniform_base_space/square_body1"));
297 
298  // Exit:
299 
300  return result;
301 }
302 
303 
304 // PRIVATE MEMBER FUNCTIONS
305 
306 void
307 fields::body_factory::
308 one_body(block<sec_vd_value_type>& xglobal_coords,
309  block<sec_vd_dof_type>& xproperty_value)
310 {
311  // Preconditions:
312 
313  require(xproperty_value.ct() == 1);
314 
315  // Body:
316 
317  sec_vd_value_type& lprop_val0 = xproperty_value[0];
318 
319  lprop_val0 = 0.0;
320  for(int i= 0; i<xglobal_coords.ct(); ++i)
321  {
322  lprop_val0 += xglobal_coords[i]*xglobal_coords[i];
323  }
324 
325  lprop_val0 = sqrt(lprop_val0);
326 
327  // Postconditions:
328 
329  ensure(xproperty_value[0] >= 0.0);
330 
331  // Exit:
332 
333  return;
334 };
335 
336 void
337 fields::body_factory::
338 two_bodies(block<sec_vd_value_type>& xglobal_coords,
339  block<sec_vd_dof_type>& xproperty_value)
340 {
341  // Preconditions:
342 
343  require(xglobal_coords.ct() > 0);
344 
345  require(xproperty_value.ct() == 1);
346 
347  // Body:
348 
349  int ldc = xglobal_coords.ct();
350  bool linside = (fabs(xglobal_coords[0]) >= .5);
351  if(linside && (ldc > 1))
352  {
353  linside = (fabs(xglobal_coords[1]) <= 0.5);
354 
355  if(linside && (ldc > 2))
356  {
357  linside = (fabs(xglobal_coords[2]) <= 0.5);
358  }
359  }
360 
361  if(linside)
362  {
363  xproperty_value[0] = 1.0;
364  }
365  else
366  {
367  xproperty_value[0] = 0.0;
368  }
369 
370  // Postconditions:
371 
372  // Exit:
373 
374  return;
375 }
376 
377 void
378 fields::body_factory::
379 build_circular_body(field_vd& xfield)
380 {
381 #ifdef DIAGNOSTIC_OUTPUT
382  cout << "entering body_factory::build_circular_body:" << endl;
383 #endif
384 
385  poset* lbase_space_host = xfield.base_space().host();
386  lbase_space_host->get_read_write_access();
387  xfield.get_read_access();
388 
389  subposet lbodies(lbase_space_host, "bodies");
391 
392  body_builder lbody_builder;
393  block<scoped_index>& lcircular_bodies =
394  *lbody_builder.build(xfield, -0.1, 0.5);
395 
396  for(int i=0; i<lcircular_bodies.ct(); ++i)
397  {
398  stringstream lstr;
399  lstr << "circular_body" << i;
400 
401  scoped_index lbody_id = lcircular_bodies[i];
402 
403  lbase_space_host->put_member_name(lbody_id, lstr.str(), true, false);
404 
405  lbodies.insert_member(lbody_id);
406  lspace.push_back(lbody_id);
407  }
408 
409 #ifdef DIAGNOSTIC_OUTPUT
410  cout << endl << "circular bodies (should be exactly one):";
411  for(int i=0; i<lcircular_bodies.ct(); ++i)
412  {
413  cout << " " << lcircular_bodies[i];
414  }
415  cout << endl;
416 #endif
417 
418  // Clean up.
419 
420  lbodies.detach_from_state();
421  xfield.release_access();
422  lbase_space_host->release_access();
423 
424  delete &lcircular_bodies;
425 
426 #ifdef DIAGNOSTIC_OUTPUT
427  cout << "leaving body_factory::build_circular_body:" << endl;
428 #endif
429  return;
430 };
431 
432 
433 void
434 fields::body_factory::
435 build_square_bodies(field_vd& xfield)
436 {
437 #ifdef DIAGNOSTIC_OUTPUT
438  cout << "entering body_factory::build_square_bodies:" << endl;
439 #endif
440 
441  poset* lbase_space_host = xfield.base_space().host();
442  lbase_space_host->get_read_write_access();
443  xfield.get_read_access();
444 
445  subposet lbodies(lbase_space_host, "bodies");
447 
448  body_builder lbody_builder;
449  block<scoped_index>& lsquare_bodies =
450  *lbody_builder.build(xfield, 0.9, 1.1);
451 
452  for(int i=0; i<lsquare_bodies.ct(); ++i)
453  {
454  stringstream lstr;
455  lstr << "square_body" << i;
456 
457  scoped_index lbody_id = lsquare_bodies[i];
458 
459  lbase_space_host->put_member_name(lbody_id, lstr.str(), true, false);
460 
461  lbodies.insert_member(lbody_id);
462  lspace.push_back(lbody_id);
463  }
464 
465 #ifdef DIAGNOSTIC_OUTPUT
466  cout << endl << "square bodies (should be exactly two):";
467  for(int i=0; i<lsquare_bodies.ct(); ++i)
468  {
469  cout << " " << lsquare_bodies[i];
470  }
471  cout << endl;
472 #endif
473 
474  // Clean up.
475 
476  lbodies.detach_from_state();
477  xfield.release_access();
478  lbase_space_host->release_access();
479 
480  delete &lsquare_bodies;
481 
482 #ifdef DIAGNOSTIC_OUTPUT
483  cout << "leaving body_factory::build_square_bodies:" << endl;
484 #endif
485 
486  return;
487 };
488 
489 void
490 fields::body_factory::
491 build_bodies(field_vd& xfield)
492 {
493  // Create a bodies subposet with a sequence id map.
494 
495  poset* lbase_space_host = xfield.base_space().host();
496  lbase_space_host->get_read_write_access(false);
497  subposet lbodies(lbase_space_host, 0, false);
498  lbodies.put_name("bodies", true, false);
499  lbodies.new_id_space("array_index_space_state");
500 
501  // Build the circular body and insert in bodies subposet.
502 
503  build_circular_body(xfield);
504 
505  // Construct a scalar field that defines two square bodies.
506 
507  xfield.put_property_dofs(two_bodies, true);
508 
509  // Build the square bodies and insert in bodies subposet.
510 
511  build_square_bodies(xfield);
512 
513  // Clean up.
514 
515  lbodies.detach_from_state();
516  lbase_space_host->release_access();
517 
518  // Exit.
519 
520  return;
521 };
virtual void get_read_write_access(bool xrelease_read_only_access=false)
Get read write access to the state associated with this. If release_read_only_access is requested...
SHEAF_DLL_SPEC void sqrt(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute sqrt of x0 (sqrt(x0)) (pre-allocated version).
Definition: sec_at0.cc:1556
A client handle for a subposet.
Definition: subposet.h:86
size_type ct() const
The number of items currently in use.
Namespace for fields component of sheaf system.
The standard fiber bundles name space; extends the standard sheaves namespace by defining base space...
virtual void get_read_access() const
Get read access to the state associated with this.
Definition: field_vd.cc:1516
bool contains_poset_member(pod_index_type xposet_hub_id, pod_index_type xmember_hub_id, bool xauto_access=true) const
True if this contains a poset with hub id xposet_hub_id which contains a member with hub id xmember_h...
STL namespace.
static field_vd * new_2d_uniform_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 2D uniform mesh.
virtual void put_member_name(pod_index_type xmbr_hub_id, const std::string &xname, bool xunique, bool xauto_access=false)
Make xname a name for the member with hub id xmbr_hub_id. if xunique, make xname the only name...
virtual void put_name(const std::string &xname, bool xunique, bool xauto_access)
Make xname a name for this; if xunique, make xname the only name.
Definition: subposet.cc:2782
static field_vd * new_3d_unstructured_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 3D unstructured mesh.
A client handle for a mutable partially ordered set.
Definition: poset.h:40
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...
poset * host() const
The poset which this is a handle to a member of.
virtual scattered_insertion_index_space_handle & new_id_space(const std::string &xstate_class_name)
Creates an id space for the members of this.
Definition: subposet.cc:554
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
virtual void insert_member(pod_index_type xmbr_hub_id)
Inserts the member of host() with hub id xmbr_hub_id.
Definition: subposet.cc:1091
A vd-valued property as a function of global coordinates.
Definition: field_vd.h:69
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
static field_vd * new_3d_uniform_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 3D uniform mesh.
SHEAF_DLL_SPEC void fabs(const sec_at0 &x0, sec_at0 &xresult, bool xauto_access)
Compute fabs of x0 (fabs(x0)) (pre-allocated version).
Definition: sec_at0.cc:1331
static field_vd * new_2d_unstructured_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 2D unstructured mesh.
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
bool contains_poset(pod_index_type xhub_id, bool xauto_access=true) const
True if this contains a poset with hub id xhub_id..
const scattered_insertion_index_space_handle & id_space() const
The id space for the members of with this (const version).
Definition: subposet.cc:508
virtual void detach_from_state()
Detach this handle from its state, if any.
void put_property_dofs(property_dof_function_type xproperty_dofs_fcn, bool xauto_access)
Sets the dofs of property() to values computed by xproperty_dofs_fcn.
Definition: field_vd.cc:454
static field_vd * new_1d_uniform_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 1D uniform mesh.
Definition: body_factory.cc:91
A handle for a scattered_insertion_index_space_state.
void push_back(const scoped_index &xhub_id)
Make the next id in this space equivalent to xhub_id in the hub id space. synonym for push_back(xhub_...
block< scoped_index > * build(field_vd &xfield, value_type xlower, value_type xupper)
Builds the bodies defined by threshholds xlower and xupper in field xfield; auto_allocated.
Definition: body_builder.cc:86
static field_vd * new_1d_unstructured_bodies(fiber_bundles_namespace &xns, const std::string &xname_prefix)
Uses body_builder to create bodies in a 1D unstructured mesh.
Definition: body_factory.cc:50
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
A simple body builder; creates base space members equivalent to subsets defined by a field classifica...
Definition: body_builder.h:58