mesh_xml_file.cpp 54.6 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

namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
8
9
10
11
12
13
14
15
16

  std::string ToLower(std::string input)
  {
	  std::string ret(input);
	  for(size_t k = 0; k < ret.size(); ++k)
		  ret[k] = tolower(ret[k]);
	  return ret;
  }

Kirill Terekhov's avatar
Kirill Terekhov committed
17
18
19
20
21
22
23
  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;
24
25
26
27
28
29
    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
30
    {
31
      reader.Report("Incorrect XML tag %s expected ParallelMesh",TagParallelMesh.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
32
33
      throw BadFile;
    }
34
35

    for(int q = 0; q < TagParallelMesh.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
36
    {
37
      XMLReader::XMLAttrib & attr = TagParallelMesh.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
38
      if( ToLower(attr.name) == "number" ) nmeshes = atoi(attr.value.c_str());
39
      else reader.Report("Unused attribute for ParallelMesh %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
40
    }
41
42
    
    for(XMLReader::XMLTag TagMesh = reader.OpenTag(); !TagMesh.Finalize() && TagMesh.name == "Mesh"; reader.CloseTag(TagMesh), TagMesh = reader.OpenTag())
Kirill Terekhov's avatar
Kirill Terekhov committed
43
44
    {
      bool repair_orientation = false;
45
      for(int q = 0; q < TagMesh.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
46
      {
47
        XMLReader::XMLAttrib & attr = TagMesh.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
48
        if( ToLower(attr.name) == "repairorientation" ) repair_orientation = reader.ParseBool(attr.value);
49
        else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
50
      }
51

Kirill Terekhov's avatar
Kirill Terekhov committed
52
53
      { //Nodes
        int nnodes = 0, ndims = 3;
54
55
56
57
58
59
60
61
62
63
        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
64
        {
65
          reader.Report("Incorrect XML tag %s expected Nodes",TagNodes.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
66
67
          throw BadFile;
        }
Kirill Terekhov's avatar
Kirill Terekhov committed
68
		  bool matchnnodes = false;
69
70

        for(int q = 0; q < TagNodes.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
71
        {
72
          XMLReader::XMLAttrib & attr = TagNodes.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
73
          if( ToLower(attr.name) == "number" )
Kirill Terekhov's avatar
Kirill Terekhov committed
74
75
76
77
		  {
			  nnodes = atoi(attr.value.c_str());
			  matchnnodes = true;
		  }
Kirill Terekhov's avatar
Kirill Terekhov committed
78
          else if( ToLower(attr.name) == "dimensions" )
Kirill Terekhov's avatar
Kirill Terekhov committed
79
          {
80
81
            ndims = atoi(attr.value.c_str());
            if( GetDimensions() != ndims ) SetDimensions(ndims);
Kirill Terekhov's avatar
Kirill Terekhov committed
82
          }
83
          else reader.Report("Unused attribute for Tags %s='%s'",attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
84
85
86
87
88
        }

        new_nodes.reserve(nnodes);

        {
89
90
91
92
93
          if( reader.ReadOpenContents() )
          {
            std::vector<double> Vector;
            int Repeat;
            dynarray<Storage::real,3> xyz;
94
            for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord() )
Kirill Terekhov's avatar
Kirill Terekhov committed
95
            {
96
97
              reader.ParseReal(val,Vector,Repeat,nnodes);
              for(int l = 0; l < Repeat; ++l)
Kirill Terekhov's avatar
Kirill Terekhov committed
98
              {
99
                for(int q = 0; q < (int)Vector.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
100
                {
101
102
103
104
105
106
                  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
107
108
109
                }
              }
            }
110
            reader.ReadCloseContents();
Kirill Terekhov's avatar
Kirill Terekhov committed
111
          }
112
113
114
115
116
117
          else 
          {
            reader.Report("Cannot find contents of XML tag");
            throw BadFile;
          }

118
          reader.CloseTag(TagNodes);
Kirill Terekhov's avatar
Kirill Terekhov committed
119
120
        }

Kirill Terekhov's avatar
Kirill Terekhov committed
121
        if( matchnnodes && new_nodes.size() != nnodes )
Kirill Terekhov's avatar
Kirill Terekhov committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        {
          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;
136
137
138
139
140


          XMLReader::XMLTag TagElems;
          
          if( pass_tag )
Kirill Terekhov's avatar
Kirill Terekhov committed
141
          {
142
143
144
145
146
147
148
149
150
            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
151
152
            throw BadFile;
          }
153
154
          if( TagElems.name != "Cells" ) repeat = true; else repeat = false;
          if( TagElems.name == "Cells" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
155
156
157
158
          {
            elems = &new_cells;
            curtype = CELL;
          }
159
          else if( TagElems.name == "Faces" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
160
161
162
163
          {
            elems = &new_faces;
            curtype = FACE;
          }
164
          else if( TagElems.name == "Edges" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
165
166
167
168
          {
            elems = &new_edges;
            curtype = EDGE;
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
169
          
170
171

          for(int q = 0; q < TagElems.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
172
          {
173
            XMLReader::XMLAttrib & attr = TagElems.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
174
            if( ToLower(attr.name) == "number" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
175
176
177
178
179
            {
                nelems = atoi(attr.value.c_str());
                matchelems = true;
                //std::cout << "nelems: " << nelems << " text " << attr.value.c_str() << std::endl;
            }
180
            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
181
          } 
182
          
Kirill Terekhov's avatar
Kirill Terekhov committed
183
184
185
186
187
188
189
190
191
192
          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]
          };

193
194
195
          
          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
196
          {
Kirill Terekhov's avatar
Kirill Terekhov committed
197
198
			  bool matchnconns = false;
			  int nexpectconns = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
199
200
            int nconns = 0;
            int offset = 0;
201
			int dims = 3; //to distinguish 3d cells from 2d cells when they are created with nodes
Kirill Terekhov's avatar
Kirill Terekhov committed
202
            ElementType subtype = NODE;
203
            for(int q = 0; q < TagConns.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
204
            {
205
              XMLReader::XMLAttrib & attr = TagConns.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
206
              if( ToLower(attr.name) == "number" )
Kirill Terekhov's avatar
Kirill Terekhov committed
207
208
209
210
			  {
				  nexpectconns = atoi(attr.value.c_str());
				  matchnconns = true;
			  }
Kirill Terekhov's avatar
Kirill Terekhov committed
211
212
213
			  else if( ToLower(attr.name) == "dimensions" ) dims = atoi(attr.value.c_str());
              else if( ToLower(attr.name) == "type" ) subtype = reader.atoes(attr.value.c_str());
              else if( ToLower(attr.name) == "offset" ) offset = atoi(attr.value.c_str());
214
              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
215
            }
216
            
Kirill Terekhov's avatar
Kirill Terekhov committed
217
            //ntotconns += nconns;
Kirill Terekhov's avatar
Kirill Terekhov committed
218
219
220
221
222
223
224
            if( subtype >= curtype )
            {
              reader.Report("%ss cannot be constructed from %ss",ElementTypeName(curtype),ElementTypeName(subtype));
              throw BadFile;
            }
            reader.ReadOpenContents();
            ElementArray<Element> subarr(this);
225
            for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
            {
              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 )
244
245
246
247
248
				{
					Face f = CreateFace(subarr.Convert<Node>()).first;
					if( repair_orientation ) f.FixEdgeOrder();
					elems->push_back(f.GetHandle());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
249
                else if( subtype == EDGE )
250
				{
251
					Face f = CreateFace(subarr.Convert<Edge>()).first;
252
253
254
					if( repair_orientation ) f.FixEdgeOrder();
					elems->push_back(f.GetHandle());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
255
256
257
258
                break;
              case CELL:
                if( subtype == NODE )
                {
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
					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
315
316
317
318
319
320
                }
                else if( subtype == EDGE )
                {
                  reader.Report("Sorry, no rule to convert %d edges into a cell",subarr.size());
                  throw BadFile;
                }
321
				else if( subtype == FACE )
322
323
				{
					Cell c = CreateCell(subarr.Convert<Face>()).first;
Kirill Terekhov's avatar
Kirill Terekhov committed
324
					if( repair_orientation ) c.FixEdgeOrder();
325
326
					elems->push_back(c.GetHandle());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
327
328
                break;
              }
Kirill Terekhov's avatar
Kirill Terekhov committed
329
				nconns++;
Kirill Terekhov's avatar
Kirill Terekhov committed
330
331
            }
            reader.ReadCloseContents();
Kirill Terekhov's avatar
Kirill Terekhov committed
332
333
334
335
336
			
			  ntotconns += nconns;
			  if( matchnconns && nconns != nexpectconns )
				  reader.Report("Number %d of elements encountered do not match to the specified number %d",nconns,nexpectconns);
          }
Kirill Terekhov's avatar
Kirill Terekhov committed
337
338
339

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

340
341
342
343
344
345
346
347
          reader.CloseTag(TagElems);

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

Kirill Terekhov's avatar
Kirill Terekhov committed
348
349
350
351
352
353
354
        } while(repeat);
      }

      { //Sets and Data
        bool repeat = false;
        do
        {
355
356
357
358
359
360
361
362
          XMLReader::XMLTag TagSetsData;
          if( pass_tag )
          {
            TagSetsData = PassTag;
            pass_tag = false;
          }
          else TagSetsData = reader.OpenTag();
          
363
364
365
366
367
368
369
370
		  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);
Kirill Terekhov's avatar
Kirill Terekhov committed
371
				  if( ToLower(attr.name) == "number" ) 
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
				  {
					  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);
Kirill Terekhov's avatar
Kirill Terekhov committed
394
395
					  if( ToLower(attr.name) == "name" ) tagname = attr.value;
					  else if( ToLower(attr.name) == "size" ) 
396
					  {
Kirill Terekhov's avatar
Kirill Terekhov committed
397
						  if( ToLower(attr.value) == "variable" )
398
399
400
							  size = ENUMUNDEF;
						  else size = atoi(attr.value.c_str());
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
401
					  else if( ToLower(attr.name) == "type" )
402
					  {
Kirill Terekhov's avatar
Kirill Terekhov committed
403
404
405
406
407
						  if( ToLower(attr.value) == "real" ) type = DATA_REAL;
						  else if( ToLower(attr.value) == "integer" ) type = DATA_INTEGER;
						  else if( ToLower(attr.value) == "reference" ) type = DATA_REFERENCE;
						  else if( ToLower(attr.value) == "remoteReference" ) type = DATA_REMOTE_REFERENCE;
						  else if( ToLower(attr.value) == "bulk" ) type = DATA_BULK;
408
#if defined(USE_AUTODIFF)
Kirill Terekhov's avatar
Kirill Terekhov committed
409
						  else if( ToLower(attr.value) == "variable" ) type = DATA_VARIABLE;
410
411
#endif
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
412
					  else if( ToLower(attr.name) == "sparse" )
413
414
415
416
					  { 
						  reader.ParseCommaSeparated(attr.value,parsed);
						  for(int q = 0; q < (int)parsed.size(); ++q) sparse |= reader.atoes(parsed[q].c_str());
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
417
					  else if( ToLower(attr.name) == "definition" )
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
					  {
						  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
441
442
443
444
445
446
447
448
449
450
451
452
          {
            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;
453
            for(int q = 0; q < TagSetsData.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
454
            {
455
              XMLReader::XMLAttrib & attr = TagSetsData.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
456
              if( ToLower(attr.name) == "number" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
457
              {
458
                nsets = atoi(attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
459
460
                matchsets = true;
              }
Kirill Terekhov's avatar
Kirill Terekhov committed
461
              else if( ToLower(attr.name) == "offset" ) sets_offset = atoi(attr.value.c_str());
462
              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
463
464
            }
            new_sets.reserve(nsets);
465
466
467
            
            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
468
            {
Kirill Terekhov's avatar
Kirill Terekhov committed
469
470
				int expectsize = 0;
				bool matchsize = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
471
472
473
474
475
              int size = 0, offset = 0;
              std::string name;
              HandleType parent = InvalidHandle(), child = InvalidHandle(), sibling = InvalidHandle();
              ElementSet::ComparatorType comparator = ElementSet::UNSORTED_COMPARATOR;
              nsets_read++;
476
              for(int q = 0; q < Set.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
477
              {
478
                XMLReader::XMLAttrib & attr = Set.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
479
                if( ToLower(attr.name) == "size" )
Kirill Terekhov's avatar
Kirill Terekhov committed
480
481
482
483
				{
					expectsize = atoi(attr.value.c_str());
					matchsize = true;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
484
485
486
                else if( ToLower(attr.name) == "offset" ) offset = atoi(attr.value.c_str());
                else if( ToLower(attr.name) == "name" ) name = attr.value;
                else if( ToLower(attr.name) == "parent" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
487
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
488
                  if( ToLower(attr.value) != "unset" )
489
                    parent = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
490
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
491
                else if( ToLower(attr.name) == "sibling" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
492
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
493
                  if( ToLower(attr.value) != "unset" )
494
                    sibling = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
495
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
496
                else if( ToLower(attr.name) == "child" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
497
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
498
                  if( ToLower(attr.value) != "unset" )
499
                    child = ComposeHandle(ESET,atoi(attr.value.c_str()));
Kirill Terekhov's avatar
Kirill Terekhov committed
500
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
501
                else if( ToLower(attr.name) == "comparator" )
Kirill Terekhov's avatar
Kirill Terekhov committed
502
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
503
504
505
506
507
                  if( ToLower(attr.value) == "unsorted" ) comparator = ElementSet::UNSORTED_COMPARATOR;
                  else if( ToLower(attr.value) == "identificator" ) comparator = ElementSet::GLOBALID_COMPARATOR;
                  else if( ToLower(attr.value) == "centroid" ) comparator = ElementSet::CENTROID_COMPARATOR;
                  else if( ToLower(attr.value) == "hierarchy" ) comparator = ElementSet::HIERARCHY_COMPARATOR;
                  else if( ToLower(attr.value) == "handle" ) comparator = ElementSet::HANDLE_COMPARATOR;
508
                  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
509
                }
510
                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
511
512
513
514
515
516
517
518
519
520
521
522
523
              } 
              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();
524
              for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
525
526
527
              {
                std::vector<std::pair<ElementType,int> > Vector;
                int Repeat;
Kirill Terekhov's avatar
Kirill Terekhov committed
528
                reader.ParseReference(val,Vector,Repeat,0);
Kirill Terekhov's avatar
Kirill Terekhov committed
529
530
531
532
533
534
535
536
537
538
                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());
Kirill Terekhov's avatar
Kirill Terekhov committed
539
					size++;
Kirill Terekhov's avatar
Kirill Terekhov committed
540
541
542
543
544
545
                  }
                    //s.PutElement(links[ElementNum(Vector[q].first)][Vector[q].second-offset]);
                  //s.PutElements(Vector[0],(enumerator)Vector.size());
                }
              }
              reader.ReadCloseContents();
Kirill Terekhov's avatar
Kirill Terekhov committed
546
547
				
				if( matchsize && size != expectsize ) reader.Report("Number %d of elements encountered do not match to the specified set size %d",size, expectsize);
Kirill Terekhov's avatar
Kirill Terekhov committed
548
            }
549
550
551
552
553
554
555
556
            
            reader.CloseTag(TagSetsData);
            if( !Set.Finalize() )
            {
              PassTag = Set;
              pass_tag = true;
            }
            
Kirill Terekhov's avatar
Kirill Terekhov committed
557
558
559
            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
560
            for(int q = 0; q < (int)new_sets.size(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
561
562
563
564
565
566
567
568
569
570
571
572
573
574
            {
              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]);
            }
          }
575
          else if( TagSetsData.name == "Data" )
Kirill Terekhov's avatar
Kirill Terekhov committed
576
577
578
579
580
581
582
583
584
585
586
587
          {
            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;
588
589

            for(int q = 0; q < TagSetsData.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
590
            {
591
              XMLReader::XMLAttrib & attr = TagSetsData.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
592
              if( ToLower(attr.name) == "number" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
593
              {
594
                ndata = atoi(attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
595
596
                matchndata = true;
              }
597
              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
598
599
            }

600
601
            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
602
603
604
            {
              ++ndatapass;
              int sparse_read = 2, offset = 0;
605
606
              std::string tagname = "", setname = "", meshname = "";
              Mesh * remote_mesh = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
607
              ElementType etype = NONE;
608
              for(int q = 0; q < TagDataSet.NumAttrib(); ++q)
Kirill Terekhov's avatar
Kirill Terekhov committed
609
              {
610
                XMLReader::XMLAttrib & attr = TagDataSet.GetAttib(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
611
                if( ToLower(attr.name) == "settype" ) 
Kirill Terekhov's avatar
Kirill Terekhov committed
612
                {
Kirill Terekhov's avatar
Kirill Terekhov committed
613
614
615
616
617
618
619
                  if( ToLower(attr.value) == "cells" ) etype = CELL;
                  else if( ToLower(attr.value) == "faces" ) etype = FACE;
                  else if( ToLower(attr.value) == "edges" ) etype = EDGE;
                  else if( ToLower(attr.value) == "nodes" ) etype = NODE;
                  else if( ToLower(attr.value) == "sets" ) etype = ESET;
                  else if( ToLower(attr.value) == "mesh" ) etype = MESH;
                  else if( ToLower(attr.value) == "setdata" ) etype = NONE;
620
				  else reader.Report("Cannot understand attribute value for %s %s='%s'",TagDataSet.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
621
                }
Kirill Terekhov's avatar
Kirill Terekhov committed
622
623
624
625
626
                else if( ToLower(attr.name) == "tagname" ) tagname = attr.value;
                else if( ToLower(attr.name) == "setname" ) setname = attr.value;
                else if( ToLower(attr.name) == "meshname" ) meshname = attr.value;
                else if( ToLower(attr.name) == "sparse" ) sparse_read = reader.ParseBool(attr.value);
                else if( ToLower(attr.name) == "offset" ) offset = atoi(attr.value.c_str());
627
                else reader.Report("Unused attribute for %s %s='%s'",TagDataSet.name.c_str(),attr.name.c_str(),attr.value.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
628
              }
629
              
Kirill Terekhov's avatar
Kirill Terekhov committed
630
631
632
633
634
635
636
637
638
639
              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;
              }
640
641
642
643
644
645
              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
646
647
648
649
650
651
652
653
654
655
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
684
685
686
687
688
689
690
691
692

              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;
693
694
              reader.ReadOpenContents();
              for(std::string val = reader.GetContentsWord(); !reader.isContentsEnded(); val = reader.GetContentsWord())
Kirill Terekhov's avatar
Kirill Terekhov committed
695
696
697
              {
                if( sparse_read )
                {
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
				  int offset;
				  if( etype == ESET && ((val[0]=='\'' && val[val.size()-1] == '\'') || (val[0] == '"' && val[1] == '"')) )
				  {
					  //helps check that the set was not found
					  offset = -1;
					  //strip qoutes
					  val = val.substr(1,val.size()-2);
					  //find set with provided name
					  for(enumerator k = 0; k < set_size; ++k)
						  if( ElementSet(this,set_elems[k])->GetName() == val )
						  {
							  offset = k;
							  break;
						  }
					  if( offset == -1 )
					  {
						  reader.Report("Set with name %s was not found",val.c_str());
						  throw BadFile;
					  }
				  }
				  else offset = atoi(val.c_str());
                  it = set_elems + offset;
Kirill Terekhov's avatar
Kirill Terekhov committed
720
721
722
723
724
725
726
727
728
                  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);
Kirill Terekhov's avatar
Kirill Terekhov committed
729
					if( t.GetSize() == ENUMUNDEF ) data.resize((enumerator)Vector.size()*Repeat);
Kirill Terekhov's avatar
Kirill Terekhov committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
                    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
744
                          if( ((int)(it-set_elems)) < (int)set_size ) data = RealArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
745
746
747
                        }
                      }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
748
749
750
751
752
					if( !sparse_read && t.GetSize() == ENUMUNDEF )
					{
						++it;
						if( ((int)(it-set_elems)) < (int)set_size ) data = RealArray(*it,t);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
753
754
755
756
757
758
759
760
                  }
                  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);
761
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
762
763
764
765
766
767
768
769
770
771
772
773
                    {
                      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
774
                          if( ((int)(it-set_elems)) < (int)set_size ) data = IntegerArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
775
776
777
                        }
                      }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
778
779
780
781
782
					if( !sparse_read && t.GetSize() == ENUMUNDEF )
					{
						++it;
						if( ((int)(it-set_elems)) < (int)set_size ) data = IntegerArray(*it,t);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
783
784
785
786
787
788
789
790
                  }
                  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);
791
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
792
793
794
795
796
797
798
799
800
801
802
803
                    {
                      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
804
                          if( ((int)(it-set_elems)) < (int)set_size ) data = BulkArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
805
806
807
                        }
                      }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
808
809
810
811
812
					  if( !sparse_read && t.GetSize() == ENUMUNDEF )
					  {
						  ++it;
						  if( ((int)(it-set_elems)) < (int)set_size ) data = BulkArray(*it,t);
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
813
814
815
816
817
818
819
820
                  }
                  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);
821
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read)
Kirill Terekhov's avatar
Kirill Terekhov committed
822
823
824
825
826
827
828
829
830
831
832
833
                    {
                      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
834
                          if( ((int)(it-set_elems)) < (int)set_size ) data = ReferenceArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
835
836
837
                        }
                      }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
838
839
840
841
842
					  if( !sparse_read && t.GetSize() == ENUMUNDEF )
					  {
						  ++it;
						  if( ((int)(it-set_elems)) < (int)set_size ) data = ReferenceArray(*it,t);
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
843
844
                  }
                  break;
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
                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
867
                            if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
868
869
870
                          }
                        }
                      }
Kirill Terekhov's avatar
Kirill Terekhov committed
871
872
873
874
875
						if( !sparse_read && t.GetSize() == ENUMUNDEF )
						{
							++it;
							if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
						}
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
                    }
                    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);
892
                          if( m == NULL ) reader.Report("Cannot find remote mesh %s, you should create it first inside of your application",Vector[q].first.c_str());
893
894
895
896
897
                          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
898
                            if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
899
900
901
                          }
                        }
                      }
Kirill Terekhov's avatar
Kirill Terekhov committed
902
903
904
905
906
						if( !sparse_read && t.GetSize() == ENUMUNDEF )
						{
							++it;
							if( ((int)(it-set_elems)) < (int)set_size ) data = RemoteReferenceArray(*it,t);
						}
907
908
909
                    }
                  }
                  break;
Kirill Terekhov's avatar
Kirill Terekhov committed
910
911
912
913
914
915
916
#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);
917
                    else if( t.GetSize() != Vector.size()*Repeat && sparse_read )
Kirill Terekhov's avatar
Kirill Terekhov committed
918
919
920
921
922
923
924
925
926
927
928
929
                    {
                      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
930
                          if( ((int)(it-set_elems)) < (int)set_size ) data = VariableArray(*it,t);
Kirill Terekhov's avatar
Kirill Terekhov committed
931
932
933
                        }
                      }
                    }
Kirill Terekhov's avatar
Kirill Terekhov committed
934
935
936
937
938
					  if( !sparse_read && t.GetSize() == ENUMUNDEF )
					  {
						  ++it;
						  if( ((int)(it-set_elems)) < (int)set_size ) data = VariableArray(*it,t);
					  }
Kirill Terekhov's avatar
Kirill Terekhov committed
939
940
941
942
943
944
945
946
947
948
                  }
                  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);

949
            reader.CloseTag(TagSetsData);
Kirill Terekhov's avatar
Kirill Terekhov committed
950

951
952
953
954
955
            if( !TagDataSet.Finalize() )
            {
              PassTag = TagDataSet;
              pass_tag = true;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
956
          }
957
958
		  else if( TagSetsData.name == "" )
			  repeat = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
959
960
          else
          {
961
            reader.Report("Unexpected tag %s, expected Tags or Sets or Data",TagSetsData.name.c_str());
Kirill Terekhov's avatar
Kirill Terekhov committed
962
963
964
965
            throw BadFile;
          }
        } while(repeat);
      }
966
967
      RepairGeometricTags();

Kirill Terekhov's avatar
Kirill Terekhov committed
968
969
970
971
972
973
974
975
      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;
      }
    }
976
    reader.CloseTag(TagParallelMesh);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
    infile.close();
  }

  void Mesh::SaveXML(std::string File)
  {
    std::fstream fout(File.c_str(),std::ios::out);
    fout << "<ParallelMesh>\n";
    fout << "\t<Mesh>\n";
    Tag idx = CreateTag("TEMPORARY_XML_ENUMERATOR",DATA_INTEGER,MESH|ESET|CELL|FACE|EDGE|NODE,NONE,1);
    Integer(GetHandle(),idx) = 0;
    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";
Kirill Terekhov's avatar
Kirill Terekhov committed
1035
    fout << "\t\t\t<Connections Number=\"" << NumberOfCells() << "\" Type=\"Faces\">\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
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
    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";
Kirill Terekhov's avatar
Kirill Terekhov committed
1099
1100
1101
	fout << "\t\t<Tags>\n";
    for(int k = 0; k < (int)tags.size(); ++k)
    {
Kirill Terekhov's avatar
Kirill Terekhov committed
1102
	  if( tags[k] == idx ) continue;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
      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;
      case DATA_REMOTE_REFERENCE: type = "RemoteReference"; break;
#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)] + ",";
      }
      if( !definition.empty() ) definition.resize(definition.size()-1); //remove trailing comma
      if( !sparse.empty() ) sparse.resize(sparse.size()-1); //remove trailing comma
      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";
    }
	fout << "\t\t</Tags>\n";
Kirill Terekhov's avatar
Kirill Terekhov committed
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
    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 << " ";
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1187
1188
			else if( !t->isSparse(etype) ) fout << " { } ";

Kirill Terekhov's avatar
Kirill Terekhov committed
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
          }
          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;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1220
			else if( !t->isSparse(etype) ) fout << " { } ";
Kirill Terekhov's avatar
Kirill Terekhov committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
          }
          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;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1252
			else if( !t->isSparse(etype) ) fout << " { } ";
Kirill Terekhov's avatar
Kirill Terekhov committed
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
1292
1293
1294
1295
1296
1297
          }
          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;
            }
Kirill Terekhov's avatar
Kirill Terekhov committed
1298
			else if( !t->isSparse(etype) ) fout << " { } "