SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericField.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 #ifndef CORE_DATATYPES_GENERICFIELD_H
32 #define CORE_DATATYPES_GENERICFIELD_H 1
33 
35 #include <Core/Basis/Locate.h>
46 
48 #include <Core/Containers/FData.h>
51 
53 
54 namespace SCIRun {
55 
56 template <class Mesh, class Basis, class FData>
57 class GenericField: public Field
58 {
59 public:
60  /// Typedefs to support the Field concept.
62  typedef typename FData::value_type value_type;
63  typedef Mesh mesh_type;
64  typedef boost::shared_ptr<mesh_type> mesh_handle_type;
65  typedef Basis basis_type;
66  typedef FData fdata_type;
67  typedef boost::shared_ptr<GenericField<Mesh, Basis, FData> > handle_type;
70 
71  /// only Pio should use this constructor
72  GenericField();
73  /// Use this constructor to actually have a field with a mesh
75  GenericField(const GenericField &copy);
76 
77  virtual ~GenericField();
78 
79  /// Clone the field data, but not the mesh.
80  /// Use mesh_detach() first to clone the complete field
81  virtual GenericField<Mesh, Basis, FData> *clone() const;
82 
83  /// Clone everything, field data and mesh.
85 
86  /// Obtain a Handle to the Mesh
87  virtual MeshHandle mesh() const;
88  virtual VMesh* vmesh() const;
89  virtual VField* vfield() const;
90 
91  #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
92  /// Clone the mesh
93  virtual void mesh_detach();
94  #endif
95 
96  /// Get the order of the field data
97  /// -1 = no data
98  /// 0 = constant data per element
99  /// 1 = linear data per element
100  /// >1 = non linear data per element
101  virtual int basis_order() const { return basis_.polynomial_order(); }
102 
103  /// Get the classes on which this function relies:
104  /// Get the basis describing interpolation within an element
105  Basis& get_basis() { return basis_; }
106 
107  /// Get the mesh describing how the elements fit together
108  // const mesh_handle_type &get_typed_mesh() const;
109 
110  /// Persistent I/O.
111  virtual void io(Piostream &stream);
112 
113  /// Tag the constructor of this class and put it in the Pio DataBase
115 
116  /// Tag the constructor of this class and put it in the Field DataBase
118 
119  /// Function to retrieve the name of this field class
120  static const std::string type_name(int n = -1);
121  virtual std::string dynamic_type_name() const { return type_id.type; }
122 
123  /// A different way of tagging a class. Currently two systems are used next
124  /// to each other: type_name and get_type_description. Neither is perfect
125  virtual
127 
128  /// Static functions to instantiate the field from Pio or using CreateField()
129  static Persistent *maker();
130  static FieldHandle field_maker();
132 
133 protected:
134 
135  /// A (generic) mesh.
137  /// Data container.
139  Basis basis_;
140 
142 
145 };
146 
147 
148 template<class FIELD>
149 class VGenericField : public VField {
150  public:
151  VGenericField(FIELD* field, VFData* vfdata)
152  {
153  DEBUG_CONSTRUCTOR("VGenericField")
154 
155  field_ = field;
156 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
157  pm_ = field;
158 #endif
159  vfdata_ = vfdata;
160  mesh_ = field_->mesh().get();
161  vmesh_ = mesh_->vmesh();
162  basis_order_ = field->basis_order();
163  number_of_nodes_ = field->get_basis().number_of_mesh_vertices();
164  number_of_enodes_ = field->get_basis().number_of_vertices() - number_of_nodes_;
165  element_dim_ = field->get_basis().domain_dimension();
166  element_dofs_ = field->get_basis().dofs();
167  data_type_ = find_type_name(static_cast<typename FIELD::value_type*>(0));
168  for (size_t j=0; j<data_type_.size(); j++)
169  if(data_type_[j] == '_') data_type_[j] = ' ';
170 
171  /// Create a fast way of checking scalar/pair/vector/tensor
172  is_scalar_ = false;
173  is_vector_ = false;
174  is_tensor_ = false;
175  is_pair_ = false;
176 
177  if (data_type_.substr(0,6) == "Vector") is_vector_ = true;
178  else if (data_type_.substr(0,6) == "Tensor") is_tensor_ = true;
179  else if (data_type_.substr(0,4) == "Pair") is_pair_ = true;
180  else if (field->basis_order() > -1) is_scalar_ = true;
181  }
182 
183  virtual ~VGenericField()
184  {
185  DEBUG_DESTRUCTOR("VGenericField")
186  if (vfdata_) delete vfdata_;
187  }
188 
189 };
190 
191 // PIO
192 const int GENERICFIELD_VERSION = 3;
193 
194 
195 template <class Mesh, class Basis, class FData>
196 Persistent *
198 {
200 }
201 
202 template <class Mesh, class Basis, class FData>
205 {
206  return boost::make_shared<GenericField<Mesh, Basis, FData>>();
207 }
208 
209 
210 template <class Mesh, class Basis, class FData>
213 {
214  mesh_handle_type mesh_handle = boost::dynamic_pointer_cast<mesh_type>(mesh);
215  if (mesh_handle)
216  return boost::make_shared<GenericField<Mesh, Basis, FData>>(mesh_handle);
217  else
218  return FieldHandle();
219 }
220 
221 
222 template <class Mesh, class Basis, class FData>
224 GenericField<Mesh, Basis, FData>::type_id(type_name(-1), "Field", maker);
225 
226 template <class Mesh, class Basis, class FData>
228 GenericField<Mesh, Basis, FData>::field_id(type_name(-1),field_maker,field_maker_mesh);
229 
230 template <class Mesh, class Basis, class FData>
232 {
233  int version = stream.begin_class(type_name(), GENERICFIELD_VERSION);
234 
235  if (stream.backwards_compat_id())
236  {
237  version = stream.begin_class(type_name(), GENERICFIELD_VERSION);
238  }
239  Field::io(stream);
240 
241  if (stream.error()) return;
242 
243  if (version < 2)
244  mesh_->io(stream);
245  else
246  Pio(stream, mesh_);
247 
248 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
249  mesh_->freeze();
250 #endif
251 
252  if (version >= 3)
253  {
254  basis_.io(stream);
255  }
256 
257  Pio(stream, fdata_);
258 
259 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
260  freeze();
261 #endif
262 
263  if (stream.backwards_compat_id())
264  {
265  stream.end_class();
266  }
267  stream.end_class();
268 
269  // A new mesh is associated with it
270  if (stream.reading())
271  {
272  vfield_->update_mesh_pointer(mesh_.get());
273  }
274 }
275 
276 template <class Mesh, class Basis, class FData>
278  Field(),
279  mesh_(mesh_handle_type(new mesh_type())),
280  fdata_(0),
281  vfield_(0),
282  basis_order_(0),
283  mesh_dimensionality_(-1)
284 {
285  DEBUG_CONSTRUCTOR("GenericField")
286 
288  if (mesh_) mesh_dimensionality_ = mesh_->dimensionality();
289 
290  VFData* vfdata = CreateVFData(fdata_,get_basis().get_nodes(),get_basis().get_derivs());
293 
294 }
295 
296 template <class Mesh, class Basis, class FData>
298  Field(copy),
299  mesh_(copy.mesh_),
300  fdata_(copy.fdata_),
301  basis_(copy.basis_),
302  vfield_(0),
303  basis_order_(copy.basis_order_),
304  mesh_dimensionality_(copy.mesh_dimensionality_)
305 {
306  DEBUG_CONSTRUCTOR("GenericField")
307 
308  VFData* vfdata = CreateVFData(fdata_,get_basis().get_nodes(),get_basis().get_derivs());
309  if (vfdata)
310  {
312  }
313 }
314 
315 
316 template <class Mesh, class Basis, class FData>
318  Field(),
319  mesh_(mesh),
320  fdata_(0),
321  vfield_(0),
322  basis_order_(0),
323  mesh_dimensionality_(-1)
324 {
325  DEBUG_CONSTRUCTOR("GenericField")
326 
328  if (mesh_) mesh_dimensionality_ = mesh_->dimensionality();
329 
330  VFData* vfdata = CreateVFData(fdata_,get_basis().get_nodes(),get_basis().get_derivs());
333 }
334 
335 
336 template <class Mesh, class Basis, class FData>
338 {
339  DEBUG_DESTRUCTOR("GenericField")
340  if (vfield_) delete vfield_;
341 }
342 
343 template <class Mesh, class Basis, class FData>
346 {
347  return new GenericField<Mesh, Basis, FData>(*this);
348 }
349 
350 template <class Mesh, class Basis, class FData>
353 {
354  auto copy = new GenericField<Mesh, Basis, FData>(*this);
355  copy->mesh_.reset(mesh_->clone());
356  copy->vfield_->update_mesh_pointer(copy->mesh_.get());
357  return copy;
358 }
359 
360 template <class Mesh, class Basis, class FData>
363 {
364  return MeshHandle(mesh_);
365 }
366 
367 template <class Mesh, class Basis, class FData>
368 VMesh*
370 {
371  if (mesh_) return mesh_->vmesh();
372  return (0);
373 }
374 
375 template <class Mesh, class Basis, class FData>
376 VField*
378 {
379  return (vfield_);
380 }
381 
382 #ifdef SCIRUN4_CODE_TO_BE_ENABLED_LATER
383 template <class Mesh, class Basis, class FData>
384 void
386 {
387  thaw();
388  mesh_.detach();
389  mesh_->thaw();
390  vfield_->update_mesh_pointer(mesh_.get_rep());
391 }
392 #endif
393 
394 template <class Mesh, class Basis, class FData>
396 {
397  ASSERT((n >= -1) && n <= 3);
398  if (n == -1)
399  {
400  static const std::string name = TypeNameGenerator::make_template_id(type_name(0), type_name(1), type_name(2), type_name(3));
401  return name;
402  }
403  else if (n == 0)
404  {
405  static const std::string nm("GenericField");
406  return nm;
407  }
408  else if (n == 1)
409  {
410  return find_type_name(static_cast<Mesh *>(0));
411  }
412  else if (n == 2)
413  {
414  return find_type_name(static_cast<Basis *>(0));
415  }
416  else
417  {
418  return find_type_name(static_cast<FData *>(0));
419  }
420 }
421 
422 template <class Mesh, class Basis, class FData>
423 const TypeDescription *
425 {
426  static std::string name(type_name(0));
427  static std::string namesp("SCIRun");
428  static std::string path(__FILE__);
429  const TypeDescription *sub1 = SCIRun::get_type_description(static_cast<Mesh*>(0));
430  const TypeDescription *sub2 = SCIRun::get_type_description(static_cast<Basis*>(0));
431  const TypeDescription *sub3 = SCIRun::get_type_description(static_cast<FData*>(0));
432 
433  switch (td) {
434  default:
435  case FULL_TD_E:
436  {
437  static TypeDescription* tdn1 = 0;
438  if (tdn1 == 0) {
440  (*subs)[0] = sub1;
441  (*subs)[1] = sub2;
442  (*subs)[2] = sub3;
443  tdn1 = new TypeDescription(name, subs, path, namesp,
445  }
446  return tdn1;
447  }
448  case FIELD_NAME_ONLY_E:
449  {
450  static TypeDescription* tdn0 = 0;
451  if (tdn0 == 0) {
452  tdn0 = new TypeDescription(name, 0, path, namesp,
454  }
455  return tdn0;
456  }
457  case MESH_TD_E:
458  {
459  return sub1;
460  }
461  case BASIS_TD_E:
462  {
463  return sub2;
464  }
465  case FDATA_TD_E:
466  {
467  return sub3;
468  }
469  };
470 }
471 
472 /// These ended up here, due to the problem with the include order in get_typedescription.
473 /// Once we have dismanteled that system this can go back to VFData
474 template<class T, class MESH>
475 inline VFData* CreateVFData(FData2d<T,MESH>& fdata, std::vector<T>& lfdata, std::vector<std::vector<T> >& hfdata)
476 {
477  return (CreateVFData(static_cast<Array2<T>& >(fdata),lfdata,hfdata));
478 }
479 
480 template<class T, class MESH>
481 inline VFData* CreateVFData(FData3d<T,MESH>& fdata, std::vector<T>& lfdata, std::vector<std::vector<T> >& hfdata)
482 {
483  return (CreateVFData(static_cast<Array3<T>& >(fdata),lfdata,hfdata));
484 }
485 
486 } // end namespace SCIRun
487 
488 
489 
490 
491 
492 
493 #endif // Datatypes_GenericField_h
Definition: GenericField.h:57
SCIRun::index_type index_type
Definition: GenericField.h:68
fdata_type fdata_
Data container.
Definition: GenericField.h:138
boost::shared_ptr< GenericField< Mesh, Basis, FData > > handle_type
Definition: GenericField.h:67
Mesh * mesh_
Definition: VField.h:698
Interface to statically allocated std::vector class.
bool reading() const
Definition: Persistent.h:164
bool backwards_compat_id() const
Definition: Persistent.h:169
int basis_order_
Definition: VField.h:708
bool is_vector_
Definition: VField.h:716
Taken from old LatVolField.h ...
specializations of template&lt;class T&gt; find_type_name() function for build-in and simple types not deri...
int element_dim_
Definition: VField.h:711
static Persistent * maker()
Static functions to instantiate the field from Pio or using CreateField()
Definition: GenericField.h:197
virtual ~VGenericField()
Definition: GenericField.h:183
Basis & get_basis()
Definition: GenericField.h:105
FieldHandle field()
get a handle to the field
Definition: VField.h:92
Definition: Persistent.h:89
Mesh mesh_type
Definition: GenericField.h:63
Definition: FData.h:55
Definition: Persistent.h:187
VMesh * vmesh_
Definition: VField.h:704
FieldHandle mesh()
Definition: BuildTDCSMatrixTests.cc:56
Definition: TypeDescription.h:45
std::vector< const TypeDescription * > td_vec
Definition: TypeDescription.h:56
bool is_pair_
Definition: VField.h:715
Definition: Mesh.h:45
boost::shared_ptr< Mesh > MeshHandle
Definition: DatatypeFwd.h:67
VGenericField(FIELD *field, VFData *vfdata)
Definition: GenericField.h:151
#define ASSERT(condition)
Definition: Assert.h:110
virtual MeshHandle mesh() const =0
virtual int begin_class(const std::string &name, int current_version)
Definition: Persistent.cc:143
Definition: TypeDescription.h:52
bool error() const
Definition: Persistent.h:166
const string find_type_name(float *)
Definition: TypeName.cc:63
bool is_tensor_
Definition: VField.h:717
int element_dofs_
Definition: VField.h:712
Basis basis_
Definition: GenericField.h:139
int mesh_dimensionality_
Definition: GenericField.h:144
Forward declarations of Mesh get_type_description.
const char * name[]
Definition: BoostGraphExampleTests.cc:87
long long size_type
Definition: Types.h:40
static FieldTypeID field_id
Tag the constructor of this class and put it in the Field DataBase.
Definition: GenericField.h:117
Definition: VFData.h:86
std::string data_type_
Definition: VField.h:720
Persistent i/o for STL containers.
std::string type
Definition: Persistent.h:72
virtual MeshHandle mesh() const
Obtain a Handle to the Mesh.
Definition: GenericField.h:362
GenericField()
only Pio should use this constructor
Definition: GenericField.h:277
virtual const TypeDescription * get_type_description(td_info_e td=FULL_TD_E) const
Definition: GenericField.h:424
PropertyManager * pm_
Definition: VField.h:701
virtual void io(Piostream &stream)
Get the mesh describing how the elements fit together.
Definition: GenericField.h:231
Definition: Field.h:41
virtual VMesh * vmesh()
Definition: Mesh.cc:413
void resize_values()
Definition: VField.h:138
Definition: Field.h:71
bool is_scalar_
Definition: VField.h:714
int number_of_enodes_
Definition: VField.h:710
int basis_order_
Definition: GenericField.h:143
static const std::string make_template_id(const std::string &templateName, const std::string &templateParam)
Definition: TypeName.h:62
Definition: GenericField.h:149
static PersistentTypeID type_id
Tag the constructor of this class and put it in the Pio DataBase.
Definition: GenericField.h:114
GenericField< Mesh, Basis, FData > field_type
Typedefs to support the Field concept.
Definition: GenericField.h:61
static FieldHandle field_maker()
Definition: GenericField.h:204
Field * field_
Definition: VField.h:697
FData fdata_type
Definition: GenericField.h:66
Definition: Array2.h:51
VFData * vfdata_
Definition: VField.h:705
static FieldHandle field_maker_mesh(MeshHandle mesh)
Definition: GenericField.h:212
void Pio(Piostream &stream, Array1< T > &array)
Definition: Array1.h:65
Basis basis_type
Definition: GenericField.h:65
virtual VField * vfield() const
Definition: GenericField.h:377
const int GENERICFIELD_VERSION
Definition: GenericField.h:192
virtual void end_class()
Definition: Persistent.cc:178
Definition: FData.h:203
boost::shared_ptr< mesh_type > mesh_handle_type
Definition: GenericField.h:64
virtual void io(Piostream &stream)
Definition: Field.cc:60
SCIRun::size_type size_type
Definition: GenericField.h:69
long long index_type
Definition: Types.h:39
virtual int basis_order() const
Definition: GenericField.h:101
VFData * CreateVFData(FData2d< T, MESH > &fdata, std::vector< T > &lfdata, std::vector< std::vector< T > > &hfdata)
Definition: GenericField.h:475
Symmetric, positive definite tensors (diffusion, conductivity)
boost::shared_ptr< Field > FieldHandle
Definition: DatatypeFwd.h:65
Definition: Field.h:87
Definition: Persistent.h:64
Definition: VField.h:46
virtual GenericField< Mesh, Basis, FData > * clone() const
Definition: GenericField.h:345
virtual ~GenericField()
Definition: GenericField.h:337
FData::value_type value_type
Definition: GenericField.h:62
int n
Definition: eab.py:9
virtual std::string dynamic_type_name() const
Definition: GenericField.h:121
#define DEBUG_CONSTRUCTOR(type)
Definition: Debug.h:64
VField * vfield_
Definition: GenericField.h:141
Definition: Array3.h:64
int number_of_nodes_
Definition: VField.h:709
Definition: VMesh.h:53
virtual VMesh * vmesh() const
Definition: GenericField.h:369
virtual std::string type_name() const
Definition: Field.cc:116
mesh_handle_type mesh_
A (generic) mesh.
Definition: GenericField.h:136
td_info_e
Type Description to retrieve information on the actual type of the field.
Definition: Field.h:70
#define DEBUG_DESTRUCTOR(type)
Definition: Debug.h:65
const TypeDescription * get_type_description(Core::Basis::ConstantBasis< T > *)
Definition: Constant.h:209
virtual GenericField< Mesh, Basis, FData > * deep_clone() const
Clone everything, field data and mesh.
Definition: GenericField.h:352