1 /*******************************************************************/
2 /*                               XDMF                              */
3 /*                   eXtensible Data Model and Format              */
4 /*                                                                 */
5 /*  Id : Id  */
6 /*  Date : $Date$ */
7 /*  Version : $Revision$ */
8 /*                                                                 */
9 /*  Author:                                                        */
10 /*     Jerry A. Clarke                                             */
11 /*     clarke@arl.army.mil                                         */
12 /*     US Army Research Laboratory                                 */
13 /*     Aberdeen Proving Ground, MD                                 */
14 /*                                                                 */
15 /*     Copyright @ 2002 US Army Research Laboratory                */
16 /*     All Rights Reserved                                         */
17 /*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
18 /*                                                                 */
19 /*     This software is distributed WITHOUT ANY WARRANTY; without  */
20 /*     even the implied warranty of MERCHANTABILITY or FITNESS     */
21 /*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
22 /*     for more information.                                       */
23 /*                                                                 */
24 /*******************************************************************/
25 #include "XdmfAttribute.h"
26 
27 #include "XdmfDataItem.h"
28 #include "XdmfDataDesc.h"
29 #include "XdmfArray.h"
30 #include "XdmfDOM.h"
31 #include "XdmfInformation.h"
32 
33 namespace xdmf2
34 {
35 
XdmfAttribute()36 XdmfAttribute::XdmfAttribute() {
37   this->SetElementName("Attribute");
38   this->AttributeType = XDMF_ATTRIBUTE_TYPE_NONE;
39   this->ValuesAreMine = 1;
40   this->Values = NULL;
41   this->ShapeDesc = new XdmfDataDesc();
42   this->Active = 0;
43   this->LightDataLimit = 100;
44   this->Units = NULL;    // Ian Curington, HR Wallingford Ltd.
45   }
46 
~XdmfAttribute()47 XdmfAttribute::~XdmfAttribute() {
48   if( this->ValuesAreMine && this->Values )  delete this->Values;
49   delete this->ShapeDesc;
50   if(this->Units) delete [] this->Units;  // Ian Curington, HR Wallingford Ltd.
51 }
52 
53 XdmfInt32
Release()54 XdmfAttribute::Release(){
55   if( this->ValuesAreMine && this->Values )  delete this->Values;
56   this->Values = NULL;
57   return(XDMF_SUCCESS);
58 }
59 
60 XdmfInt32
Insert(XdmfElement * Child)61 XdmfAttribute::Insert( XdmfElement *Child){
62     if(Child && (
63         XDMF_WORD_CMP(Child->GetElementName(), "DataItem") ||
64         XDMF_WORD_CMP(Child->GetElementName(), "Information")
65         )){
66         return(XdmfElement::Insert(Child));
67     }else{
68         XdmfErrorMessage("Attribute can only Insert DataItem or Information elements");
69     }
70     return(XDMF_FAIL);
71 }
72 
GetDataItem()73 XdmfDataItem * XdmfAttribute::GetDataItem(){
74     XdmfDataItem *di = NULL;
75     XdmfXmlNode Node = this->DOM->FindDataElement(0, this->GetElement());
76 
77     if(Node) {
78         di = (XdmfDataItem *)this->GetCurrentXdmfElement(Node);
79     }
80     if(!di){
81         di = new XdmfDataItem;
82         Node = this->DOM->InsertNew(this->GetElement(), "DataItem");
83         di->SetDOM(this->DOM);
84         di->SetElement(Node);
85     }
86     return(di);
87 }
88 
GetInformation(const XdmfInt32 Index)89 XdmfInformation * XdmfAttribute::GetInformation(const XdmfInt32 Index){
90     if(Index < this->DOM->FindNumberOfElements("Information", this->Element )){
91       XdmfInformation *di = NULL;
92       XdmfXmlNode Node =  this->DOM->FindElement( "Information", Index, this->Element, 0);
93       if(Node) {
94         di = new XdmfInformation;
95         di->SetDeleteOnGridDelete(true);
96         di->SetDOM( this->DOM );
97         di->SetElement( Node );
98         di->UpdateInformation();
99         return(di);
100       }
101     }else{
102         XdmfErrorMessage("Grid has " << this->DOM->FindNumberOfElements("Information", this->Element ) << " Information. Index " << Index << " is out of range");
103     }
104     return(NULL);
105 }
106 
107 XdmfInt32
Build()108 XdmfAttribute::Build(){
109     if(XdmfElement::Build() != XDMF_SUCCESS) return(XDMF_FAIL);
110     if (this->GetActive())
111       {
112       this->Set("Active", "1");
113       }
114     this->Set("AttributeType", this->GetAttributeTypeAsString());
115     this->Set("Center", this->GetAttributeCenterAsString());
116     if(this->BuildFromDataXml() == XDMF_SUCCESS) return(XDMF_SUCCESS);
117     if(this->Values){
118         XdmfDataItem * di = this->GetDataItem();
119         di->SetArray(this->Values);
120         if(this->Values->GetNumberOfElements() > this->LightDataLimit) di->SetFormat(XDMF_FORMAT_HDF);
121         di->Build();
122         this->SetCurrentXdmfElement(di->GetElement(), NULL);
123         delete di;
124     }
125 // PATCH September 09, Ian Curington, HR Wallingford Ltd.
126 	if(this->Units)
127 	{
128 		this->Set("Units", this->GetUnits());
129 	}
130 // end patch
131 
132     return(XDMF_SUCCESS);
133 }
134 
135 XdmfConstString
GetAttributeTypeAsString(void)136 XdmfAttribute::GetAttributeTypeAsString( void ){
137   switch ( this->AttributeType ){
138     case XDMF_ATTRIBUTE_TYPE_SCALAR  :
139       return("Scalar");
140     case XDMF_ATTRIBUTE_TYPE_VECTOR  :
141       return("Vector");
142     case XDMF_ATTRIBUTE_TYPE_TENSOR  :
143       return("Tensor");
144     case XDMF_ATTRIBUTE_TYPE_MATRIX  :
145       return("Matrix");
146     case XDMF_ATTRIBUTE_TYPE_TENSOR6 :
147       return("Tensor6");
148     case XDMF_ATTRIBUTE_TYPE_GLOBALID :
149       return("GlobalId");
150     default :
151       break;
152     }
153 return("None");
154 }
155 
156 XdmfInt32
SetAttributeTypeFromString(XdmfConstString attributeType)157 XdmfAttribute::SetAttributeTypeFromString( XdmfConstString attributeType ){
158 XdmfInt64  Dimensions[3];
159 
160 XdmfDebug("Setting Type to " << attributeType );
161 if( XDMF_WORD_CMP( attributeType, "Scalar" ) ) {
162   this->AttributeType = XDMF_ATTRIBUTE_TYPE_SCALAR;
163   Dimensions[0] = 1;
164   this->ShapeDesc->SetShape( 1, Dimensions );
165 } else if( XDMF_WORD_CMP( attributeType, "Vector" ) ) {
166   this->AttributeType = XDMF_ATTRIBUTE_TYPE_VECTOR;
167   Dimensions[0] = 3;
168   this->ShapeDesc->SetShape( 1, Dimensions );
169 } else if( XDMF_WORD_CMP( attributeType, "Tensor" ) ) {
170   this->AttributeType = XDMF_ATTRIBUTE_TYPE_TENSOR;
171   Dimensions[0] = 3;
172   Dimensions[1] = 3;
173   this->ShapeDesc->SetShape( 2, Dimensions );
174 } else if( XDMF_WORD_CMP( attributeType, "Matrix" ) ) {
175   this->AttributeType = XDMF_ATTRIBUTE_TYPE_MATRIX;
176 } else if( XDMF_WORD_CMP( attributeType, "Tensor6" ) ) {
177   this->AttributeType = XDMF_ATTRIBUTE_TYPE_TENSOR6;
178   Dimensions[0] = 6;
179 } else if( XDMF_WORD_CMP( attributeType, "GlobalId" ) ) {
180   this->AttributeType = XDMF_ATTRIBUTE_TYPE_GLOBALID;
181   Dimensions[0] = 1;
182 } else {
183   XdmfErrorMessage("Unknown Attribute Type " << attributeType );
184   return( XDMF_FAIL );
185   }
186 return( XDMF_SUCCESS );
187 }
188 
189 XdmfConstString
GetAttributeCenterAsString(void)190 XdmfAttribute::GetAttributeCenterAsString( void ){
191   switch ( this->AttributeCenter ){
192     case XDMF_ATTRIBUTE_CENTER_GRID :
193       return( "Grid" );
194     case XDMF_ATTRIBUTE_CENTER_CELL :
195       return( "Cell" );
196     case XDMF_ATTRIBUTE_CENTER_FACE :
197       return( "Face" );
198     case XDMF_ATTRIBUTE_CENTER_EDGE :
199       return( "Edge" );
200     case XDMF_ATTRIBUTE_CENTER_NODE :
201       return( "Node" );
202     default :
203       break;
204     }
205 return( "Node" );
206 }
207 
208 XdmfInt32
SetAttributeCenterFromString(XdmfConstString attributeCenter)209 XdmfAttribute::SetAttributeCenterFromString( XdmfConstString attributeCenter ){
210 
211 if( XDMF_WORD_CMP( attributeCenter, "Grid" ) ) {
212   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_GRID;
213 } else if( XDMF_WORD_CMP( attributeCenter, "Cell" ) ) {
214   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_CELL;
215 } else if( XDMF_WORD_CMP( attributeCenter, "Face" ) ) {
216   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_FACE;
217 } else if( XDMF_WORD_CMP( attributeCenter, "Edge" ) ) {
218   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_EDGE;
219 } else if( XDMF_WORD_CMP( attributeCenter, "Node" ) ) {
220   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_NODE;
221 } else {
222   XdmfErrorMessage("Unknown Attribute Center " << attributeCenter );
223   return( XDMF_FAIL );
224   }
225 return( XDMF_SUCCESS );
226 }
227 
228 XdmfInt32
SetValues(XdmfArray * someValues)229 XdmfAttribute::SetValues(XdmfArray *someValues){
230     if(someValues == this->Values) return(XDMF_SUCCESS);
231     if(this->ValuesAreMine && this->Values) delete this->Values;
232     this->ValuesAreMine = 0;
233     this->Values = someValues;
234     return(XDMF_SUCCESS);
235 }
236 
237 XdmfArray *
GetValues(XdmfInt32 Create)238 XdmfAttribute::GetValues(XdmfInt32 Create){
239     if(!this->Values && Create){
240         this->Values = new XdmfArray;
241         this->ValuesAreMine = 1;
242     }
243     return(this->Values);
244 }
245 
246 XdmfInt32
UpdateInformation()247 XdmfAttribute::UpdateInformation() {
248 
249 XdmfConstString  Attribute;
250 
251 if(XdmfElement::UpdateInformation() != XDMF_SUCCESS) return(XDMF_FAIL);
252 if( XDMF_WORD_CMP(this->GetElementType(), "Attribute") == 0){
253     XdmfErrorMessage("Element type" << this->GetElementType() << " is not of type 'Attribute'");
254     return(XDMF_FAIL);
255 }
256 
257 Attribute = this->Get( "AttributeType" );
258 if(!Attribute) Attribute = this->Get( "Type" );
259 if( Attribute ){
260   this->SetAttributeTypeFromString( Attribute );
261 } else {
262   this->AttributeType = XDMF_ATTRIBUTE_TYPE_SCALAR;
263 }
264 free((void*)Attribute);
265 
266 // PATCH September 09, Ian Curinton HR Wallingford Ltd.
267 Attribute = this->Get( "Units" );
268 if( Attribute ){
269   this->SetUnits( Attribute );
270 } else {
271   if(this->Units) delete [] this->Units;
272   this->Units = NULL;
273 }
274 // end patch
275 free((void*)Attribute);
276 
277 Attribute = this->Get( "Active" );
278 this->Active = 0;
279 if ( Attribute ){
280   if( XDMF_WORD_CMP( Attribute, "1" ) ) {
281     this->Active = 1;
282   }
283 }
284 free((void*)Attribute);
285 
286 Attribute = this->Get( "Center" );
287 if( Attribute ){
288   this->SetAttributeCenterFromString( Attribute );
289 } else {
290   this->AttributeCenter = XDMF_ATTRIBUTE_CENTER_NODE;
291 }
292 free((void*)Attribute);
293 
294 Attribute = this->Get( "Dimensions" );
295 if( Attribute ){
296   this->ShapeDesc->SetShapeFromString( Attribute );
297 }else{
298     XdmfXmlNode  ValuesNode;
299     ValuesNode = this->DOM->FindDataElement( 0, Element );
300     if(!ValuesNode){
301         XdmfErrorMessage("Dimensions of Attribute not set in XML and no DataItem found");
302     }
303     Attribute = this->DOM->Get( ValuesNode, "Dimensions" );
304     if(!Attribute){
305         XdmfErrorMessage("Dimensions of Attribute not set in XML or DataItem");
306         free((void*)Attribute);
307         return(XDMF_FAIL);
308     }else{
309         this->ShapeDesc->SetShapeFromString( Attribute );
310     }
311 }
312 if(!this->Name) this->SetName(GetUnique("Attribute_"));
313 free((void*)Attribute);
314 return( XDMF_SUCCESS );
315 }
316 
317 XdmfInt32
Update()318 XdmfAttribute::Update() {
319 
320 XdmfInt32  Status;
321 XdmfXmlNode  ValuesNode;
322 XdmfDataItem ValueReader;
323 
324 
325 
326 if(XdmfElement::Update() != XDMF_SUCCESS) return(XDMF_FAIL);
327 if( this->AttributeType == XDMF_ATTRIBUTE_TYPE_NONE ){
328   Status = this->UpdateInformation();
329   if( Status == XDMF_FAIL ) {
330     XdmfErrorMessage("Can't Initialize");
331     return( XDMF_FAIL );
332     }
333   }
334 
335 ValuesNode = this->DOM->FindDataElement( 0, Element );
336 if( ValuesNode ){
337   ValueReader.SetDOM( this->DOM );
338   ValueReader.SetDsmBuffer(this->DsmBuffer);
339   if( this->ValuesAreMine && this->Values ){
340     delete this->Values;
341     this->Values = NULL;
342   } else {
343   }
344   if(ValueReader.SetElement(ValuesNode) == XDMF_FAIL) return(XDMF_FAIL);
345   if(ValueReader.UpdateInformation() == XDMF_FAIL) return(XDMF_FAIL);
346   if(ValueReader.Update() == XDMF_FAIL) return(XDMF_FAIL);
347   // Steal the array
348   this->Values = ValueReader.GetArray();
349   ValueReader.SetArrayIsMine(0);
350   this->ValuesAreMine = 1;
351   if( !this->Values ) {
352     XdmfErrorMessage("Error Retriving Data Values");
353     return( XDMF_FAIL );
354     }
355 } else {
356   XdmfErrorMessage("Element has no Data");
357   return( XDMF_FAIL );
358   }
359 return( XDMF_SUCCESS );
360 }
361 
362 }
363