mesh_xml_file.cpp 51 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
#include "inmost.h"
2
3


4
#if defined(USE_MESH)
Kirill Terekhov's avatar
Kirill Terekhov committed
5
6
7
8
9
10
11
12
13
14

namespace INMOST
{
  void Mesh::LoadXML(std::string File)
  {
    std::fstream infile(File.c_str(),std::ios::in);
    XMLReader reader(File,infile);
    std::vector<Tag> tags;
    std::vector<HandleType> new_nodes, new_edges, new_faces, new_cells, new_sets;
    std::vector<ElementSet::ComparatorType> set_comparators;
15
16
17
18
19
20
    XMLReader::XMLTag PassTag;
    bool pass_tag = false;
    int nmeshes = 1;

    XMLReader::XMLTag TagParallelMesh = reader.OpenTag();
    if( TagParallelMesh.name != "ParallelMesh" )
Kirill Terekhov's avatar
Kirill Terekhov committed
21
    {
22
      reader.Report("Incorrect XML tag %s expected ParallelMesh",TagParallelMesh.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
23
24
      throw BadFile;
    }
25
26

    for(int q = 0; q < TagParallelMesh.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
27
    {
28
29
30
      XMLReader::XMLAttrib & attr = TagParallelMesh.GetAttib(q);
      if( attr.name == "Number" ) nmeshes = atoi(attr.value.c_str());
      else reader.Report("Unused attribute for ParallelMesh %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
31
    }
32
33
    
    for(XMLReader::XMLTag TagMesh = reader.OpenTag(); !TagMesh.Finalize() && TagMesh.name == "Mesh"; reader.CloseTag(TagMesh), TagMesh = reader.OpenTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
34
35
    {
      bool repair_orientation = false;
36
      for(int q = 0; q < TagMesh.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
37
      {
38
39
40
        XMLReader::XMLAttrib & attr = TagMesh.GetAttib(q);
        if( attr.name == "RepairOrientation" ) repair_orientation = reader.ParseBool(attr.value);
        else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
41
      }
42

Kirill Terekhov's avatar
Kirill Terekhov committed
43
44
      { //Nodes
        int nnodes = 0, ndims = 3;
45
46
47
48
49
50
51
52
53
54
        XMLReader::XMLTag TagNodes;
        if( pass_tag )
        {
          TagNodes = PassTag;
          pass_tag = false;
        }
        else
          TagNodes = reader.OpenTag();

        if( TagNodes.name != "Nodes" )
Kirill Terekhov's avatar
Kirill Terekhov committed
55
        {
56
          reader.Report("Incorrect XML tag %s expected Nodes",TagNodes.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
          throw BadFile;
        }
59
60

        for(int q = 0; q < TagNodes.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
61
        {
62
63
64
          XMLReader::XMLAttrib & attr = TagNodes.GetAttib(q);
          if( attr.name == "Number" ) nnodes = atoi(attr.value.c_str());
          else if( attr.name == "Dimensions" )
Kirill Terekhov's avatar
Kirill Terekhov committed
65
          {
66
67
            ndims = atoi(attr.value.c_str());
            if( GetDimensions() != ndims ) SetDimensions(ndims);
Kirill Terekhov's avatar
Kirill Terekhov committed
68
          }
69
          else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
70
71
72
73
74
        }

        new_nodes.reserve(nnodes);

        {
75
76
77
78
79
          if( reader.ReadOpenContents() )
          {
            std::vector<double> Vector;
            int Repeat;
            dynarray<Storage::real,3> xyz;
80
            for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord() )
Kirill Terekhov's avatar
Kirill Terekhov committed
81
            {
82
83
              reader.ParseReal(val,Vector,Repeat,nnodes);
              for(int l = 0; l < Repeat; ++l)
Kirill Terekhov's avatar
Kirill Terekhov committed
84
              {
85
                for(int q = 0; q < (int)Vector.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
86
                {
87
88
89
90
91
92
                  xyz.push_back(Vector[q]);
                  if( xyz.size() == ndims )
                  {
                    new_nodes.push_back(CreateNode(xyz.data())->GetHandle());
                    xyz.clear();
                  }
Kirill Terekhov's avatar
Kirill Terekhov committed
93
94
95
                }
              }
            }
96
            reader.ReadCloseContents();
Kirill Terekhov's avatar
Kirill Terekhov committed
97
          }
98
99
100
101
102
103
          else 
          {
            reader.Report("Cannot find contents of XML tag");
            throw BadFile;
          }

104
          reader.CloseTag(TagNodes);
Kirill Terekhov's avatar
Kirill Terekhov committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
        }

        if( new_nodes.size() != nnodes )
        {
          reader.Report("Number of records for nodes %d do not match specified number of coordinates %d",new_nodes.size(),nnodes);
        }
      }

      { //Edges, Faces, Cells
        bool repeat = false;
          
        do
        {
          int nelems = 0, ntotconns = 0;
          bool matchelems = false;
          std::vector<HandleType> * elems;
          ElementType curtype;
122
123
124
125
126


          XMLReader::XMLTag TagElems;
          
          if( pass_tag )
Kirill Terekhov's avatar
Kirill Terekhov committed
127
          {
128
129
130
131
132
133
134
135
136
            TagElems = PassTag;
            pass_tag = false;
          }
          else
            TagElems = reader.OpenTag();

          if( !(TagElems.name == "Cells" || TagElems.name == "Faces" || TagElems.name == "Edges" ) )
          {
            reader.Report("Unexpected tag %s while waiting for either Cells or Faces or Edges",TagElems.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
137
138
            throw BadFile;
          }
139
140
          if( TagElems.name != "Cells" ) repeat = true; else repeat = false;
          if( TagElems.name == "Cells" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
141
142
143
144
          {
            elems = &new_cells;
            curtype = CELL;
          }
145
          else if( TagElems.name == "Faces" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
146
147
148
149
          {
            elems = &new_faces;
            curtype = FACE;
          }
150
          else if( TagElems.name == "Edges" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
151
152
153
154
          {
            elems = &new_edges;
            curtype = EDGE;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
155
          
156
157

          for(int q = 0; q < TagElems.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
158
          {
159
            XMLReader::XMLAttrib & attr = TagElems.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
160
161
162
163
164
165
            if( attr.name == "Number" ) 
            {
                nelems = atoi(attr.value.c_str());
                matchelems = true;
                //std::cout << "nelems: " << nelems << " text " << attr.value.c_str() << std::endl;
            }
166
            else reader.Report("Unused attribute for %ss %s='%s'",TagElems.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
167
          } 
168
          
Kirill Terekhov's avatar
Kirill Terekhov committed
169
170
171
172
173
174
175
176
177
178
          elems->reserve(nelems);

            
          HandleType * links[3] =
          {
            new_nodes.empty() ? NULL : &new_nodes[0],
            new_edges.empty() ? NULL : &new_edges[0],
            new_faces.empty() ? NULL : &new_faces[0]
          };

179
180
181
          
          XMLReader::XMLTag TagConns;
          for(TagConns = reader.OpenTag(); !TagConns.Finalize() && TagConns.name == "Connections"; reader.CloseTag(TagConns), TagConns = reader.OpenTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
182
183
184
          {
            int nconns = 0;
            int offset = 0;
185
			int dims = 3; //to distinguish 3d cells from 2d cells when they are created with nodes
Kirill Terekhov's avatar
Kirill Terekhov committed
186
            ElementType subtype = NODE;
187
            for(int q = 0; q < TagConns.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
188
            {
189
              XMLReader::XMLAttrib & attr = TagConns.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
190
              if( attr.name == "Number" ) nconns = atoi(attr.value.c_str());
191
			  else if( attr.name == "Dimensions" ) dims = atoi(attr.value.c_str());
192
193
194
              else if( attr.name == "Type" ) subtype = reader.atoes(attr.value.c_str());
              else if( attr.name == "Offset" ) offset = atoi(attr.value.c_str());
              else reader.Report("Unused attribute for %ss %s='%s'",TagConns.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
195
            }
196
            
Kirill Terekhov's avatar
Kirill Terekhov committed
197
198
199
200
201
202
203
204
            ntotconns += nconns;
            if( subtype >= curtype )
            {
              reader.Report("%ss cannot be constructed from %ss",ElementTypeName(curtype),ElementTypeName(subtype));
              throw BadFile;
            }
            reader.ReadOpenContents();
            ElementArray<Element> subarr(this);
205
            for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
            {
              int num = atoi(val.c_str()), elem;
              subarr.clear();
              subarr.reserve(num);
              for(int q = 0; q < num && !reader.isContentsEnded(); ++q)
              {
                val = reader.GetContentsWord();
                elem = atoi(val.c_str());
                //subarr.push_back(ComposeHandle(subtype,elem-offset));
                subarr.push_back(links[ElementNum(subtype)][elem-offset]);
              }
              switch(curtype)
              {
              case EDGE:
                elems->push_back(CreateEdge(subarr.Convert<Node>()).first.GetHandle());
                break;
              case FACE:
                if( subtype == NODE )
224
225
226
227
228
				{
					Face f = CreateFace(subarr.Convert<Node>()).first;
					if( repair_orientation ) f.FixEdgeOrder();
					elems->push_back(f.GetHandle());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
229
                else if( subtype == EDGE )
230
231
232
233
234
				{
					Face f = CreateFace(subarr.Convert<Node>()).first;
					if( repair_orientation ) f.FixEdgeOrder();
					elems->push_back(f.GetHandle());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
235
236
237
238
                break;
              case CELL:
                if( subtype == NODE )
                {
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
					if( dims == 3 )
					{
						switch(subarr.size())
						{
						case 4: //Tetrahedron
							{
								const integer nodesnum[12] = {0,2,1,0,1,3,1,2,3,0,3,2};
								const integer sizes[4] = {3,3,3,3};
								elems->push_back(CreateCell(subarr.Convert<Node>(),nodesnum,sizes,4).first.GetHandle());
							}
							break;
						case 5: //Pyramid
							{
								const integer nodesnum[16] = {0,4,3,0,1,4,1,2,4,3,4,2,0,3,2,1};
								const integer sizes[5] = {3,3,3,3,4};
								elems->push_back(CreateCell(subarr.Convert<Node>(),nodesnum,sizes,5).first.GetHandle());
							}
							break;
						case 6: //Wedge or Prism
							{
								const integer nodesnum[18] = {0,2,5,3,1,4,5,2,0,3,4,1,3,5,4,0,1,2};
								const integer sizes[5] = {4,4,4,3,3};
								elems->push_back(CreateCell(subarr.Convert<Node>(),nodesnum,sizes,5).first.GetHandle());
							}
							break;
						case 8: //Hexahedron
							{
								const integer nodesnum[24] = {0,4,7,3,1,2,6,5,0,1,5,4,3,7,6,2,0,3,2,1,4,5,6,7};
								const integer sizes[6] = {4,4,4,4,4,4};
								elems->push_back(CreateCell(subarr.Convert<Node>(),nodesnum,sizes,6).first.GetHandle());
							}
							break;
						default: 
							reader.Report("Sorry, no rule to convert %d nodes into a cell",subarr.size());
							throw BadFile;
							break;
						}
					}
					else if( dims == 2 )
					{
						ElementArray<Node> e_nodes(this,1);
						ElementArray<Edge> f_edges(this,2);
						ElementArray<Face> c_faces;
						for(unsigned int k = 0; k < subarr.size(); k++)
						{
							e_nodes.at(0) = subarr.at(k);
							f_edges.at(0) = CreateEdge(e_nodes).first->GetHandle();
							e_nodes.at(0) = subarr.at((k+1)%subarr.size());
							f_edges.at(1) = CreateEdge(e_nodes).first->GetHandle();
							c_faces.push_back(CreateFace(f_edges).first);
						}
						Cell c = CreateCell(c_faces).first;
						if( repair_orientation ) c.FixEdgeOrder();
						elems->push_back(c.GetHandle());
					}
					else reader.Report("Sorry, cannot understand number of dimensions %d for a cell",dims);
Kirill Terekhov's avatar
Kirill Terekhov committed
295
296
297
298
299
300
301
302
303
304
305
306
                }
                else if( subtype == EDGE )
                {
                  reader.Report("Sorry, no rule to convert %d edges into a cell",subarr.size());
                  throw BadFile;
                }
                else if( subtype == FACE )
                  elems->push_back(CreateCell(subarr.Convert<Face>()).first.GetHandle());
                break;
              }
            }
            reader.ReadCloseContents();
307
          } 
Kirill Terekhov's avatar
Kirill Terekhov committed
308
309
310

          if( matchelems && nelems != ntotconns) reader.Report("Number %d of elements encountered do not match to the specified number %d",ntotconns,nelems);

311
312
313
314
315
316
317
318
          reader.CloseTag(TagElems);

          if( !TagConns.Finalize() )
          {
            PassTag = TagConns;
            pass_tag = true;
          }

Kirill Terekhov's avatar
Kirill Terekhov committed
319
320
321
322
323
324
325
        } while(repeat);
      }

      { //Sets and Data
        bool repeat = false;
        do
        {
326
327
328
329
330
331
332
333
          XMLReader::XMLTag TagSetsData;
          if( pass_tag )
          {
            TagSetsData = PassTag;
            pass_tag = false;
          }
          else TagSetsData = reader.OpenTag();
          
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
		  if( TagSetsData.name == "Tags" )
		  {
			  repeat = true;
			  int ntags = 0;
			  bool matchntags = false;
			  for(int q = 0; q < TagSetsData.NumAttrib(); ++q)
			  {
				  XMLReader::XMLAttrib & attr = TagSetsData.GetAttib(q);
				  if( attr.name == "Number" ) 
				  {
					  ntags = atoi(attr.value.c_str());
					  matchntags = true;
				  }
				  else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
			  }

			  tags.reserve(ntags);


			  XMLReader::XMLTag TagTag;
			  for(TagTag = reader.OpenTag(); !TagTag.Finalize() && TagTag.name == "Tag"; reader.CloseTag(TagTag), TagTag = reader.OpenTag())
			  {
				  std::vector<std::string> parsed;
				  std::string tagname = "";
				  enumerator size = ENUMUNDEF;
				  DataType type = DATA_REAL;
				  ElementType sparse = NONE;
				  ElementType defined = NONE;
				  for(int q = 0; q < TagTag.NumAttrib(); ++q)
				  {
					  XMLReader::XMLAttrib & attr = TagTag.GetAttib(q);
					  if( attr.name == "Name" ) tagname = attr.value;
					  else if( attr.name == "Size" ) 
					  {
						  if( attr.value == "Variable" )
							  size = ENUMUNDEF;
						  else size = atoi(attr.value.c_str());
					  }
					  else if( attr.name == "Type" )
					  {
						  if( attr.value == "Real" ) type = DATA_REAL;
						  else if( attr.value == "Integer" ) type = DATA_INTEGER;
						  else if( attr.value == "Reference" ) type = DATA_REFERENCE;
						  else if( attr.value == "RemoteReference" ) type = DATA_REMOTE_REFERENCE;
						  else if( attr.value == "Bulk" ) type = DATA_BULK;
#if defined(USE_AUTODIFF)
						  else if( attr.value == "Variable" ) type = DATA_VARIABLE;
#endif
					  }
					  else if( attr.name == "Sparse" )
					  { 
						  reader.ParseCommaSeparated(attr.value,parsed);
						  for(int q = 0; q < (int)parsed.size(); ++q) sparse |= reader.atoes(parsed[q].c_str());
					  }
					  else if( attr.name == "Definition" )
					  {
						  reader.ParseCommaSeparated(attr.value,parsed);
						  for(int q = 0; q < (int)parsed.size(); ++q) defined |= reader.atoes(parsed[q].c_str());
					  }
					  else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
				  }
				  if( tagname == "" )
					  reader.Report("Tag name was not specified");
				  else if( defined == NONE )
					  reader.Report("Domain of definition for the tag was not specified");
				  tags.push_back(CreateTag(tagname,type,defined,sparse,size));
			  }
			  reader.CloseTag(TagSetsData);

			  if( !TagTag.Finalize() )
			  {
				  PassTag = TagTag;
				  pass_tag = true;
			  }

			  if( matchntags && ntags != tags.size() ) reader.Report("Number %d of XML tags Tag red do not match to the specified number %d",tags.size(),ntags);
		  }
          else if( TagSetsData.name == "Sets" )
Kirill Terekhov's avatar
Kirill Terekhov committed
412
413
414
415
416
417
418
419
420
421
422
423
          {
            repeat = true;
            HandleType * links[4] =
            {
              new_nodes.empty() ? NULL : &new_nodes[0],
              new_edges.empty() ? NULL : &new_edges[0],
              new_faces.empty() ? NULL : &new_faces[0],
              new_cells.empty() ? NULL : &new_cells[0]
            };
            int nsets = 0, sets_offset = 0;
            int nsets_read = 0;
            bool matchsets = false;
424
            for(int q = 0; q < TagSetsData.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
425
            {
426
427
              XMLReader::XMLAttrib & attr = TagSetsData.GetAttib(q);
              if( attr.name == "Number" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
428
              {
429
                nsets = atoi(attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
430
431
                matchsets = true;
              }
432
433
              else if( attr.name == "Offset" ) sets_offset = atoi(attr.value.c_str());
              else reader.Report("Unused attribute for %ss %s='%s'",TagSetsData.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
434
435
            }
            new_sets.reserve(nsets);
436
437
438
            
            XMLReader::XMLTag Set;
            for(Set = reader.OpenTag(); !Set.Finalize() && Set.name == "Set"; reader.CloseTag(Set), Set = reader.OpenTag() )
Kirill Terekhov's avatar
Kirill Terekhov committed
439
440
441
442
443
444
            {
              int size = 0, offset = 0;
              std::string name;
              HandleType parent = InvalidHandle(), child = InvalidHandle(), sibling = InvalidHandle();
              ElementSet::ComparatorType comparator = ElementSet::UNSORTED_COMPARATOR;
              nsets_read++;
445
              for(int q = 0; q < Set.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
446
              {
447
448
449
450
451
                XMLReader::XMLAttrib & attr = Set.GetAttib(q);
                if( attr.name == "Size" ) size = atoi(attr.value.c_str());
                else if( attr.name == "Offset" ) offset = atoi(attr.value.c_str());
                else if( attr.name == "Name" ) name = attr.value;
                else if( attr.name == "Parent" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
452
                {
453
454
                  if( attr.value != "Unset" )
                    parent = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
455
                }
456
                else if( attr.name == "Sibling" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
457
                {
458
459
                  if( attr.value != "Unset" )
                    sibling = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
460
                }
461
                else if( attr.name == "Child" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
462
                {
463
464
                  if( attr.value != "Unset" )
                    child = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
465
                }
466
                else if( attr.name == "Comparator" )
Kirill Terekhov's avatar
Kirill Terekhov committed
467
                {
468
469
470
471
472
473
                  if( attr.value == "Unsorted" ) comparator = ElementSet::UNSORTED_COMPARATOR;
                  else if( attr.value == "Identificator" ) comparator = ElementSet::GLOBALID_COMPARATOR;
                  else if( attr.value == "Centroid" ) comparator = ElementSet::CENTROID_COMPARATOR;
                  else if( attr.value == "Hierarchy" ) comparator = ElementSet::HIERARCHY_COMPARATOR;
                  else if( attr.value == "Handle" ) comparator = ElementSet::HANDLE_COMPARATOR;
                  else reader.Report("Unexpected comparator type %s for attribute Comparator, expected Unsorted,Identificator,Centroid,Hierarchy,Handle",attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
474
                }
475
                else reader.Report("Unused attribute for %ss %s='%s'",Set.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
476
477
478
479
480
481
482
483
484
485
486
487
488
              } 
              ElementSet s = CreateSet(name).first;
              new_sets.push_back(s.GetHandle());
              set_comparators.push_back(comparator);
              Element::adj_type & hc = HighConn(s.GetHandle());
              Element::adj_type & lc = LowConn(s.GetHandle());
              //here we believe that all the connections are consistent
              //hc.resize(ElementSet::high_conn_reserved);
              if( parent != InvalidHandle() ) ElementSet::hParent(hc) = parent;
              if( child != InvalidHandle() ) ElementSet::hChild(hc) = child;
              if( sibling != InvalidHandle() ) ElementSet::hSibling(hc) = sibling;
              //s.SortSet(comparator);
              reader.ReadOpenContents();
489
              for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
              {
                std::vector<std::pair<ElementType,int> > Vector;
                int Repeat;
                reader.ParseReference(val,Vector,Repeat,size);
                if( !Vector.empty() ) for(int l = 0; l < Repeat; ++l) 
                {
                  for(int q = 0; q < (int)Vector.size(); ++q)
                  {
                    if( ElementNum(Vector[q].first) < 4 )
                      lc.push_back(links[ElementNum(Vector[q].first)][Vector[q].second-offset]);
                    else if( ElementNum(Vector[q].first) == 4 ) //Set
                      lc.push_back(ComposeHandle(Vector[q].first,Vector[q].second-offset));
                    else //Mesh
                      lc.push_back(GetHandle());
                  }
                    //s.PutElement(links[ElementNum(Vector[q].first)][Vector[q].second-offset]);
                  //s.PutElements(Vector[0],(enumerator)Vector.size());
                }
              }
              reader.ReadCloseContents();
            }
511
512
513
514
515
516
517
518
            
            reader.CloseTag(TagSetsData);
            if( !Set.Finalize() )
            {
              PassTag = Set;
              pass_tag = true;
            }
            
Kirill Terekhov's avatar
Kirill Terekhov committed
519
520
521
            if( matchsets && nsets != nsets_read ) reader.Report("Number %d of XML tags Set read do not match to the number %d specified",nsets_read,nsets);

            //correct links between sets
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
522
            for(int q = 0; q < (int)new_sets.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
            {
              Element::adj_type & lc = HighConn(new_sets[q]);
              for(Element::adj_type::iterator jt = lc.begin(); jt != lc.end(); ++jt)
              {
                if( GetHandleElementType(*jt) == ESET )
                  *jt = new_sets[GetHandleID(*jt)];
              }
              Element::adj_type & hc = HighConn(new_sets[q]);
              if( ElementSet::hParent(hc) != InvalidHandle() ) ElementSet::hParent(hc) = new_sets[GetHandleID(ElementSet::hParent(hc))-sets_offset];
              if( ElementSet::hChild(hc) != InvalidHandle() ) ElementSet::hChild(hc) = new_sets[GetHandleID(ElementSet::hChild(hc))-sets_offset];
              if( ElementSet::hSibling(hc) != InvalidHandle() ) ElementSet::hSibling(hc) = new_sets[GetHandleID(ElementSet::hSibling(hc))-sets_offset];
              if( set_comparators[q] != ElementSet::UNSORTED_COMPARATOR ) ElementSet(this,new_sets[q]).SortSet(set_comparators[q]);
            }
          }
537
          else if( TagSetsData.name == "Data" )
Kirill Terekhov's avatar
Kirill Terekhov committed
538
539
540
541
542
543
544
545
546
547
548
549
          {
            repeat = false;
            HandleType * links[5] =
            {
              new_nodes.empty() ? NULL : &new_nodes[0],
              new_edges.empty() ? NULL : &new_edges[0],
              new_faces.empty() ? NULL : &new_faces[0],
              new_cells.empty() ? NULL : &new_cells[0],
              new_sets.empty() ? NULL : &new_sets[0]
            };
            int ndata = 0, ndatapass = 0;
            bool matchndata = false;
550
551

            for(int q = 0; q < TagSetsData.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
552
            {
553
554
              XMLReader::XMLAttrib & attr = TagSetsData.GetAttib(q);
              if( attr.name == "Number" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
555
              {
556
                ndata = atoi(attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
557
558
                matchndata = true;
              }
559
              else reader.Report("Unused attribute for %ss %s='%s'",TagSetsData.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
560
561
            }

562
563
            XMLReader::XMLTag TagDataSet;
            for(TagDataSet = reader.OpenTag(); !TagDataSet.Finalize() && TagDataSet.name == "DataSet"; reader.CloseTag(TagDataSet), TagDataSet = reader.OpenTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
564
565
566
            {
              ++ndatapass;
              int sparse_read = 2, offset = 0;
567
568
              std::string tagname = "", setname = "", meshname = "";
              Mesh * remote_mesh = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
569
              ElementType etype = NONE;
570
              for(int q = 0; q < TagDataSet.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
571
              {
572
573
                XMLReader::XMLAttrib & attr = TagDataSet.GetAttib(q);
                if( attr.name == "SetType" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
574
                {
575
576
577
578
579
580
581
                  if( attr.value == "Cells" ) etype = CELL;
                  else if( attr.value == "Faces" ) etype = FACE;
                  else if( attr.value == "Edges" ) etype = EDGE;
                  else if( attr.value == "Nodes" ) etype = NODE;
                  else if( attr.value == "Sets" ) etype = ESET;
                  else if( attr.value == "Mesh" ) etype = MESH;
                  else if( attr.value == "SetData" ) etype = NONE;
Kirill Terekhov's avatar
Kirill Terekhov committed
582
                }
583
584
585
586
587
588
                else if( attr.name == "TagName" ) tagname = attr.value;
                else if( attr.name == "SetName" ) setname = attr.value;
                else if( attr.name == "MeshName" ) meshname = attr.value;
                else if( attr.name == "Sparse" ) sparse_read = reader.ParseBool(attr.value);
                else if( attr.name == "Offset" ) offset = atoi(attr.value.c_str());
                else reader.Report("Unused attribute for %ss %s='%s'",TagDataSet.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
589
              }
590
              
Kirill Terekhov's avatar
Kirill Terekhov committed
591
592
593
594
595
596
597
598
599
600
              if( tagname == "" )
              {
                reader.Report("DataSet had no attribute TagName");
                throw BadFile;
              }
              if( !HaveTag(tagname) )
              {
                reader.Report("Tag %s do not exist",tagname.c_str());
                throw BadFile;
              }
601
602
603
604
605
606
              if( meshname != "" )
              {
                remote_mesh = Mesh::GetMesh(meshname);
                if( !remote_mesh )
                  reader.Report("Remote mesh %s do not exist, you should create it first inside of your application",meshname.c_str());
              }
Kirill Terekhov's avatar
Kirill Terekhov committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

              Tag t = GetTag(tagname);
              HandleType * set_elems = NULL, *it = NULL;
              enumerator set_size = 0;

              if( setname != "" )
              {
                if( etype != NONE ) reader.Report("Warning: SetType should be SetData for data specified for set.");
                ElementSet s = GetSet(setname);
                if( !s.isValid() )
                {
                  reader.Report("Set %s is not defined on the mesh",setname.c_str());
                  throw BadFile;
                }
                set_elems = s.getHandles();
                set_size = s.Size(); 
                if( sparse_read == 2 ) sparse_read = 0;
              }
              else
              {
                if( etype == NONE )
                {
                  for(ElementType test = NODE; test <= MESH; test = NextElementType(test) )
                    if( t.isDefined(test) ) etype |= test;
                  if( !OneType(etype) )
                  {
                    reader.Report("You must explicitly specify type of elements for which"
                                  "the data is written since tag %s is defined on multiple types of elements",tagname.c_str());
                    throw BadFile;
                  }
                }
                if( sparse_read == 2 )
                {
                  if( t.isSparse(etype) ) sparse_read = 1; //Expect data to be listed in sparse manner for sparse tag
                  else sparse_read = 0;
                }
                switch(etype)
                {
                case NODE: set_elems = &new_nodes[0]; set_size = (enumerator)new_nodes.size(); break;
                case EDGE: set_elems = &new_edges[0]; set_size = (enumerator)new_edges.size(); break;
                case FACE: set_elems = &new_faces[0]; set_size = (enumerator)new_faces.size(); break;
                case CELL: set_elems = &new_cells[0]; set_size = (enumerator)new_cells.size(); break;
                case ESET: set_elems = &new_sets[0]; set_size = (enumerator)new_sets.size(); break;
                case MESH: set_elems = &handle; set_size = 1; break;
                }
              }
              it = set_elems;
654
655
              reader.ReadOpenContents();
              for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
              {
                if( sparse_read )
                {
                  it = set_elems + atoi(val.c_str());
                  val = reader.GetContentsWord();
                }
                switch(t.GetDataType())
                {
                case DATA_REAL:
                  {
                    Storage::real_array data = RealArray(*it,t);
                    std::vector<Storage::real> Vector; int Repeat;
                    reader.ParseReal(val,Vector,Repeat,set_size);
                    if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read )
                    {
                      reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                      throw BadFile;
                    }
                      
                    for(int l = 0; l < Repeat; ++l)
                    {
                      for(int q = 0; q < (int)Vector.size(); ++q)
                      {
                        data[(q + l*((int)Vector.size()))%data.size()] = Vector[q];
                        if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                        {
                          ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
684
                          if( ((int)(it-set_elems)) < (int)set_size ) data = RealArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
685
686
687
688
689
690
691
692
693
694
695
                        }
                      }
                    }
                  }
                  break;
                case DATA_INTEGER:
                  {
                    Storage::integer_array data = IntegerArray(*it,t);
                    std::vector<Storage::integer> Vector; int Repeat;
                    reader.ParseInteger(val,Vector,Repeat,set_size);
                    if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
696
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
697
698
699
700
701
702
703
704
705
706
707
708
                    {
                      reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                      throw BadFile;
                    }
                    for(int l = 0; l < Repeat; ++l)
                    {
                      for(int q = 0; q < (int)Vector.size(); ++q)
                      {
                        data[(q + l*((int)Vector.size()))%data.size()] = Vector[q];
                        if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                        {
                          ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
709
                          if( ((int)(it-set_elems)) < (int)set_size ) data = IntegerArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
710
711
712
713
714
715
716
717
718
719
720
                        }
                      }
                    }
                  }
                  break;
                case DATA_BULK:
                  {
                    Storage::bulk_array data = BulkArray(*it,t);
                    std::string Vector; int Repeat;
                    reader.ParseBulk(val,Vector,Repeat,set_size);
                    if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
721
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
722
723
724
725
726
727
728
729
730
731
732
733
                    {
                      reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                      throw BadFile;
                    }
                    for(int l = 0; l < Repeat; ++l)
                    {
                      for(int q = 0; q < (int)Vector.size(); ++q)
                      {
                        data[(q + l*((int)Vector.size()))%data.size()] = Vector[q];
                        if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                        {
                          ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
734
                          if( ((int)(it-set_elems)) < (int)set_size ) data = BulkArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
735
736
737
738
739
740
741
742
743
744
745
                        }
                      }
                    }
                  }
                  break;
                case DATA_REFERENCE:
                  {
                    Storage::reference_array data = ReferenceArray(*it,t);
                    std::vector<std::pair<ElementType,int> > Vector; int Repeat;
                    reader.ParseReference(val,Vector,Repeat,set_size);
                    if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
746
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
747
748
749
750
751
752
753
754
755
756
757
758
                    {
                      reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                      throw BadFile;
                    }
                    for(int l = 0; l < Repeat; ++l)
                    {
                      for(int q = 0; q < (int)Vector.size(); ++q)
                      {
                        data[(q + l*((int)Vector.size()))%data.size()] = Element(this,links[ElementNum(Vector[q].first)][Vector[q].second-offset]);
                        if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                        {
                          ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
759
                          if( ((int)(it-set_elems)) < (int)set_size ) data = ReferenceArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
760
761
762
763
764
                        }
                      }
                    }
                  }
                  break;
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
                case DATA_REMOTE_REFERENCE:
                  {
                    Storage::remote_reference_array data = RemoteReferenceArray(*it,t);
                    if( remote_mesh != NULL )
                    {
                      std::vector<std::pair<ElementType,int> > Vector; int Repeat;
                      reader.ParseReference(val,Vector,Repeat,set_size);
                      if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
                      else if( t.GetSize() != Vector.size()*Repeat && sparse_read )
                      {
                        reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                        throw BadFile;
                      }
                      for(int l = 0; l < Repeat; ++l)
                      {
                        for(int q = 0; q < (int)Vector.size(); ++q)
                        {
                          data.at((q + l*((int)Vector.size()))%data.size()).first  = remote_mesh;
                          data.at((q + l*((int)Vector.size()))%data.size()).second = ComposeHandle(Vector[q].first,Vector[q].second-offset);
                          if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                          {
                            ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
787
                            if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
                          }
                        }
                      }
                    }
                    else
                    {
                      std::vector<std::pair<std::string,std::pair<ElementType,int> > > Vector; int Repeat;
                      reader.ParseRemoteReference(val,Vector,Repeat,set_size);
                      if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
                      else if( t.GetSize() != Vector.size()*Repeat && sparse_read )
                      {
                        reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                        throw BadFile;
                      }
                      for(int l = 0; l < Repeat; ++l)
                      {
                        for(int q = 0; q < (int)Vector.size(); ++q)
                        {
                          Mesh * m = GetMesh(Vector[q].first);
807
                          if( m == NULL ) reader.Report("Cannot find remote mesh %s, you should create it first inside of your application",Vector[q].first.c_str());
808
809
810
811
812
                          data.at((q + l*((int)Vector.size()))%data.size()).first = m;
                          data.at((q + l*((int)Vector.size()))%data.size()).second = ComposeHandle(Vector[q].second.first,Vector[q].second.second-offset);
                          if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                          {
                            ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
813
                            if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
814
815
816
817
818
819
                          }
                        }
                      }
                    }
                  }
                  break;
Kirill Terekhov's avatar
Kirill Terekhov committed
820
821
822
823
824
825
826
#if defined(USE_AUTODIFF)
                case DATA_VARIABLE:
                  {
                    Storage::var_array data = VariableArray(*it,t);
                    std::vector<Storage::var> Vector; int Repeat;
                    reader.ParseVariable(val,Vector,Repeat,set_size);
                    if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
827
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read )
Kirill Terekhov's avatar
Kirill Terekhov committed
828
829
830
831
832
833
834
835
836
837
838
839
                    {
                      reader.Report("Cannot write record of size %d into tag data of size %d",Vector.size()*Repeat,t.GetSize());
                      throw BadFile;
                    }
                    for(int l = 0; l < Repeat; ++l)
                    {
                      for(int q = 0; q < (int)Vector.size(); ++q)
                      {
                        data[(q + l*((int)Vector.size()))%data.size()] = Vector[q];
                        if( !sparse_read && t.GetSize() != ENUMUNDEF && (q + l*((int)Vector.size())+1)%t.GetSize() == 0 )
                        {
                          ++it;
Kirill Terekhov's avatar
Fixes    
Kirill Terekhov committed
840
                          if( ((int)(it-set_elems)) < (int)set_size ) data = VariableArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
841
842
843
844
845
846
847
848
849
850
851
852
853
                        }
                      }
                    }
                  }
                  break;
#endif
                }
              } 
              reader.ReadCloseContents();
            }

            if( matchndata && ndata != ndatapass ) reader.Report("warning: Number %d of DataSet tags encountered do not match to the specified %d",ndatapass,ndata);

854
            reader.CloseTag(TagSetsData);
Kirill Terekhov's avatar
Kirill Terekhov committed
855

856
857
858
859
860
            if( !TagDataSet.Finalize() )
            {
              PassTag = TagDataSet;
              pass_tag = true;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
861
          }
862
863
		  else if( TagSetsData.name == "" )
			  repeat = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
864
865
          else
          {
866
            reader.Report("Unexpected tag %s, expected Tags or Sets or Data",TagSetsData.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
867
868
869
870
            throw BadFile;
          }
        } while(repeat);
      }
871
872
      RepairGeometricTags();

Kirill Terekhov's avatar
Kirill Terekhov committed
873
874
875
876
877
878
879
880
      if( repair_orientation )
      {
        int numfixed = 0;
        for(int q = 0; q < (int)new_faces.size(); ++q)
          if( Face(this,new_faces[q]).FixNormalOrientation() ) numfixed++;
        if( numfixed ) std::cout << "Fixed orientation of " << numfixed << " faces" << std::endl;
      }
    }
881
    reader.CloseTag(TagParallelMesh);
Kirill Terekhov's avatar
Kirill Terekhov committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
    infile.close();
  }

  void Mesh::SaveXML(std::string File)
  {
    std::fstream fout(File.c_str(),std::ios::out);
    fout << "<ParallelMesh>\n";
    fout << "\t<Mesh>\n";
    fout << "\t\t<Tags>\n";
    for(int k = 0; k < (int)tags.size(); ++k)
    {
      if( tags[k].GetTagName().substr(0,9) == "PROTECTED" ) continue;
      std::string names[6] = {"Nodes","Edges","Faces","Cells","Sets","Mesh"};
      std::string definition = "", sparse = "", type = "";
      switch(tags[k].GetDataType())
      {
      case DATA_REAL: type = "Real"; break;
      case DATA_INTEGER: type = "Integer"; break;
      case DATA_BULK: type = "Bulk"; break;
      case DATA_REFERENCE: type = "Reference"; break;
902
      case DATA_REMOTE_REFERENCE: type = "RemoteReference"; break;
Kirill Terekhov's avatar
Kirill Terekhov committed
903
904
905
906
907
908
909
910
911
#if defined(USE_AUTODIFF)
      case DATA_VARIABLE: type = "Variable"; break;
#endif
      }
      for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype) )
      {
        if( tags[k].isDefined(etype) ) definition += names[ElementNum(etype)] + ",";
        if( tags[k].isSparse(etype) ) sparse += names[ElementNum(etype)] + ",";
      }
912
913
      if( !definition.empty() ) definition.resize(definition.size()-1); //remove trailing comma
      if( !sparse.empty() ) sparse.resize(sparse.size()-1); //remove trailing comma
Kirill Terekhov's avatar
Kirill Terekhov committed
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
      if( sparse == "" ) sparse = "None";
      fout << "\t\t\t<Tag Name  =\"" << tags[k].GetTagName() << "\"\n";
      if( tags[k].GetSize() !=ENUMUNDEF )
      fout << "\t\t\t     Size  =\"" << tags[k].GetSize() << "\"\n";
      fout << "\t\t\t     Type  =\"" << type << "\"\n";
      fout << "\t\t\t     Sparse=\"" << sparse << "\"\n";
      fout << "\t\t\t     Definition=\"" << definition << "\"/>\n";
    }
    Tag idx = CreateTag("TEMPORARY_XML_ENUMERATOR",DATA_INTEGER,MESH|ESET|CELL|FACE|EDGE|NODE,NONE,1);
    Integer(GetHandle(),idx) = 0;
    fout << "\t\t</Tags>\n";
    fout << "\t\t<Nodes Number=\"" << NumberOfNodes() << "\" Dimensions=\"" << GetDimensions() << "\">\n";
    fout << "\t\t\t<![CDATA[\n";
    dynarray<Storage::real,3> xyz(GetDimensions());
    int cnt = 0;
    for(iteratorNode it = BeginNode(); it != EndNode(); ++it)
    {
      fout << "\t\t\t";
      it->Centroid(xyz.data());
      for(int q = 0; q < GetDimensions(); ++q)
        fout << xyz[q] << " ";
      fout << "\n";
      it->Integer(idx) = cnt++;
    }
    fout << "\t\t\t]]>\n";
    fout << "\t\t</Nodes>\n";
    fout << "\t\t<Edges>\n";
    fout << "\t\t\t<Connections Number=\"" << NumberOfEdges() << "\" Type=\"Nodes\">\n";
    fout << "\t\t\t<![CDATA[\n";
    cnt = 0;
    for(iteratorEdge it = BeginEdge(); it != EndEdge(); ++it)
    {
      ElementArray<Node> nodes = it->getNodes();
      fout << "\t\t\t" << nodes.size();
      for(ElementArray<Node>::iterator jt = nodes.begin(); jt != nodes.end(); ++jt)
        fout << " " << jt->Integer(idx);
      fout << "\n";
      it->Integer(idx) = cnt++;
    }
    fout << "\t\t\t]]>\n";
    fout << "\t\t\t</Connections>\n";
    fout << "\t\t</Edges>\n";
    fout << "\t\t<Faces>\n";
    fout << "\t\t\t<Connections Number=\"" << NumberOfFaces() << "\" Type=\"Edges\">\n";
    fout << "\t\t\t<![CDATA[\n";
    cnt = 0;
    for(iteratorFace it = BeginFace(); it != EndFace(); ++it)
    {
      ElementArray<Edge> edges = it->getEdges();
      fout << "\t\t\t" << edges.size();
      for(ElementArray<Edge>::iterator jt = edges.begin(); jt != edges.end(); ++jt)
        fout << " " << jt->Integer(idx);
      fout << "\n";
      it->Integer(idx) = cnt++;
    }
    fout << "\t\t\t]]>\n";
    fout << "\t\t\t</Connections>\n";
    fout << "\t\t</Faces>\n";
    fout << "\t\t<Cells>\n";
    fout << "\t\t\t<Connections Number=\"" << NumberOfFaces() << "\" Type=\"Faces\">\n";
    fout << "\t\t\t<![CDATA[\n";
    cnt = 0;
    for(iteratorCell it = BeginCell(); it != EndCell(); ++it)
    {
      ElementArray<Face> faces = it->getFaces();
      fout << "\t\t\t" << faces.size();
      for(ElementArray<Face>::iterator jt = faces.begin(); jt != faces.end(); ++jt)
        fout << " " << jt->Integer(idx);
      fout << "\n";
      it->Integer(idx) = cnt++;
    }
    fout << "\t\t\t]]>\n";
    fout << "\t\t\t</Connections>\n";
    fout << "\t\t</Cells>\n";
    fout << "\t\t<Sets>\n";
    cnt = 0;
    for(iteratorSet it = BeginSet(); it != EndSet(); ++it)
      it->Integer(idx) = cnt++;
    for(iteratorSet it = BeginSet(); it != EndSet(); ++it)
    {
      fout << "\t\t\t<Set Name=\"" << it->GetName() << "\"\n";
      if( it->HaveParent() )
      fout << "\t\t\t     Parent=\"" << it->GetParent()->Integer(idx) << "\"\n";
      if( it->HaveChild() )
      fout << "\t\t\t     Child=\"" << it->GetChild()->Integer(idx) << "\"\n";
      if( it->HaveSibling() )
      fout << "\t\t\t     Sibling=\"" << it->GetSibling()->Integer(idx) << "\"\n";
      if( it->GetComparator() != ElementSet::UNSORTED_COMPARATOR )
      {
        fout << "\t\t\t     Comparator=\"";
        switch(it->GetComparator())
        {
        case ElementSet::GLOBALID_COMPARATOR: fout << "Identificator"; break;
        case ElementSet::CENTROID_COMPARATOR: fout << "Centroid"; break;
        case ElementSet::HIERARCHY_COMPARATOR: fout << "Hierarchy"; break;
        case ElementSet::HANDLE_COMPARATOR: fout << "Handle"; break;
        }
        fout << "\"\n";
      }
      fout << "\t\t\t     Size=\"" << it->Size() << "\">\n";
      fout << "\t\t\t<![CDATA[\n";
      int endl_count = 0;
      fout << "\t\t\t";
      for(ElementSet::iterator jt = it->Begin(); jt != it->End(); ++jt)
      {
        switch(jt->GetElementType())
        {
        case NODE: fout << "Node:"; break;
        case EDGE: fout << "Edge:"; break;
        case FACE: fout << "Face:"; break;
        case CELL: fout << "Cell:"; break;
        case ESET: fout << "Set:"; break;
        case MESH: fout << "Mesh:"; break;
        }
        fout << jt->Integer(idx) << " ";
        endl_count++;
        if( endl_count % 8 == 0 )
          fout << "\n\t\t\t";
      }
      fout << "\n\t\t\t]]>\n";
      fout << "\t\t\t</Set>\n";
    }
    fout << "\t\t</Sets>\n";
    fout << "\t\t<Data>\n";
    for(iteratorTag t = BeginTag(); t != EndTag(); ++t)
    {
      if( *t == idx ) continue;
      if( t->GetTagName().substr(0,9) == "PROTECTED" ) continue;
      for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype)) if( t->isDefined(etype) )
      {
        fout << "\t\t\t<DataSet TagName=\"" << t->GetTagName() << "\"\n";
        fout << "\t\t\t         SetType=\"";
        switch(etype)
        {
        case NODE: fout << "Nodes"; break;
        case EDGE: fout << "Edges"; break;
        case FACE: fout << "Faces"; break;
        case CELL: fout << "Cells"; break;
        case ESET: fout << "Sets"; break;
        case MESH: fout << "Mesh"; break;
        }
        fout << "\">\n";
        fout << "\t\t\t<![CDATA[\n\t\t\t";
        int endl_count = 0;
        switch(t->GetDataType())
        {
        case DATA_REAL:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::real_array data = jt->RealArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) 
              {
                fout << data[0];
              }
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                  fout << data[q] << ",";
                fout << data.back();
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
            }
          }
          break;
        case DATA_INTEGER:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::integer_array data = jt->IntegerArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) fout << data[0];
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                  fout << data[q] << ",";
                fout << data.back();
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
              //fout << std::endl;
            }
          }
          break;
        case DATA_BULK:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::bulk_array data = jt->BulkArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) fout << CharToHex(data[0]);
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                  fout << CharToHex(data[q]) << ",";
                fout << CharToHex(data.back());
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
              //fout << std::endl;
            }
          }
          break;
        case DATA_REFERENCE:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::reference_array data = jt->ReferenceArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) 
              {
                if( data[0].isValid() )
                  fout << ReferenceToString(data.at(0),data[0].Integer(idx));
                else
                  fout << "None:0";
              }
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                {
                  if( data[q].isValid() )
                    fout << ReferenceToString(data.at(q),data[q].Integer(idx)) << ",";
                  else
                    fout << "None:0,";
                }
                if( data[data.size()-1].isValid() )
                  fout << ReferenceToString(data.at(data.size()-1),data[data.size()-1].Integer(idx));
                else
                  fout << "None:0";
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
              //fout << std::endl;
            }
          }
          break;
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
        case DATA_REMOTE_REFERENCE:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::remote_reference_array data = jt->RemoteReferenceArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) 
              {
                if( data[0].isValid() )
                  fout << data.at(0).first->GetMeshName() << ":" << ReferenceToString(data.at(0).second,data[0].Integer(idx));
                else
                  fout << ":None:0";
              }
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                {
                  if( data[q].isValid() )
                    fout << data.at(q).first->GetMeshName() << ":" << ReferenceToString(data.at(q).second,data[q].Integer(idx)) << ",";
                  else
                    fout << ":None:0,";
                }
                if( data[data.size()-1].isValid() )
                  fout << data.at(data.size()-1).first->GetMeshName() << ":" << ReferenceToString(data.at(data.size()-1).second,data[data.size()-1].Integer(idx));
                else
                  fout << ":None:0";
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
              //fout << std::endl;
            }
          }
          break;
Kirill Terekhov's avatar
Kirill Terekhov committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
#if defined(USE_AUTODIFF)
        case DATA_VARIABLE:
          for(iteratorStorage jt = Begin(etype); jt != End(); ++jt) if( jt->HaveData(*t) )
          {
            Storage::var_array data = jt->VariableArray(*t);
            if( !data.empty() )
            {
              if( t->isSparse(etype) ) 
              {
                fout << jt->Integer(idx) << " ";
                endl_count++;
              }
              if( data.size() == 1 ) fout << VariableToString(data.at(0));
              else
              {
                fout << "{";
                for(int q = 0; q < (int)data.size()-1; ++q)
                  fout << VariableToString(data.at(q)) << ",";
                fout << VariableToString(data.back());
                fout << "}";
              }
              endl_count+=data.size();
              if( endl_count > 8 )
              {
                fout << "\n\t\t\t";
                endl_count = 0;
              }
              else fout << " ";
              //fout << std::endl;
            }
          }
          break;
#endif
        }
        fout << "\n\t\t\t]]>\n";
        fout << "\t\t\t</DataSet>\n";
      }
    }
    DeleteTag(idx);
    fout << "\t\t</Data>\n";
    fout << "\t</Mesh>\n";
    fout << "</ParallelMesh>\n";
    fout.close();
  }
}

#endif