MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DataElement.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2009 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include "header.h"
11 #include "FuncOrder.h"
12 
13 DataElement::DataElement( Id id, const Cinfo* c, const string& name,
14  unsigned int numData )
15  :
16  Element( id, c, name )
17 {
18  data_ = c->dinfo()->allocData( numData );
19  numLocalData_ = numData;
20  size_ = cinfo()->dinfo()->sizeIncrement();
21  c->postCreationFunc( id, this );
22 }
23 
24 /*
25  * Used for copies. Note that it does NOT call the postCreation Func,
26  * so FieldElements are copied rather than created by the Cinfo when
27  * the parent element is created. This allows the copied FieldElements to
28  * retain info from the originals.
29  * Note that n is the number of DataEntries made. This may be _less_ than
30  * the starting number of entries. If you want to copy an element tree
31  * then you have to pre-multiply n by the original size.
32  * startEntry is the starting index. It is expected that the subset used
33  * will be contiguous from this index.
34  */
36  unsigned int n, unsigned int startEntry )
37  :
38  Element( id, orig->cinfo(), orig->getName() )
39 {
40  numLocalData_ = n;
41  size_ = cinfo()->dinfo()->sizeIncrement();
42  data_ = cinfo()->dinfo()->copyData( orig->data( 0 ), orig->numData(),
43  numLocalData_, startEntry );
44  // cinfo_->postCreationFunc( id, this );
45 }
46 
47 // Virtual destructor.
49 {
50  // cout << "deleting element " << getName() << endl;
51  cinfo()->dinfo()->destroyData( data_ );
52  data_ = 0;
53  // The base class destroys the messages.
54 }
55 
57 // DataElement info functions
59 
60 // virtual func.
61 unsigned int DataElement::numLocalData() const
62 {
63  return numLocalData_;
64 }
65 
66 // virtual func.
67 unsigned int DataElement::numField( unsigned int entry ) const
68 {
69  return 1;
70 }
71 
72 // virtual func.
73 unsigned int DataElement::totNumLocalField() const
74 {
75  return numLocalData_;
76 }
77 
79 // Data access functions.
81 // virtual func, overridden.
82 char* DataElement::data( unsigned int rawIndex, unsigned int fieldIndex ) const
83 {
84  assert( rawIndex < numLocalData_ );
85  // return data_ + ( rawIndex * cinfo()->dinfo()->size() );
86  return data_ + rawIndex * size_;
87 }
88 
95 void DataElement::resize( unsigned int newNumLocalData )
96 {
97  numLocalData_ = newNumLocalData;
98  char* temp = data_;
99  data_ = cinfo()->dinfo()->copyData(
100  temp, numLocalData_, newNumLocalData, 0 );
101  cinfo()->dinfo()->destroyData( temp );
102  numLocalData_ = newNumLocalData;
103 }
104 
106 // Zombie stuff
108 
109 void DataElement::zombieSwap( const Cinfo* zCinfo )
110 {
111  cinfo()->dinfo()->destroyData( data_ );
112  data_ = zCinfo->dinfo()->allocData( numLocalData_ );
113  replaceCinfo( zCinfo );
114  size_ = zCinfo->dinfo()->sizeIncrement();
115  Element::zombieSwap( zCinfo ); // Handles clock tick reassignment.
116 }
void zombieSwap(const Cinfo *newCinfo)
Virtual func.
unsigned int numField(unsigned int rawIndex) const
Define only in derived classes: getNode( unsigned int dataIndex ) const;.
Definition: DataElement.cpp:67
char * data_
Definition: DataElement.h:136
virtual void zombieSwap(const Cinfo *zCinfo)
virtual func, this base version must be called by all derived classes
Definition: Element.cpp:159
virtual void destroyData(char *d) const =0
unsigned int size_
Definition: DataElement.h:148
unsigned int totNumLocalField() const
Definition: DataElement.cpp:73
virtual char * copyData(const char *orig, unsigned int origEntries, unsigned int copyEntries, unsigned int startEntry) const =0
char * data(unsigned int rawIndex, unsigned int fieldIndex=0) const
Definition: DataElement.cpp:82
const DinfoBase * dinfo() const
Definition: Cinfo.cpp:275
void resize(unsigned int newNumData)
Definition: DataElement.cpp:95
virtual unsigned int sizeIncrement() const =0
void postCreationFunc(Id newId, Element *newElm) const
Definition: Cinfo.cpp:143
const Cinfo * cinfo() const
Definition: Element.cpp:66
virtual unsigned int numData() const =0
Returns number of data entries across all nodes.
unsigned int numLocalData_
Definition: DataElement.h:141
static char name[]
Definition: mfield.cpp:401
virtual char * data(unsigned int rawIndex, unsigned int fieldIndex=0) const =0
virtual char * allocData(unsigned int numData) const =0
Definition: Id.h:17
unsigned int numLocalData() const
Defined only in derived classes: unsigned int numData() const;.
Definition: DataElement.cpp:61
void replaceCinfo(const Cinfo *newCinfo)
Support function for zombieSwap, replaces Cinfo.
Definition: Element.cpp:740
DataElement(Id id, const Cinfo *c, const string &name, unsigned int numData=1)
Definition: Cinfo.h:18