mesh_file.cpp 98.6 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "inmost.h"

#if defined(USE_MESH)
#include <deque>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>

#define R_VERSION     0
#define R_USERDATA    1
#define R_DATATYPE    2
#define R_WAITDATA    3

#define R_SGRID       4
#define R_RGRID       5
#define R_UGRID       6
#define R_POLYDATA    7
#define R_FIELD       8
#define R_SPOINTS     9

#define R_ATTRIBUTES  10
#define R_ATTRDATA    11

#define R_QUIT		  100


template<typename T>
void ReadCoords(FILE * f,INMOST_DATA_REAL_TYPE c[3])
{
	T temp[3];
	if( fread(temp,sizeof(T),3,f) == 3 )
	{
		c[0] = temp[0];
		c[1] = temp[1];
		c[2] = temp[2];
	}
	else throw INMOST::BadFile;
}

#if defined(USE_PARALLEL_WRITE_TIME)
#define REPORT_MPI(x) {WriteTab(out_time) << "<MPI><![CDATA[" << #x << "]]></MPI>\n"; x;}
#define REPORT_STR(x) {WriteTab(out_time) << "<TEXT><![CDATA[" << x << "]]></TEXT>\n";}
#define REPORT_VAL(str,x) {WriteTab(out_time) << "<VALUE name=\"" << str << "\"> <CONTENT><![CDATA[" << x << "]]> </CONTENT><CODE><![CDATA[" << #x << "]]></CODE></VALUE>\n";}
#define ENTER_FUNC() long double all_time = Timer(); WriteTab(out_time) << "<FUNCTION name=\"" << __FUNCTION__ << "\" id=\"func" << func_id++ << "\">\n"; Enter();
#define EXIT_FUNC() WriteTab(out_time) << "<TIME>" << Timer() - all_time << "</TIME>\n"; Exit(); WriteTab(out_time) << "</FUNCTION>\n";
#else
#define REPORT_MPI(x) x
#define REPORT_STR(x) 
#define REPORT_VAL(str,x)
#define ENTER_FUNC()
#define EXIT_FUNC() 
#endif



#define GMV_HEADER 0
#define GMV_NODES 1
#define GMV

namespace INMOST
{


	typedef char HeaderType;
	const HeaderType EndOfData  = 0x01;
	const HeaderType NodeHeader = 0x02;
	const HeaderType EdgeHeader = 0x03;
	const HeaderType FaceHeader = 0x04;
	const HeaderType CellHeader = 0x05;
	const HeaderType ESetHeader = 0x06;
	const HeaderType TagsHeader = 0x07;
	const HeaderType MeshHeader = 0x08;
	const HeaderType EoMHeader  = 0x09;
	const HeaderType INMOSTFile   = 0x10;
	const HeaderType MeshDataHeader = 0x11;

	
	std::ostream & operator <<(std::ostream & out, HeaderType H)
	{
		out.put(H);
		return out;
	}
	
	std::istream & operator >>(std::istream & in, HeaderType &H)
	{
		in.get(H);
		return in;
	}

	void Mesh::WriteTag(std::ostream & out, Tag X)
	{
		std::string name = X.GetTagName();
		INMOST_DATA_ENUM_TYPE namesize = name.size();
		INMOST_DATA_BULK_TYPE datatype = static_cast<INMOST_DATA_BULK_TYPE>(X.GetDataType());
		ElementType sparsemask = NONE;
		ElementType definedmask = NONE;
		INMOST_DATA_ENUM_TYPE datalength = X.GetSize();
		for(ElementType current_type = NODE; current_type <= MESH; current_type = current_type << 1)
		{
			if( X.isSparse(current_type) ) sparsemask |= current_type;
			if( X.isDefined(current_type) ) definedmask |= current_type;
		}
		uconv.write_iValue(out,namesize);
		out.write(name.c_str(), name.size());
		out.put(datatype);
		out.put(sparsemask);
		out.put(definedmask);
		uconv.write_iValue(out,datalength);
	}
	
	Tag Mesh::ReadTag(std::istream & in)
	{
		INMOST_DATA_ENUM_TYPE namesize;
		std::string name;
		char datatype;
		char sparsemask,definedmask;
		INMOST_DATA_ENUM_TYPE datalength;
		uconv.read_iValue(in,namesize);
		name.resize(namesize);
		in.read(reinterpret_cast<char *>(&name[0]), namesize);
		in.get(datatype);
		in.get(sparsemask);
		in.get(definedmask);
		uconv.read_iValue(in,datalength);
		return CreateTag(name,static_cast<DataType>(datatype),static_cast<ElementType>(definedmask),static_cast<ElementType>(sparsemask),datalength);
	}
	void Mesh::ReadData(std::istream & in, Tag t, Storage * X,std::vector<Node *> & new_nodes,std::vector<Edge *> & new_edges,std::vector<Face *> & new_faces,std::vector<Cell *> & new_cells)
	{
		
		if( !t.isDefined(X->GetElementType()) ) return;
		//if( t.isSparse(X->GetElementType()) )
		//{
		//	char have_data;
		//	in.get(have_data);
		//	if(!have_data) return;
		//}
		INMOST_DATA_ENUM_TYPE size = t.GetSize(),k,lid;
		if( size == ENUMUNDEF )
		{
			uconv.read_iValue(in,size);
			X->SetDataSize(t,size);
		}
		switch(t.GetDataType())
		{
		case DATA_REAL:      
			{
			//	INMOST_DATA_REAL_TYPE stub;
				for(k = 0; k < size; k++) 
					iconv.read_fValue(in,X->RealArray(t)[k]); 
			//			iconv.read_fValue(in,stub); 
			}
			break;
		case DATA_INTEGER:   
			{
				for(k = 0; k < size; k++) 
					iconv.read_iValue(in,X->IntegerArray(t)[k]); 
			}
			break;
		case DATA_BULK:      
			{
				in.read(reinterpret_cast<char *>(&X->Bulk(t)),size); 
			}
			break;
		case DATA_REFERENCE: 
			{
				for(k = 0; k < size; k++)
				{
					char type;
					in.get(type);
Kirill Terekhov's avatar
Kirill Terekhov committed
171
					if (type != NONE)
Kirill Terekhov's avatar
Kirill Terekhov committed
172
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
173
174
175
176
177
178
179
180
						uconv.read_iValue(in, lid);
						switch (static_cast<ElementType>(type))
						{
						case NODE: X->ReferenceArray(t)[k] = new_nodes[lid]; break;
						case EDGE: X->ReferenceArray(t)[k] = new_edges[lid]; break;
						case FACE: X->ReferenceArray(t)[k] = new_faces[lid]; break;
						case CELL: X->ReferenceArray(t)[k] = new_cells[lid]; break;
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
181
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
182
					else X->ReferenceArray(t)[k] = NULL;
Kirill Terekhov's avatar
Kirill Terekhov committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
				}
			}
			break;
		}
	}
	void Mesh::WriteData(std::ostream & out, Tag t, Storage & X)
	{
		if( !t.isDefined(X.GetElementType()) ) return;
		//if( t.isSparse(X.GetElementType()) )
		//{
		//	char have_data = X.HaveData(t);
		//	out.put(have_data);
		//	if(!have_data) return;
		//}
		INMOST_DATA_ENUM_TYPE size = t.GetSize(),k, lid;
		if( size == ENUMUNDEF )
		{
			size = X.GetDataSize(t);
			uconv.write_iValue(out,size);
		}
		switch(t.GetDataType())
		{
		case DATA_REAL:      for(k = 0; k < size; k++) iconv.write_fValue(out,X.RealArray(t)[k]); break;
		case DATA_INTEGER:   for(k = 0; k < size; k++) iconv.write_iValue(out,X.IntegerArray(t)[k]); break;
		case DATA_BULK:      out.write(reinterpret_cast<char *>(&X.Bulk(t)),size); break;
		case DATA_REFERENCE: 
			{
				for(k = 0; k < size; k++)
				{
					Element * e = X.ReferenceArray(t)[k];
Kirill Terekhov's avatar
Kirill Terekhov committed
213
214
215
216
217
218
219
220
221
222
223
					if (e != NULL)
					{
						char type = e->GetElementType();
						lid = e->LocalID();
						out.put(type);
						uconv.write_iValue(out, lid);
					}
					else
					{
						out.put(NONE);
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
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
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
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
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
				}
			}
		break;
		}
	}
	Node * Mesh::ReadNode(std::istream & in, std::vector<Node *> & old_nodes)
	{
		Node * ret;
		unsigned int dim = GetDimensions();
		Storage::real coords[3];
		for(unsigned int i = 0; i < dim; i++)
			iconv.read_fValue(in,coords[i]);
		int find = -1;
		if( !old_nodes.empty() ) find = binary_search(&old_nodes[0],old_nodes.size(),sizeof(Node *),CompareCoordSearch,&coords[0]);
		if( find == -1 ) 
			ret = CreateNode(&coords[0]);
		else 
			ret = old_nodes[find];
		MIDType markers;
		uconv.read_iValue(in,markers);
		ret->SetMarkerSpace(markers);
		return ret;
	}
	void Mesh::WriteNode(std::ostream & out,Node * X)
	{
		Storage::real_array coords = X->Coords();
		for(Storage::real_array::iterator it = coords.begin(); it != coords.end(); it++)
			iconv.write_fValue(out,*it);
		uconv.write_iValue(out,X->GetMarkerSpace());
	}

	Edge * Mesh::ReadEdge(std::istream & in, std::vector<Node *> & nodes)
	{
		Edge * ret;
		INMOST_DATA_ENUM_TYPE nlow,lid,i;
		dynarray<Node *,64> sub_elements;
		uconv.read_iValue(in,nlow);
		//assert(nlow == 2);
		for(i = 0; i < nlow; i++)
		{
			uconv.read_iValue(in,lid);
			sub_elements.push_back(nodes[lid]);
		}
		ret = CreateEdge(&sub_elements[0], sub_elements.size()).first;
		MIDType markers;
		uconv.read_iValue(in,markers);
		ret->SetMarkerSpace(markers);
		return ret;	
	}
	void Mesh::WriteEdge(std::ostream & out,Edge * X)
	{
		adjacent<Node> sub = X->getNodes();
		INMOST_DATA_ENUM_TYPE nlow =sub.size(),lid;
		assert(nlow == 2 );
		uconv.write_iValue(out,nlow);
		for(adjacent<Node>::iterator it = sub.begin(); it != sub.end(); it++)
		{
			lid = it->LocalID();
			uconv.write_iValue(out,lid);
		}
		uconv.write_iValue(out,X->GetMarkerSpace());
	}
	
	Face * Mesh::ReadFace(std::istream & in, std::vector<Edge *> & edges)
	{
		Face * ret;
		INMOST_DATA_ENUM_TYPE nlow,lid,i;
		dynarray<Edge *,64> sub_elements;
		uconv.read_iValue(in,nlow);
		for(i = 0; i < nlow; i++)
		{
			uconv.read_iValue(in,lid);
			sub_elements.push_back(edges[lid]);
		}
		ret = CreateFace(&sub_elements[0], sub_elements.size()).first;
		MIDType markers;
		uconv.read_iValue(in,markers);
		ret->SetMarkerSpace(markers);
		return ret;	
	}
	void Mesh::WriteFace(std::ostream & out,Face * X)
	{
		adjacent<Edge> sub = X->getEdges();
		INMOST_DATA_ENUM_TYPE nlow =sub.size(),lid;
		uconv.write_iValue(out,nlow);
		for(adjacent<Node>::iterator it = sub.begin(); it != sub.end(); it++)
		{
			lid = it->LocalID();
			uconv.write_iValue(out,lid);
		}
		uconv.write_iValue(out,X->GetMarkerSpace());
	}

	Cell * Mesh::ReadCell(std::istream & in, std::vector<Face *> & faces, std::vector<Node *> & nodes)
	{
		Cell * ret = NULL;
		INMOST_DATA_ENUM_TYPE nlow, nhigh,lid,i;
		dynarray<Face *,64> sub_elements;
		dynarray<Node *,64> suggest_nodes;
		uconv.read_iValue(in,nlow);
		for(i = 0; i < nlow; i++)
		{
			uconv.read_iValue(in,lid);
			sub_elements.push_back(faces[lid]);
		}
		uconv.read_iValue(in,nhigh);
		for(i = 0; i < nhigh; i++)
		{
			uconv.read_iValue(in,lid);
			suggest_nodes.push_back(nodes[lid]);
		}
		ret = CreateCell(&sub_elements[0],sub_elements.size(),&suggest_nodes[0],suggest_nodes.size()).first;
		MIDType markers;
		uconv.read_iValue(in,markers);
		ret->SetMarkerSpace(markers);
		return ret;	
	}
	void Mesh::WriteCell(std::ostream & out,Cell * X)
	{
		adjacent<Node> nodes = X->getNodes();
		adjacent<Face> sub = X->getFaces();
		INMOST_DATA_ENUM_TYPE nlow = sub.size(), nhigh = nodes.size(),lid;
		uconv.write_iValue(out,nlow);
		for(adjacent<Face>::iterator it = sub.begin(); it != sub.end(); it++)
		{
			lid = it->LocalID();
			uconv.write_iValue(out,lid);
		}
		uconv.write_iValue(out,nhigh);
		for(adjacent<Node>::iterator it = nodes.begin(); it != nodes.end(); it++)
		{
			lid = it->LocalID();
			uconv.write_iValue(out,lid);
		}
		uconv.write_iValue(out,X->GetMarkerSpace());
	}
	
	void Mesh::WriteElementSet(std::ostream & out, ElementSet * X)
	{
		INMOST_DATA_ENUM_TYPE size = X->size(),lid;
		char ordered = X->isOrdered();
		uconv.write_iValue(out,size);
		out.put(ordered);
		for(ElementSet::iterator it = X->begin(); it != X->end(); it++)
		{
			char type = static_cast<char>(it->GetElementType());
			lid = it->LocalID();
			out.put(type);
			uconv.write_iValue(out,lid);
		}
		uconv.write_iValue(out,X->GetMarkerSpace());
	}
	

	
	ElementSet * Mesh::ReadElementSet(std::istream & in,std::vector<Node *> & new_nodes,std::vector<Edge *> & new_edges,std::vector<Face *> & new_faces,std::vector<Cell *> & new_cells)
	{
		INMOST_DATA_ENUM_TYPE size, lid, it;
		char ordered;
		uconv.read_iValue(in,size);
		in.get(ordered);
		ElementSet * ret = ordered? CreateOrderedSet() : CreateSet();
		for(it = 0; it < size; it++)
		{
			char type;
			in.get(type);
			uconv.read_iValue(in,lid);
			switch(static_cast<ElementType>(type))
			{
				case NODE: ret->Insert(new_nodes[lid]); break;
				case EDGE: ret->Insert(new_edges[lid]); break;
				case FACE: ret->Insert(new_faces[lid]); break;
				case CELL: ret->Insert(new_cells[lid]); break;
			}
		}
		MIDType markers;
		uconv.read_iValue(in,markers);
		ret->SetMarkerSpace(markers);
		return ret;
	}



	void Mesh::Load(std::string File)
	{
		ENTER_FUNC();
		REPORT_VAL("File",File);
		std::string LFile;
		LFile.resize(File.size());
		std::transform(File.begin(),File.end(),LFile.begin(),::tolower);
//		long double load_timer = Timer();
		/*
		if(File.find("gmv") != std::string::npos) //this is gmv
		{
			char readline[2048];
			std::vector<File *> fs;
			fs.push_back(fopen(File.c_str(),"r"));
			if( fs[0] == NULL ) throw BadFileName;
			while( !fs.empty() )
			{
				while( fgets(readline,2048,fs[fs.size()-1]) != NULL )
				{
					switch(state)
					{
						case 
					}
				}
				fclose(fs[fs.size()-1]);
				fs.pop();
			}
		}
		else 
		*/
		if(LFile.find(".grdecl") != std::string::npos) // this is eclipse grid
		{
		
			Storage::real origin[2], axisx[2], axisy[2];
			std::vector<Cell *> new_cells;
			Storage::integer nx,ny,nz, i , j , k, m, q;
			std::vector< std::vector< std::vector< std::vector<Storage::real> > > > coords, zcorn;
			std::vector< std::vector< std::vector<Storage::integer> > > pinch;
			size_t l;
			std::string str;
			std::fstream stream(File.c_str(),std::ios::in);
			while(stream.good())
			{
				getline(stream,str);
				l = str.find_first_not_of(" \t\r\n");
				if( l != std::string::npos ) str.substr(l,str.size()-l).swap(str);
				else str.clear();
				l = str.find_last_not_of(" \t\r\n");
				if( l != std::string::npos ) str.resize(l+1);
				else str.clear();
				
				if( str.empty() ) continue;
				if( str[0] == '-' && str[1] == '-' ) continue;
				
				if( str == "NOECHO" ) continue;
				else if( str == "PINCH" )
				{
					getline(stream,str,'/');
				}
				else if( str == "MAPUNITS" )
				{
					getline(stream,str,'/');
				}
				else if( str == "MAPAXES" )
				{
					getline(stream,str,'/');
					std::istringstream sstream(str);
					sstream >> origin[0] >> origin[1] >> axisx[0] >> axisx[1] >> axisy[0] >> axisy[1];
				}
				else if( str == "GRIDUNIT" )
				{
					getline(stream,str,'/');
				}
				else if( str == "SPECGRID" )
				{
					getline(stream,str,'/');
					std::istringstream sstream(str);
					sstream >> nx >> ny >> nz;
					
					
					coords.resize(nx+1);
					for(i = 0; i < nx+1; i++)
					{
						coords[i].resize(ny+1);
						for(j = 0; j < ny+1; j++)
						{
							coords[i][j].resize(2);
							for(l = 0; l < 2; l++)
								coords[i][j][l].resize(3);
						}
					}
						
					zcorn.resize(nx);
					for(i = 0; i < nx; i++)
					{
						zcorn[i].resize(ny);
						for(j = 0; j < ny; j++)
						{
							zcorn[i][j].resize(nz);
							for(k = 0; k < nz; k++)
								zcorn[i][j][k].resize(8);
						}
					}
					pinch.resize(nx+1);
					for(i = 0; i < nx+1; i++)
					{
						pinch[i].resize(ny+1);
						for(j = 0; j < ny+1; j++)
							pinch[i][j].resize(nz+1);
					}

				}
				else if( str == "COORDSYS" )
				{
					getline(stream,str,'/');
				}
				else if( str == "COORD" )
				{
					for(j = 0; j < ny+1; j++)
						for(i = 0; i < nx+1; i++)
							for(l = 0; l < 2; l++)
								for(k = 0; k < 3; k++)
									stream >> coords[i][j][l][k];
					getline(stream,str,'/');
				}
				else if( str == "ZCORN" )
				{
					for(k = 0; k < nz; k++)
					{
						for(q = 0; q < 2; q++)
							for(j = 0; j < ny; j++)
								for(m = 0; m < 2; m++)
									for(i = 0; i < nx; i++)
										for(l = 0; l < 2; l++)
											stream >> zcorn[i][j][k][l+m*2+(1-q)*4];
					}
					getline(stream,str,'/');
				}
				else if( str == "ACTNUM" )
				{
					pinch.resize(nx+1);
					for(k = 0; k < nz+1; k++)
						for(j = 0; j < ny+1; j++)
							for(i = 0; i < nx+1; i++)
								stream >> pinch[i][j][k];
					getline(stream,str,'/');
				}
				else
				{
					std::cout << "Ignoring keyword " << str << std::endl;
					getline(stream,str,'/');
				}
			}
			{
				std::vector< std::vector < std::map< Storage::real, Node * > > > nodes;
				nodes.resize(nx+1);
				for(i = 0; i < nx+1; i++) nodes[i].resize(ny+1);
				
				//nx = ny = 15;
				//nz = 80;
				
				//std::cout << "Creating grid" << std::endl;
				
				Node * c_nodes[8];
				int cbad = 0, cflat = 0;
				for(i = 0; i < nx; i++)
				{
					for(j = 0; j < ny; j++)
					{
						for(k = 0; k < nz; k++)
						{
							for(l = 0; l < 8; l++)
							{
								int i2 = i + l%2, j2 = j + (l/2)%2;
								std::map<Storage::real, Node * >::iterator search = nodes[i2][j2].find(zcorn[i][j][k][l]);
								if( search == nodes[i2][j2].end() )
								{
									Storage::real node_coords[3];
									Storage::real alpha = (zcorn[i][j][k][l]-coords[i2][j2][1][2])/(coords[i2][j2][0][2]-coords[i2][j2][1][2]);
									node_coords[0] = coords[i2][j2][1][0] + alpha * (coords[i2][j2][0][0] - coords[i2][j2][1][0]);
									node_coords[1] = coords[i2][j2][1][1] + alpha * (coords[i2][j2][0][1] - coords[i2][j2][1][1]);
									node_coords[2] = zcorn[i][j][k][l];
									search = nodes[i2][j2].insert(std::pair<Storage::real,Node *>(zcorn[i][j][k][l],CreateNode(node_coords))).first;
								}
								c_nodes[l] = search->second;
							}
							INMOST_DATA_ENUM_TYPE nodesnum[24] = {2,3,1,0,5,7,6,4,4,6,2,0,3,7,5,1,1,5,4,0,6,7,3,2};
							INMOST_DATA_ENUM_TYPE facessize[6] = {4,4,4,4,4,4};
							//const int nodesnum[6][4] = {{0,1,3,2},{4,6,7,5},{0,2,6,4},{1,5,7,3},{0,4,5,1},{2,3,7,6}};
							new_cells.push_back(CreateCell(c_nodes,nodesnum,facessize,6).first);
						}
					}
				}
				std::cout << "bad cells: " << cbad << " flat cells: " << cflat << std::endl;
				//std::cout << "Done" << std::endl;
			}
			stream.close();
		}
		else
		if(LFile.find(".pvtk") != std::string::npos) //this is legacy parallel vtk
		{
			int state = 0, np, nf = 0;
			size_t l,attrl, ql,qr;
			//~ if( m_state == Mesh::Serial ) SetCommunicator(INMOST_MPI_COMM_WORLD);
			std::string str, tag, attrval, path = "";
			std::vector<std::string> files;
			std::fstream stream(File.c_str(),std::ios::in);
			l = File.find_last_of("/\\");
			if( l != std::string::npos )
				path = File.substr(0,l+1);
			while(stream.good())
			{
				getline(stream,str,'>');
				l = str.find_first_not_of(" \t\r\n");
				if( l != std::string::npos ) str.substr(l,str.size()-l).swap(str);
				else str.clear();
				if( str.empty() ) continue;
				l = str.find_first_of(' ');
				tag = str.substr(1,l-1);
				//std::cout << " tag name " << tag << std::endl;
				if( state == 0 )
				{
					if( tag == "File" )
					{
						attrl = str.find("version=\"pvtk-1.0\"",l);
						if( attrl == std::string::npos ) throw BadFile;
						attrl = str.find("dataType=\"vtkUnstructuredGrid\"",l);
						if( attrl == std::string::npos ) throw BadFile;
						attrl = str.find("numberOfPieces");
						if( attrl == std::string::npos ) throw BadFile;
						ql = str.find_first_of('"',attrl);
						qr = str.find_first_of('"',ql+1);
						attrval = str.substr(ql+1,qr-ql-1);
						//std::cout << "number of peaces: " << attrval << " l: " << ql << " r: " << qr << std::endl;
						np = atoi(attrval.c_str());
						//std::cout << "np: " << np << std::endl;;
						files.resize(np);
						state = 1;
					}
					else throw BadFile;
				}
				else if( state == 1 )
				{
					if( tag == "Piece")
					{
						attrl = str.find("fileName");
						ql = str.find_first_of('"',attrl);
						qr = str.find_first_of('"',ql+1);
						attrval = str.substr(ql+1,qr-ql-1);
						//std::cout << "file" << nf << ":" << attrval << std::endl;
						files[nf++] = attrval;
						if( nf == np ) state = 2;
					}
					else throw BadFile;
				}
				else if( state == 2 )
				{
					if( tag != "/File" ) throw BadFile;
					else break;
				}
			}
			for(int i = GetProcessorRank(); i < static_cast<int>(files.size()); i+= GetProcessorsNumber()) 
			{
				//std::cout << "load " << i << ": " << path + files[i] << " by " << GetProcessorRank() << std::endl;
				Load(path + files[i]);
			}
			ResolveShared();
		}
		else if(LFile.find(".vtk") != std::string::npos) //this is legacy vtk
		{
			MIDType unused_marker = CreateMarker();
			std::vector<Node *> old_nodes;
			for(nodes_container::iterator it = nodes.begin(); it != nodes.end(); it++) if( *it != NULL )
				old_nodes.push_back(*it);

			if( !old_nodes.empty() ) qsort(&old_nodes[0],old_nodes.size(),sizeof(Node *),CompareElementsCCentroid);
			
			FILE * f = fopen(File.c_str(),"r");
			if( !f ) throw BadFileName;
			std::vector<Node *> newnodes;
			std::vector<Cell *> newcells;
			std::vector<int> cp;
			std::vector<int> ct;
			unsigned int state = R_VERSION;
			char readline[2048];
			int filled;
			bool binary = false;
			int read_into = NONE;
			while( state != R_QUIT )
			{
				
				if( fgets(readline,2048,f) == NULL )
				{
					state = R_QUIT;
					continue;
				}
				if( readline[strlen(readline)-1] == '\n' )
					readline[strlen(readline)-1] = '\0';
				if( strlen(readline) == 0 ) continue;
				switch( state )
				{
					case R_VERSION:
					{
						int h,l;
						filled = sscanf(readline,"# vtk DataFile Version %1d.%1d",&h,&l);
						if( filled == 0 ) throw BadFile;
						//file version checker
						state = R_USERDATA;
						break;
					}
					case R_USERDATA:
					{
						//printf("Attached info: %s\n",readline);
						state = R_DATATYPE;
						break;
					}
					case R_DATATYPE:
					{
						if( !strncmp(readline,"BINARY",6) )
							binary = 1;
						else if( !strncmp(readline,"ASCII",5) )
							state = R_WAITDATA;
						else throw BadFile;
						break;
					}
					case R_ATTRDATA:
					{
						char dataname[1024];
						char attrname[1024];
						char attrtype[1024];
						int nentries = 1;
						filled = sscanf(readline," %s ",dataname);
						if( filled != 1 ) throw BadFile;
						if( !strcmp(dataname,"SCALARS") )
						{
							DataType t = DATA_BULK;
							filled = sscanf(readline," SCALARS %s %s %d",attrname,attrtype,&nentries);
							if( filled < 2 ) {printf("found %d arguments to SCALARS field, must be >= 2\nline:\n%s\n",filled,readline); throw BadFile;}
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							Tag attr = CreateTag(attrname,t,read_into,NONE,nentries);
							if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
							if( read_into == CELL )
							{
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										if( newcells[it] != NULL )
										{
											Storage::integer_array attrdata = newcells[it]->IntegerArray(attr);
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
										if( newcells[it] != NULL )
										{
											Storage::real_array attrdata = newcells[it]->RealArray(attr);
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; }
									}
								}
								filled = fscanf(f,"\n");
							}
							if( read_into == NODE )
							{
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										Storage::integer_array attrdata = newnodes[it]->IntegerArray(attr);
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
										Storage::real_array attrdata = newnodes[it]->RealArray(attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"COLOR_SCALARS") )
						{	
							char attrname[1024];
							filled = sscanf(readline," COLOR_SCALARS %s %d",attrname,&nentries);
							Tag attr = CreateTag(attrname,DATA_REAL,read_into,NONE,nentries);
							if( read_into == CELL )
							{
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( newcells[it] != NULL )
									{
										Storage::real_array attrdata = newcells[it]->RealArray(attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
								}
								filled = fscanf(f,"\n");
							}
							if( read_into == NODE )
							{
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									Storage::real_array attrdata = newnodes[it]->RealArray(attr);
									for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname," LOOKUP_TABLE") )
						{
							int nentries;
							filled = sscanf(readline," LOOKUP_TABLE %*s %d",&nentries);
							if( filled != 1 ) throw BadFile;
							for(int i = 0; i < nentries; i++)
							{
								filled = fscanf(f," %*f %*f %*f %*f");
							}
							filled = fscanf(f,"\n");
							break;
						}
						else if( !strcmp(dataname,"NORMALS") || !strcmp(dataname,"VECTORS") || !strcmp(dataname,"TENSORS"))
						{
							char attrname[1024];
							char attrtype[1024];
							int nentries = 3;
							if( !strcmp(dataname,"TENSORS") ) nentries = 9;
							DataType t = DATA_BULK;
							filled = sscanf(readline,"%*s %s %s",attrname,attrtype);
							if( filled != 2 ) throw BadFile;
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							Tag attr = CreateTag(attrname,t,read_into,NONE,nentries);
							if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
							if( read_into == CELL )
							{
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										if( newcells[it] != NULL )
										{
											Storage::integer_array attrdata = newcells[it]->IntegerArray(attr);
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
										if( newcells[it] != NULL )
										{
											Storage::real_array attrdata = newcells[it]->RealArray(attr);
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									}
								}
								filled = fscanf(f,"\n");
							}
							if( read_into == NODE )
							{
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										Storage::integer_array attrdata = newnodes[it]->IntegerArray(attr);
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
										Storage::real_array attrdata = newnodes[it]->RealArray(attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"TEXTURE_COORDINATES") )
						{
							char attrname[1024];
							char attrtype[1024];
							int nentries;
							DataType t = DATA_BULK;
							filled = sscanf(readline," TEXTURE_COORDINATES %s %d %s",attrname,&nentries,attrtype);
							if( filled != 3 ) throw BadFile;
							for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
							if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
							   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
							   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
								t = DATA_INTEGER;
							else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
								t = DATA_REAL;
							else
								throw BadFile;
							Tag attr = CreateTag(attrname,t,read_into,NONE,nentries);
							if( fgets(readline,2048,f) == NULL ) throw BadFile; //LOOK_UP TABLE
							if( read_into == CELL )
							{
								for(unsigned int it = 0; it < newcells.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										if( newcells[it] != NULL )
										{
											Storage::integer_array attrdata = newcells[it]->IntegerArray(attr);
											for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
									}
									if( t == DATA_REAL )
									{
										if( newcells[it] != NULL )
										{
											Storage::real_array attrdata = newcells[it]->RealArray(attr);
											for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										} else for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
									}
								}
								filled = fscanf(f,"\n");
							}
							if( read_into == NODE )
							{
								for(unsigned int it = 0; it < newnodes.size(); it++)
								{
									if( t == DATA_INTEGER )
									{
										Storage::integer_array attrdata = newnodes[it]->IntegerArray(attr);
										for(int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
									if( t == DATA_REAL )
									{
										Storage::real_array attrdata = newnodes[it]->RealArray(attr);
										for(int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
									}
								}
								filled = fscanf(f,"\n");
							}
							break;
						}
						else if( !strcmp(dataname,"FIELD") )
						{
							int nfields = 0;
							filled = sscanf(readline," FIELD %*s %d",&nfields);
							if( filled != 1 ) throw BadFile;
							for(int i = 0; i < nfields; i++)
							{
								char attrname[1024];
								char attrtype[1024];
								unsigned int nentries, ntuples;
								DataType t = DATA_BULK;
								filled = fscanf(f," %s %u %u %s\n",attrname,&nentries,&ntuples,attrtype);
								if( filled != 4 ) throw BadFile;
								for(unsigned int i = 0; i < strlen(attrtype); i++) attrtype[i] = tolower(attrtype[i]);
								if( !strcmp(attrtype,"bit") || !strcmp(attrtype,"unsigned_char") || !strcmp(attrtype,"char") ||
								   !strcmp(attrtype,"unsigned_short") || !strcmp(attrtype,"short") || !strcmp(attrtype,"unsigned_int") ||
								   !strcmp(attrtype,"int") || !strcmp(attrtype,"unsigned_long") || !strcmp(attrtype,"long"))
									t = DATA_INTEGER;
								else if( !strcmp(attrtype,"float") || !strcmp(attrtype,"double") )
									t = DATA_REAL;
								else
									throw BadFile;
								Tag attr = CreateTag(attrname,t,read_into,NONE,nentries);
								
								if( read_into == CELL )
								{
									if( ntuples != newcells.size() ) printf("number of tuples in field is not equal to number of cells\n");
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
											if( newcells[it] != NULL )
											{
												Storage::integer_array attrdata = newcells[it]->IntegerArray(attr);
												for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
											} else for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile;}
										}
										if( t == DATA_REAL )
										{
											if( newcells[it] != NULL )
											{
												Storage::real_array attrdata = newcells[it]->RealArray(attr);
												for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
											} else for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile;}
										}
									}
									filled = fscanf(f,"\n");
								}
								if( read_into == NODE )
								{
									if( ntuples != newcells.size() ) printf("number of tuples in field is not equal to number of nodes\n");
									for(unsigned int it = 0; it < ntuples; it++)
									{
										if( t == DATA_INTEGER )
										{
											Storage::integer_array attrdata = newnodes[it]->IntegerArray(attr);
											for(unsigned int jt = 0; jt < nentries; jt++) {int temp; filled = fscanf(f," %d",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
										if( t == DATA_REAL )
										{
											Storage::real_array attrdata = newnodes[it]->RealArray(attr);
											for(unsigned int jt = 0; jt < nentries; jt++) {double temp; filled = fscanf(f," %lf",&temp); if(filled != 1 ) throw BadFile; attrdata[jt] = temp;}
										}
									}
									filled = fscanf(f,"\n");
								}
							}
							break;
						}
						else
						{
							state = R_ATTRIBUTES;
						}
					}
					case R_ATTRIBUTES:
					{
						char datatype[1024];
						unsigned int npoints;
						filled = sscanf(readline," %s %u",datatype,&npoints);
						if( filled != 2 ) throw BadFile;
						if( !strcmp(datatype,"CELL_DATA") )
						{
							if( npoints != newcells.size() ) printf("number of attributes is not equal to number of cells\n");
							state = R_ATTRDATA;
							read_into = CELL;
							break;
						}
						else if( !strcmp(datatype,"POINT_DATA") )
						{
							if( npoints != newnodes.size() ) printf("number of attributes is not equal to number of nodes\n");
							state = R_ATTRDATA;
							read_into = NODE;
							break;
						}
						else
						{
							printf("%s\n",readline);
							printf("Unknown type of attributes\n");
							state = R_WAITDATA;
						}
					}
					case R_WAITDATA:
					{
						char check[1024];
						filled = sscanf(readline," DATASET %s",check);
						if( filled != 1 ) throw BadFile;
						if( !strcmp(check,"STRUCTURED_POINTS") )
							state = R_SPOINTS;
						else if( !strcmp(check,"STRUCTURED_GRID" ) )
							state = R_SGRID;
						else if( !strcmp(check,"UNSTRUCTURED_GRID") )
							state = R_UGRID;
						else if( !strcmp(check,"POLYDATA") )
							state = R_POLYDATA;
						else if( !strcmp(check,"RECTLINEAR_GRID") )
							state = R_RGRID;
						else if( !strcmp(check,"FIELD") )
							state = R_FIELD;
						else throw BadFile;
						break;
					}
					case R_UGRID:
					{
						INMOST_DATA_REAL_TYPE coords[3] = {0,0,0};
						int npoints = 0, i = 0, ncells = 0, ncells2 = 0, nints = 0;
						char datatype[1024];
						filled = sscanf(readline,"%*s %d %s",&npoints,datatype);
						//printf("number of nodes: %d\n",npoints);
						if( filled != 2 ) throw BadFile;
						if( binary )
						{
							i = 0;
							while(i != npoints)
							{
								if( !strcmp(datatype,"double") ) ReadCoords<double>(f,coords);
								else if( !strcmp(datatype,"float") ) ReadCoords<float>(f,coords);
								else if( !strcmp(datatype,"unsigned char") ) ReadCoords<unsigned char>(f,coords);
								else if( !strcmp(datatype,"char") ) ReadCoords<char>(f,coords);
								else if( !strcmp(datatype,"unsigned short") ) ReadCoords<unsigned short>(f,coords);
								else if( !strcmp(datatype,"short") ) ReadCoords<short>(f,coords);
								else if( !strcmp(datatype,"unsigned int") ) ReadCoords<unsigned int>(f,coords);
								else if( !strcmp(datatype,"int") ) ReadCoords<int>(f,coords);
								else if( !strcmp(datatype,"unsigned long") ) ReadCoords<unsigned long>(f,coords);
								else if( !strcmp(datatype,"long") ) ReadCoords<long>(f,coords);
								else throw BadFile;
								int find = -1;
								if( !old_nodes.empty() )
									find = binary_search(&old_nodes[0],old_nodes.size(),sizeof(Element*),CompareCoordSearch,coords);
								if( find == -1 ) newnodes.push_back(CreateNode(coords));
								else newnodes.push_back(old_nodes[find]);
								newnodes.back()->SetMarker(unused_marker);
								i++;
							}
						}
						else
						{
							i = 0;
							while(i != npoints)
							{
								double c[3];
								filled = fscanf(f," %lg %lg %lg",&c[0],&c[1],&c[2]);
								coords[0] = c[0];
								coords[1] = c[1];
								coords[2] = c[2];
								if( filled != 3 ) throw BadFile;
								int find = -1;
								if( !old_nodes.empty() )
									find = binary_search(&old_nodes[0],old_nodes.size(),sizeof(Element*),CompareCoordSearch,coords);
								if( find == -1 ) newnodes.push_back(CreateNode(coords));
								else newnodes.push_back(old_nodes[find]);
								newnodes.back()->SetMarker(unused_marker);
								i++;
							}
							filled = fscanf(f,"\n");
						}
						if( fgets(readline,2048,f) == NULL ) throw BadFile;
						filled = sscanf(readline,"%*s %d %d\n",&ncells,&nints);
						//printf("number of cells: %d\nnumber of ints: %d\n",ncells,nints);
						if( filled != 2 ) throw BadFile;
						cp.resize(nints);
						ct.resize(ncells);
						if( binary )
							filled = fread(&cp[0],sizeof(int),nints,f);
						else
						{
							i = 0;
							while(i != nints)
							{
								filled = fscanf(f," %d",&cp[i]);
								if( filled != 1 ) throw BadFile;
								i++;
							}
							filled = fscanf(f,"\n");
						}
						if( fgets(readline,2048,f) == NULL ) throw BadFile;
						filled = sscanf(readline,"%*s %d\n",&ncells2);
						if( filled != 1 ) throw BadFile;
						if( ncells2 != ncells ) throw BadFile;
						if( binary )
							filled = fread(&ct[0],sizeof(int),nints,f);
						else
						{
							i = 0;
							while(i != ncells)
							{
								filled = fscanf(f," %d",&ct[i]);
								if( filled != 1 ) throw BadFile;
								i++;
							}
							filled = fscanf(f,"\n");
						}
						{
							int j = 0;
							dynarray<Node *,256> c_nodes;
							Node * e_nodes[2];
							dynarray<Edge *,64> f_edges;
							dynarray<Face *,64> c_faces;
							dynarray<Edge *,64> v_edges;
							for(i = 0; i < ncells; i++)
							{
								//printf("load progress: %20.2f%%\r",(float)(i+1)/(float)ncells*100.0f);
								fflush(stdin);
								c_nodes.clear();
								f_edges.clear();
								c_faces.clear();
								v_edges.clear();
								switch(ct[i])
								{
									case 1: // VTK_VERTEX
									{
										printf("VTK_VERTEX found, but 0d cell objects are not supported\n");
										newcells.push_back(NULL);
										break;
									}
									case 2: //VTK_POLY_VERTEX
									{
										printf("VTK_POLY_VERTEX cannot be represented\n");
										break;
									}
									case 3: //VTK_LINE
									{
										printf("VTK_LINE found, but 1d cell objects are not supported\n");
										newcells.push_back(NULL);
										break;
									}
									case 4: //VTK_POLY_LINE
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() < 2 ) throw BadFile;
										f_edges.resize(2);
										for(unsigned int k = 0; k < c_nodes.size()-1; k++)
										{
											e_nodes[0] = c_nodes[k];
											f_edges[0] = CreateEdge(e_nodes, 1).first;
											e_nodes[0] = c_nodes[(k+1)];
											f_edges[1] = CreateEdge(e_nodes, 1).first;
											c_faces.push_back(CreateFace(&f_edges[0],2).first);
										}
										Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],c_nodes.size()).first;
										c_faces.clear();
										newcells.push_back(c);
										break;
									}
									case 5: //VTK_TRIANGLE
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 3 ) throw BadFile;
										f_edges.resize(2);
										for(unsigned int k = 0; k < 3; k++)
										{
											e_nodes[0] = c_nodes[k];
											f_edges[0] = CreateEdge(e_nodes, 1).first;
											e_nodes[0] = c_nodes[(k+1)%3];
											f_edges[1] = CreateEdge(e_nodes, 1).first;
											c_faces.push_back(CreateFace(&f_edges[0],2).first);
										}
										Cell * c = CreateCell(&c_faces[0],2,&c_nodes[0],c_nodes.size()).first;
										c_faces.clear();
										newcells.push_back(c);
										break;
									}
									case 6: //VTK_TRIANGLE_STRIP
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() < 3 ) throw BadFile;
										std::deque<Node *> d_nodes;
										unsigned int l = 3;
										f_edges.resize(3);
										d_nodes.insert(d_nodes.begin(),c_nodes.begin(),c_nodes.begin()+3);
										do
										{
											for(int q = 0; q < 3; q++)
											{
												e_nodes[0] = d_nodes[q];
												e_nodes[1] = d_nodes[(q+1)%3];
												f_edges[q] = CreateEdge(e_nodes, 2).first;
											}
											if( l == c_nodes.size() ) break;
											c_faces.push_back(CreateFace(&f_edges[0],3).first);
											d_nodes.pop_front();
											d_nodes.push_back(c_nodes[l]);
											l++;
										}
										while(l <= c_nodes.size());
										Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],c_nodes.size()).first;
										newcells.push_back(c);
										break;
									}
									case 7: //VTK_POLYGON
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										f_edges.resize(2);
										for(unsigned int k = 0; k < c_nodes.size(); k++)
										{
											e_nodes[0] = c_nodes[k];
											f_edges[0] = CreateEdge(e_nodes, 1).first;
											e_nodes[0] = c_nodes[(k+1)%c_nodes.size()];
											f_edges[1] = CreateEdge(e_nodes, 1).first;
											c_faces.push_back(CreateFace(&f_edges[0],2).first);
										}
										Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],c_nodes.size()).first;
										c_faces.clear();
										newcells.push_back(c);
										break;
									}
									case 8: //VTK_PIXEL
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 4 ) throw BadFile;
										Node * temp = c_nodes[2];
										c_nodes[2] = c_nodes[3];
										c_nodes[3] = temp;
										f_edges.resize(2);
										for(int k = 0; k < 4; k++)
										{
											e_nodes[0] = c_nodes[k];
											f_edges[0] = CreateEdge(e_nodes, 1).first;
											e_nodes[0] = c_nodes[(k+1)%4];
											f_edges[1] = CreateEdge(e_nodes, 1).first;
											c_faces.push_back(CreateFace(&f_edges[0],2).first);
										}
										Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],4).first;
										newcells.push_back(c);
										break;
									}
									case 9: //VTK_QUAD
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 4 ) throw BadFile;
										f_edges.resize(2);
										for(int k = 0; k < 4; k++)
										{
											e_nodes[0] = c_nodes[k];
											f_edges[0] = CreateEdge(e_nodes, 1).first;
											e_nodes[0] = c_nodes[(k+1)%4];
											f_edges[1] = CreateEdge(e_nodes, 1).first;
											c_faces.push_back(CreateFace(&f_edges[0],2).first);
										}
										Cell * c = CreateCell(&c_faces[0],c_faces.size(),&c_nodes[0],4).first;
										c_faces.clear();
										newcells.push_back(c);
										break;
									}
									case 10: //VTK_TETRA
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 4 ) throw BadFile;
										INMOST_DATA_ENUM_TYPE nodesnum[12] = {0,2,1,0,1,3,1,2,3,0,3,2};
										INMOST_DATA_ENUM_TYPE sizes[4] = {3,3,3,3};
										Cell * c = CreateCell(&c_nodes[0],nodesnum,sizes,4).first;
										newcells.push_back(c);
										break;
									}
									case 11: //VTK_VOXEL
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if (c_nodes.size() != 8) throw BadFile;
										{
											Node * temp;
											temp = c_nodes[2];
											c_nodes[2] = c_nodes[3];
											c_nodes[3] = temp;
											temp = c_nodes[6];
											c_nodes[6] = c_nodes[7];
											c_nodes[7] = temp;
										}
										//INMOST_DATA_ENUM_TYPE nodesnum[24] = {0,4,6,2,1,3,7,5,2,6,7,3,0,1,5,4,0,2,3,1,4,5,7,6};
										INMOST_DATA_ENUM_TYPE 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};
										INMOST_DATA_ENUM_TYPE sizes[6] = {4,4,4,4,4,4};
										Cell * c = CreateCell(&c_nodes[0],nodesnum,sizes,6).first;
										newcells.push_back(c);
										break;
									}
									case 12: //VTK_HEXAHEDRON
									{

										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 8 ) throw BadFile;
										INMOST_DATA_ENUM_TYPE 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};
										INMOST_DATA_ENUM_TYPE sizes[6] = {4,4,4,4,4,4};
										Cell * c = CreateCell(&c_nodes[0],nodesnum,sizes,6).first;
										newcells.push_back(c);
										break;
									}
									case 13: //VTK_WEDGE
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 6 ) throw BadFile;
										INMOST_DATA_ENUM_TYPE nodesnum[18] = {0,2,5,3,1,4,5,2,0,3,4,1,3,5,4,0,1,2};
										INMOST_DATA_ENUM_TYPE sizes[5] = {4,4,4,3,3};
										Cell * c = CreateCell(&c_nodes[0],nodesnum,sizes,5).first;
										newcells.push_back(c);
										break;
									}
									case 14: //VTK_PYRAMID
									{
										for(int k = j+1; k < j+1+cp[j]; k++)
										{
											c_nodes.push_back(newnodes[cp[k]]);
											newnodes[cp[k]]->RemMarker(unused_marker);
										}
										j = j + 1 + cp[j];
										if( c_nodes.size() != 5 ) throw BadFile;
										INMOST_DATA_ENUM_TYPE nodesnum[16] = {0,4,3,0,1,4,1,2,4,3,4,2,0,3,2,1};
										INMOST_DATA_ENUM_TYPE sizes[5] = {3,3,3,3,4};
										Cell * c = CreateCell(&c_nodes[0],nodesnum,sizes,5).first;
										newcells.push_back(c);
										break;
									}
									case 15: //VTK_PENTAGONAL_PRISM
									{
										printf("no order description for VTK_PENTAGONAL_PRISM\n");
										break;
									}
									case 16: //VTK_HEXAGONAL_PRISM
									{
										printf("no order description for VTK_HEXAGONAL_PRISM\n");
										break;
									}
									case 21: //VTK_QUADRATIC_EDGE
									{
										printf("no order description for VTK_QUADRATIC_EDGE\n");
										break;
									}
									case 22: //VTK_QUADRATIC_TRIANGLE
									{
										printf("no order description for VTK_QUADRATIC_TRIANGLE\n");
										break;
									}
									case 23: //VTK_QUADRATIC_QUAD
									{
										printf("no order description for VTK_QUADRATIC_QUAD\n");
										break;
									}
									case 24: //VTK_QUADRATIC_TETRA
									{
										printf("no order description for VTK_QUADRATIC_TETRA\n");
										break;
									}
									case 25: //VTK_QUADRATIC_HEXAHEDRON
									{
										printf("no order description for VTK_QUADRATIC_HEXAHEDRON\n");
										break;
									}
									case 26: //VTK_QUADRATIC_WEDGE
									{
										printf("no order description for VTK_QUADRATIC_WEDGE\n");
										break;
									}
									case 27: //VTK_QUADRATIC_PYRAMID
									{
										printf("no order description for VTK_QUADRATIC_PYRAMID\n");
										break;
									}
									case 28: //VTK_BIQUADRATIC_QUAD
									{
										printf("no order description for VTK_BIQUADRATIC_QUAD\n");
										break;
									}
									case 29: //VTK_TRIQUADRATIC_HEXAHEDRON
									{
										printf("no order description for VTK_TRIQUADRATIC_HEXAHEDRON\n");
										break;
									}
									case 30: //VTK_QUADRATIC_LINEAR_QUAD
									{
										printf("no order description for VTK_QUADRATIC_LINEAR_QUAD\n");
										break;
									}
									case 31: //VTK_QUADRATIC_LINEAR_WEDGE
									{
										printf("no order description for VTK_QUADRATIC_LINEAR_WEDGE\n");
										break;
									}
									case 32: //VTK_BIQUADRATIC_QUADRATIC_WEDGE
									{
										printf("no order description for VTK_BIQUADRATIC_QUADRATIC_WEDGE\n");
										break;
									}
									case 33: //VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON
									{
										printf("no order description for VTK_BIQUADRATIC_QUADRATIC_HEXAHEDRON\n");
										break;
									}
									case 34: //VTK_BIQUADRATIC_TRIANGLE
									{
										printf("no order description for VTK_BIQUADRATIC_TRIANGLE\n");
										break;
									}
									case 35: //VTK_CUBIC_LINE
									{
										printf("no order description for VTK_CUBIC_LINE\n");
										break;
									}
									case 41: //VTK_CONVEX_POINT_SET
									{
										printf("no algorithm for VTK_CONVEX_POINT_SET yet\n");
										break;
									}
									case 42: //VTK_POLYHEDRON
									{
										int k = j;
										k++; //skip number of total number of integers
										dynarray<INMOST_DATA_ENUM_TYPE,64> sizes(cp[k++]);
										for(INMOST_DATA_ENUM_TYPE m = 0; m < sizes.size(); m++)
										{
											sizes[m] = cp[k++];
											for (INMOST_DATA_ENUM_TYPE l = 0; l < sizes[m]; l++)
											{
												c_nodes.push_back(newnodes[cp[k++]]);
												c_nodes.back()->RemMarker(unused_marker);
											}
										}
										j = k;
										Cell * c = CreateCell(&c_nodes[0],&sizes[0],sizes.size()).first;
										newcells.push_back(c);
										break;
									}
									case 51: //VTK_PARAMETRIC_CURVE
									{
										printf("no order description for VTK_PARAMETRIC_CURVE\n");
										break;
									}
									case 52: //VTK_PARAMETRIC_SURFACE
									{
										printf("no order description for VTK_PARAMETRIC_SURFACE\n");
										break;
									}
									case 53: //VTK_PARAMETRIC_TRI_SURFACE
									{
										printf("no order description for VTK_PARAMETRIC_TRI_SURFACE\n");
										break;
									}
									case 54: //VTK_PARAMETRIC_QUAD_SURFACE
									{
										printf("no order description for VTK_PARAMETRIC_QUAD_SURFACE\n");
										break;
									}
									case 55: //VTK_PARAMETRIC_TETRA_REGION
									{
										printf("no order description for VTK_PARAMETRIC_TETRA_REGION\n");
										break;
									}
									case 56: //VTK_PARAMETRIC_HEX_REGION
									{
										printf("no order description for VTK_PARAMETRIC_HEX_REGION\n");
										break;
									}
									case 60: //VTK_HIGHER_ORDER_EDGE
									{
										printf("no order description for VTK_HIGHER_ORDER_EDGE\n");
										break;
									}
									case 61: //VTK_HIGHER_ORDER_TRIANGLE
									{
										printf("no order description for VTK_HIGHER_ORDER_TRIANGLE\n");
										break;
									}
									case 62: //VTK_HIGHER_ORDER_QUAD
									{
										printf("no order description for VTK_HIGHER_ORDER_QUAD\n");
										break;
									}
									case 63: //VTK_HIGHER_ORDER_POLYGON
									{
										printf("no order description for VTK_HIGHER_ORDER_POLYGON\n");
										break;
									}
									case 64: //VTK_HIGHER_ORDER_TETRAHEDRON
									{
										printf("no order description for VTK_HIGHER_ORDER_TETRAHEDRON\n");
										break;
									}
									case 65: //VTK_HIGHER_ORDER_WEDGE
									{
										printf("no order description for VTK_HIGHER_ORDER_WEDGE\n");
										break;
									}
									case 66: //VTK_HIGHER_ORDER_PYRAMID
									{
										printf("no order description for VTK_HIGHER_ORDER_PYRAMID\n");
										break;
									}
									case 67: //VTK_HIGHER_ORDER_HEXAHEDRON
									{
										printf("no order description for VTK_HIGHER_ORDER_HEXAHEDRON\n");
										break;
									}
									default: //VTK_NUMBER_OF_CELL_TYPES
									{
										printf("Cell type %d is not known\n",ct[i]);
									}
								}
							}
							ReorderEmpty(FACE | EDGE);
							state = R_ATTRIBUTES;
						}
						break;
					}
					case R_SPOINTS:
					{
						printf("STRUCTURED POINTS is not yet implemented\n");
						break;
					}
					case R_POLYDATA:
					{
						printf("POLYDATA is not yet implemented\n");
						break;
					}
					case R_SGRID:
					{
						printf("STRUCTURED GRID is not yet implemented\n");
						break;
					}
					case R_RGRID:
					{
						printf("RECTLINEAR GRID is not yet implemented\n");
						break;
					}
					case R_FIELD:
					{
						printf("FIELD is not yet implemented\n");
						break;
					}
				}
			}
			{
				int count_unused = 0;
				for(unsigned q = 0; q < newnodes.size(); q++)
					if( newnodes[q]->GetMarker(unused_marker) )
					{
						count_unused++;
						delete newnodes[q];
						//~ newnodes[q]->RemMarker(unused_marker);
					}
				if( count_unused ) std::cout << __FILE__ << ":" << __LINE__ << " Warning: deleted " << count_unused << " unused nodes, file " << File << std::endl;
			}
			ReleaseMarker(unused_marker);
			fclose(f);
		} 
		else if(LFile.find(".pmf") != std::string::npos) //this is inner parallel/platform mesh format
		{
			REPORT_STR("start load pmf");
			std::vector<INMOST_DATA_ENUM_TYPE> myprocs;
			std::stringstream in(std::ios::in | std::ios::out | std::ios::binary);
			HeaderType token;
#if defined(USE_MPI)
			if( m_state == Mesh::Parallel )
			{
#if defined(USE_MPI2)
				if( parallel_file_strategy == 1 )
				{
					int ierr;
					std::string buffer;
					MPI_File fh;
					MPI_Status stat;
					ierr = MPI_File_open(GetCommunicator(),const_cast<char *>(File.c_str()),MPI_MODE_RDONLY,MPI_INFO_NULL,&fh);
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
					INMOST_DATA_ENUM_TYPE numprocs = GetProcessorsNumber(), recvsize = 0, mpirank = GetProcessorRank();
					std::vector<INMOST_DATA_ENUM_TYPE> recvsizes;

					if( mpirank == 0 ) //the alternative is to read alltogether
					{
						INMOST_DATA_ENUM_TYPE datanum, chunk,pos,k,q;
						std::vector<INMOST_DATA_ENUM_TYPE> datasizes;
						std::stringstream header;
						
						buffer.resize(3);
						//ierr = MPI_File_read_all(fh,&buffer[0],3,MPI_CHAR,&stat);
						ierr = MPI_File_read_shared(fh,&buffer[0],3,MPI_CHAR,&stat);
						if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);

						if( static_cast<HeaderType>(buffer[0]) != INMOST::INMOSTFile ) throw BadFile;

						header << buffer.c_str()+1;
						uconv.read_iByteOrder(header);
						uconv.read_iByteSize(header);

						buffer.resize(uconv.get_source_iByteSize());
						//ierr = MPI_File_read_all(fh,&buffer[0],buffer.size(),MPI_CHAR,&stat);
						ierr = MPI_File_read_shared(fh,&buffer[0],buffer.size(),MPI_CHAR,&stat);
						if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);

						header << buffer;
						uconv.read_iValue(header,datanum);

						buffer.resize(datanum*uconv.get_source_iByteSize());
						datasizes.resize(datanum);
						//ierr = MPI_File_read_all(fh,&buffer[0],buffer.size(),MPI_CHAR,&stat);
						ierr = MPI_File_read_shared(fh,&buffer[0],buffer.size(),MPI_CHAR,&stat);
						if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);

						header << buffer;
						for(k = 0; k < datanum; k++) uconv.read_iValue(header,datasizes[k]);


						// use this commented code when all processors read the file alltogether through MPI_File_read_all
						//{
						//	MPI_Offset off;
						//	ierr = MPI_File_get_position(fh,&off);
						//	if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
						//	ierr = MPI_File_seek_shared( fh, off, MPI_SEEK_SET );
						//	if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
						//}
						//if( datanum <= numprocs )
						//{
						//	if( mpirank < datanum )
						//		recvsize = datasizes[mpirank];
						//}
						//else
						//{
						//	chunk = static_cast<INMOST_DATA_ENUM_TYPE>(floor(static_cast<double>(datanum)/static_cast<double>(numprocs)));
						//	if( mpirank < numprocs - 1)
						//	{
						//		for(k = chunk*(mpirank); k < chunk*(mpirank+1); k++)
						//			recvsize += datasizes[k];
						//	}
						//	else
						//	{
						//		for(k = chunk*(mpirank); k < datanum; k++)
						//			recvsize += datasizes[k];
						//	}
						//}

						recvsizes.resize(numprocs,0);
						if( datanum <= recvsizes.size() )
						{
							for(k = 0; k < datanum; k++)
								recvsizes[k] = datasizes[k];
						}
						else
						{
							chunk = static_cast<INMOST_DATA_ENUM_TYPE>(floor(static_cast<double>(datanum)/static_cast<double>(recvsizes.size())));
							pos = 0;
							for(k = 0; k < recvsizes.size()-1; k++)
								for(q = 0; q < chunk; q++)
									recvsizes[k] += datasizes[pos++];
							for(k = pos; k < datanum; k++)
								recvsizes[recvsizes.size()-1] += datasizes[k];
						}
					}
					else recvsizes.resize(1); //protect from dereferencing null

					ierr = MPI_Scatter(&recvsizes[0],1,INMOST_MPI_DATA_ENUM_TYPE,&recvsize,1,INMOST_MPI_DATA_ENUM_TYPE,0,GetCommunicator());
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);


					buffer.resize(std::max(1u,recvsize)); //protect from dereferencing null


					{
						ierr = MPI_File_read_ordered(fh,&buffer[0],recvsize,MPI_CHAR,&stat);
						if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
						in.write(&buffer[0],recvsize);
					}

					ierr = MPI_File_close(&fh);
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
				}
				else
#endif
				{
					int ierr;
					std::string buffer, local_buffer;
					INMOST_DATA_ENUM_TYPE recvsize;
					
					INMOST_DATA_ENUM_TYPE numprocs = GetProcessorsNumber();
					std::vector<INMOST_DATA_ENUM_TYPE> recvsizes(numprocs,0);
					std::vector<int> sendcnts(numprocs), displs(numprocs);
					if( GetProcessorRank() == 0 ) //zero reads everything
					{
						std::fstream fin(File.c_str(),std::ios::in | std::ios::binary);
						fin.get(token);
						if( token != INMOST::INMOSTFile ) throw BadFile;
						uconv.read_iByteOrder(fin);
						uconv.read_iByteSize(fin);
						INMOST_DATA_ENUM_TYPE datanum,k,q,datasum = 0,chunk,pos;
						uconv.read_iValue(fin,datanum);
						std::vector<INMOST_DATA_ENUM_TYPE> datasizes(datanum);
						for(k = 0; k < datanum; k++) 
						{
							uconv.read_iValue(fin,datasizes[k]);
							datasum += datasizes[k];
						}
						{
							buffer.resize(datasum);
							fin.read(&buffer[0],buffer.size());
						}
						fin.close();


						if( datanum <= recvsizes.size() )
						{
							for(k = 0; k < datanum; k++)
								recvsizes[k] = datasizes[k];
						}
						else
						{
							chunk = static_cast<INMOST_DATA_ENUM_TYPE>(floor(static_cast<double>(datanum)/static_cast<double>(recvsizes.size())));
							pos = 0;
							for(k = 0; k < recvsizes.size()-1; k++)
								for(q = 0; q < chunk; q++)
									recvsizes[k] += datasizes[pos++];
							for(k = pos; k < datanum; k++)
								recvsizes[recvsizes.size()-1] += datasizes[k];
						}

						displs[0] = 0;
						sendcnts[0] = recvsizes[0];
						for(k = 1; k < numprocs; k++)
						{
							sendcnts[k] = recvsizes[k];
							displs[k] = sendcnts[k-1]+displs[k-1];
						}
					}
					else 
					{
						//protect from dereferencing null
						buffer.resize(1);
					}
					ierr = MPI_Scatter(&recvsizes[0],1,INMOST_MPI_DATA_ENUM_TYPE,&recvsize,1,INMOST_MPI_DATA_ENUM_TYPE,0,GetCommunicator());
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
					local_buffer.resize(recvsize);
					ierr = MPI_Scatterv(&buffer[0],&sendcnts[0],&displs[0],MPI_CHAR,&local_buffer[0],recvsize,MPI_CHAR,0,GetCommunicator());
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
					in.write(&local_buffer[0],local_buffer.size());
				}

			}
			else
#endif
			{
				std::fstream fin(File.c_str(),std::ios::in | std::ios::binary);
				fin.get(token);
				if( token != INMOST::INMOSTFile ) throw BadFile;
				uconv.read_iByteOrder(fin);
				uconv.read_iByteSize(fin);
				INMOST_DATA_ENUM_TYPE datanum,k,datasum = 0;
				uconv.read_iValue(fin,datanum);
				std::vector<INMOST_DATA_ENUM_TYPE> datasizes(datanum);
				for(k = 0; k < datanum; k++) 
				{
					uconv.read_iValue(fin,datasizes[k]);
					datasum += datasizes[k];
				}
				{
					std::string buffer;
					buffer.resize(datasum);
					fin.read(&buffer[0],buffer.size());
					in.write(&buffer[0],buffer.size());
				}
				fin.close();
			}
			
			//std::fstream in(File.c_str(),std::ios::in | std::ios::binary);
			
			std::vector<Tag> tags;
			std::vector<Node *> old_nodes;
			std::vector<Node *> new_nodes;
			std::vector<Edge *> new_edges;
			std::vector<Face *> new_faces;
			std::vector<Cell *> new_cells;
			std::vector<ElementSet *> new_sets;
			INMOST_DATA_ENUM_TYPE size,i;
			TopologyCheck tmp;
			

			bool start = false;
			
			std::map<GeometricData,ElementType> table;
			
			BeginModification();
		
			while (!(in >> token).eof()) 
			{
				if( !start ) 
				{
					if( token != INMOST::INMOSTFile ) throw BadFile; //check that this is valid file
					else 
					{
						REPORT_STR("File chunk start read");
						//~ std::cout << "start read" << std::endl;
						tags.clear();
						old_nodes.clear();
						new_nodes.clear();
						new_edges.clear();
						new_faces.clear();
						new_cells.clear();
						new_sets.clear();
						old_nodes.reserve(NumberOfNodes());
						for(nodes_container::iterator it = nodes.begin(); it != nodes.end(); it++) if( *it != NULL )
							old_nodes.push_back(*it);
						if( !old_nodes.empty() )qsort(&old_nodes[0],old_nodes.size(),sizeof(Node *),CompareElementsCCentroid);
						if( old_nodes.empty() )
						{
							tmp = GetTopologyCheck(DUPLICATE_CELL | DUPLICATE_FACE | DUPLICATE_EDGE); //we expect not to have duplicates
							RemTopologyCheck(DUPLICATE_CELL | DUPLICATE_FACE | DUPLICATE_EDGE);
						}
						else
						{
							tmp = GetTopologyCheck(DUPLICATE_CELL | DUPLICATE_FACE | DUPLICATE_EDGE); //we expect to have duplicates
							SetTopologyCheck(DUPLICATE_CELL | DUPLICATE_FACE | DUPLICATE_EDGE);
						}
						
						start = true;
					}
				}
				else if (token == INMOST::EoMHeader)  
				{
					
					if( !start ) throw BadFile;
					REPORT_STR("File chunk end read");
					//~ std::cout << "end read" << std::endl;
					if( old_nodes.empty() )
					{
						SetTopologyCheck(tmp);
					}
					else
					{
						if( !(tmp & DUPLICATE_CELL) ) RemTopologyCheck(DUPLICATE_CELL);
						if( !(tmp & DUPLICATE_FACE) ) RemTopologyCheck(DUPLICATE_FACE);
						if( !(tmp & DUPLICATE_EDGE) ) RemTopologyCheck(DUPLICATE_EDGE);
					}
					REPORT_VAL("NODE",new_nodes.size());
					REPORT_VAL("EDGE",new_edges.size());
					REPORT_VAL("FACE",new_faces.size());
					REPORT_VAL("CELL",new_cells.size());
					REPORT_VAL("ESET",new_sets.size());
					REPORT_VAL("TAG",tags.size());
					start = false; //probably the next file is in the input
				}
				else if (token == INMOST::MeshHeader)
				{
					REPORT_STR("MeshHeader");
					uconv.read_iByteOrder(in);
					uconv.read_iByteSize(in);
					iconv.read_iByteOrder(in);
					iconv.read_iByteSize(in);
					iconv.read_fByteOrder(in);
					iconv.read_fByteSize(in);

					INMOST_DATA_ENUM_TYPE header[9],k;
					for(k = 0; k < 9; k++)
						uconv.read_iValue(in,header[k]);
					
					{
						
						char rtemp[5][3];
						in.read(reinterpret_cast<char *>(rtemp),sizeof(rtemp));
						for(GeometricData d = CENTROID; d <= BARYCENTER; d++)
							for(ElementType et = EDGE; et <= CELL; et = et << 1)
								if( rtemp[d][ElementNum(et)-1] ) table[d] |= et;
						
					}

					

					SetDimensions(header[0]);
					new_nodes.reserve(header[1]);
					new_edges.reserve(header[2]);
					new_faces.reserve(header[3]);
					new_cells.reserve(header[4]);
					new_sets.reserve(header[5]);
					tags.reserve(header[6]);
					//~ if( static_cast<Mesh::MeshState>(header[7]) == Mesh::Parallel && m_state != Mesh::Parallel)
						//~ SetCommunicator(INMOST_MPI_COMM_WORLD);
					myprocs.push_back(header[8]);
				}
				else if (token == INMOST::TagsHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("TagsHeader");
					REPORT_VAL("tag_size",size);
					for(i = 0; i < size; i++) tags.push_back(ReadTag(in));
				}
				else if (token == INMOST::NodeHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("NodeHeader");
					REPORT_VAL("node_size",size);
					for(i = 0; i < size; i++) new_nodes.push_back(ReadNode(in,old_nodes));
				}
				else if (token == INMOST::EdgeHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("EdgeHeader");
					REPORT_VAL("edge_size",size);
					for(i = 0; i < size; i++) new_edges.push_back(ReadEdge(in,new_nodes));
				}
				else if (token == INMOST::FaceHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("FaceHeader");
					REPORT_VAL("face_size",size);
					for(i = 0; i < size; i++) new_faces.push_back(ReadFace(in,new_edges));
				}
				else if (token == INMOST::CellHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("CellHeader");
					REPORT_VAL("cell_size",size);
					for(unsigned i = 0; i < size; i++) new_cells.push_back(ReadCell(in,new_faces,new_nodes));
				}
				else if (token == INMOST::ESetHeader)
				{
					uconv.read_iValue(in,size);
					REPORT_STR("EsetHeader");
					REPORT_VAL("eset_size",size);
					for(unsigned i = 0; i < size; i++) new_sets.push_back(ReadElementSet(in,new_nodes,new_edges,new_faces,new_cells));
				}
				else if (token == INMOST::MeshDataHeader)
				{
					REPORT_STR("MeshDataHeader");
					for(size_t j = 0; j < tags.size(); j++) 
					{
						REPORT_VAL("TagName",tags[j].GetTagName());
						Tag * jt = &tags[j];
						for(ElementType etype = NODE; etype <= MESH; etype = etype << 1)
						{
							if( jt->isDefined(etype) )
							{
								INMOST_DATA_ENUM_TYPE q, cycle_end;
								Storage * e;
								switch(etype)
								{
								case NODE: cycle_end = new_nodes.size(); break;
								case EDGE: cycle_end = new_edges.size(); break;
								case FACE: cycle_end = new_faces.size(); break;
								case CELL: cycle_end = new_cells.size(); break;
								case ESET: cycle_end = new_sets.size(); break;
								case MESH: cycle_end = 1; break;
								}
								if( jt->isSparse(etype) )
								{
									uconv.read_iValue(in,q);
									//~ std::cout << jt->GetTagName() << std::endl;
									while(q != cycle_end)
									{
										switch(etype)
										{
										case NODE: e = new_nodes[q]; break;
										case EDGE: e = new_edges[q]; break;
										case FACE: e = new_faces[q]; break;
										case CELL: e = new_cells[q]; break;
										case ESET: e = new_sets[q]; break;
										case MESH: e = this; break;
										}
										//~ std::cout << q << "/" << cycle_end << std::endl;
										ReadData(in,*jt,e,new_nodes,new_edges,new_faces,new_cells);
										uconv.read_iValue(in,q);
									}
								}
								else
								{
									for(q = 0; q < cycle_end; q++)
									{
										switch(etype)
										{
										case NODE: e = new_nodes[q]; break;
										case EDGE: e = new_edges[q]; break;
										case FACE: e = new_faces[q]; break;
										case CELL: e = new_cells[q]; break;
										case ESET: e = new_sets[q]; break;
										case MESH: e = this; break;
										}
										ReadData(in,*jt,e,new_nodes,new_edges,new_faces,new_cells);
									}
								}
							}
						}
					}
				}
				else throw BadFile;
			}
			
			
			
			if( m_state == Mesh::Parallel )
			{
#if defined(USE_MPI) 
				
				bool restore_state = false;
				INMOST_DATA_ENUM_TYPE numprocs = GetProcessorsNumber(), size = myprocs.size(),k, procs_sum = 0;
				std::vector<INMOST_DATA_ENUM_TYPE> procs_sizes(numprocs), procs;
				REPORT_MPI(MPI_Allgather(&size,1,INMOST_MPI_DATA_ENUM_TYPE,&procs_sizes[0],1,INMOST_MPI_DATA_ENUM_TYPE,GetCommunicator()));
				for(k = 0; k < numprocs; k++) if( procs_sizes[k] > 1 ) restore_state = true;
				REPORT_VAL("restore_state",restore_state);
				if( restore_state ) //we have to do something with parallel status data
				{
					int ierr;
					std::vector<int> recvcnts(numprocs), displs(numprocs);
					Storage::integer myrank = GetProcessorRank();
					std::sort(myprocs.begin(),myprocs.end());
					//have to allgatherv myprocs from all processors to detect the ownership of elements
					procs_sum = procs_sizes[0];
					recvcnts[0] = procs_sizes[0];
					displs[0] = 0;
					for(k = 1; k < numprocs; k++) 
					{
						procs_sum += procs_sizes[k];
						recvcnts[k] = procs_sizes[k];
						displs[k] = displs[k-1]+recvcnts[k-1];
					}
					myprocs.resize(std::max(1u,static_cast<unsigned>(myprocs.size())));
					procs.resize(procs_sum);
					REPORT_MPI(ierr = MPI_Allgatherv(&myprocs[0],procs_sizes[myrank],INMOST_MPI_DATA_ENUM_TYPE,&procs[0],&recvcnts[0],&displs[0],INMOST_MPI_DATA_ENUM_TYPE,GetCommunicator()));
					if( ierr != MPI_SUCCESS ) MPI_Abort(GetCommunicator(),-1);
					//we have to distinguish new elements and old elements
					//all new elements with owner in myprocs belong to me
					
					if( procs_sizes[myrank] > 0 )
					{
						for(Mesh::iteratorElement it = BeginElement(CELL | FACE | EDGE | NODE); it != EndElement(); ++it)
							if( it->New() )
							{
								Storage::integer & owner = it->IntegerDF(tag_owner);
								if( std::binary_search(myprocs.begin(),myprocs.end(),static_cast<INMOST_DATA_ENUM_TYPE>(owner)) ) 
									owner = myrank;
								else
								{
									for(k = 0; k < numprocs; k++)
										if( std::binary_search(procs.begin()+displs[k],procs.begin()+displs[k]+recvcnts[k],static_cast<INMOST_DATA_ENUM_TYPE>(owner)) )
										{
											owner = k;
											break;
										}
								}
								Storage::integer_array v = it->IntegerArrayDV(tag_processors);
								for(Storage::integer_array::iterator jt = v.begin(); jt != v.end(); ++jt)
									if( std::binary_search(myprocs.begin(),myprocs.end(),static_cast<INMOST_DATA_ENUM_TYPE>(*jt)) )
										*jt = myrank;
									else
									{
										for(k = 0; k < numprocs; k++)
											if( std::binary_search(procs.begin()+displs[k],procs.begin()+displs[k]+recvcnts[k],static_cast<INMOST_DATA_ENUM_TYPE>(*jt)) )
											{
												*jt = k;
												break;
											}
									}
								std::sort(v.begin(),v.end());
								v.resize(std::unique(v.begin(),v.end())-v.begin());
								if( myrank == owner )
								{
									if( v.size() == 1 )
										it->BulkDF(tag_shared) = Element::Owned;
									else
										it->BulkDF(tag_shared) = Element::Shared;
								}
								else
									it->BulkDF(tag_shared) = Element::Ghost;
							}
					}

				}
				ComputeSharedProcs();
				RecomputeParallelStorage(CELL | FACE | EDGE | NODE);
				
				//Share number of Layers
				REPORT_MPI(MPI_Bcast(&Integer(tag_layers),1,INMOST_MPI_DATA_INTEGER_TYPE,0,GetCommunicator()));
				REPORT_MPI(MPI_Bcast(&Integer(tag_bridge),1,INMOST_MPI_DATA_BULK_TYPE,0,GetCommunicator()));
#else // if there is no mpi, we don't care about statuses
				tag_shared = DeleteTag(tag_shared);
				tag_processors = DeleteTag(tag_processors);
				tag_owner = DeleteTag(tag_owner);
				tag_layers = DeleteTag(tag_layers);
				tag_bridge = DeleteTag(tag_bridge);
				tag_sendto = DeleteTag(tag_sendto);
				m_state = Mesh::Serial;
#endif
			}
			EndModification();
			
			//~ PrepareGeometricData(table);
			RestoreGeometricTags();
			
			if( HaveTag("GLOBAL_ID") )
			{
				tag_global_id = GetTag("GLOBAL_ID");
				for(ElementType etype = NODE; etype <= CELL; etype = etype << 1)
					if( tag_global_id.isDefined(etype) ) have_global_id |= etype;
			}
#if defined(USE_MPI)
			if( m_state == Mesh::Parallel )
			{
				int hgi = have_global_id, recvtype = 0;
				//~ int flag = 0, recvflag;
				REPORT_MPI(MPI_Allreduce(&hgi,&recvtype,1,MPI_INT,MPI_BOR,comm));
				REPORT_VAL("local  types", hgi);
				REPORT_VAL("global types", recvtype);
				for(ElementType etype = NODE; etype <= CELL; etype = etype << 1 )
				{
					REPORT_VAL("test global id tag type",ElementTypeName(etype));
					if( (etype & recvtype) && !(etype & have_global_id) ) 
					{
						tag_global_id = CreateTag("GLOBAL_ID",DATA_INTEGER, etype, NONE,1);
						have_global_id |= etype;
						//~ flag = 1;
						REPORT_VAL("created new global id tag",ElementTypeName(etype));
					}
				}
				//~ REPORT_MPI(MPI_Allreduce(&flag,&recvflag,1,MPI_INT,MPI_BOR,comm));
				//~ REPORT_VAL("flag",&recvflag);
				//~ if( recvflag ) 
				AssignGlobalID(recvtype);
			}
#endif
		}
		else throw NotImplemented;
		EXIT_FUNC();
	}
	
	int VtkElementType(ElementType t)
	{
		switch(t)
		{
			case Element::Tri: return 5;
			case Element::Quad: return 9;
			case Element::MultiLine: return 4;
			case Element::Polygon: return 7;
			case Element::Tet: return 10;
			case Element::Hex: return 12;
			case Element::Prism: return 13;
			case Element::Pyramid: return 14;
			case Element::Polyhedron: return 42;
			case Element::MultiPolygon: return 42;
		}
		return -1;
	}
	
	
	
	void Mesh::Save(std::string File)
	{
		ENTER_FUNC();
		REPORT_VAL("File",File);
		std::string LFile;
		LFile.resize(File.size());
		std::transform(File.begin(),File.end(),LFile.begin(),::tolower);
		if(LFile.find(".pvtk") != std::string::npos) //this is legacy parallel vtk
		{
			std::string name=File;
			int pos=name.rfind(".pvtk");
			name.erase(pos); 

			int l=name.find_last_of("/\\");
			std::string fname=name.substr(l+1,name.length());
			if(GetProcessorRank()==0)
			{//create pvtk file
				std::stringstream ss;
				std::string numproc;
				ss << GetProcessorsNumber();
				numproc=ss.str();
				FILE  *f=fopen(File.c_str(), "w");
				fprintf(f,"%s%s%s", "<File version=\"pvtk-1.0\" dataType=\"vtkUnstructuredGrid\" numberOfPieces=\"", numproc.c_str(),"\">\n");
				for (int i=0; i<GetProcessorsNumber(); i++)
				{
					std::stringstream ss;
					ss << i;//task: leading zeroes
					std::string end="_"+ss.str()+".vtk";

					std::string temp=fname;
					temp.append(end);
					fprintf(f, "%s%s%s", "<Piece fileName=\"" ,temp.c_str() ,"\"/>\n");
				}
				fprintf(f, "%s", "</File>");
				fclose(f);
			}
			
			std::stringstream ss;
			ss << GetProcessorRank();//task: leading zeroes
			std::string end="_"+ss.str()+".vtk";
			
			name.append(end);
			Save(name);
		}
		else if(LFile.find(".vtk") != std::string::npos) //this is legacy vtk
		{
			unsigned int dim = GetDimensions();
			if( dim > 3 )
			{
				printf("VTK file supports 3 dimensions max\n");
				return;
			}
			FILE * f = fopen(File.c_str(),"w");
			if( !f ) throw BadFileName;
			fprintf(f,"# vtk DataFile Version 3.0\n");
			fprintf(f,"file is written by INMOST\n");
			fprintf(f,"ASCII\n");
			fprintf(f,"DATASET UNSTRUCTURED_GRID\n");
			ReorderEmpty(CELL | NODE);
			fprintf(f,"POINTS %u double\n",nodes.size());
			for(Mesh::nodes_container::iterator it = nodes.begin(); it != nodes.end(); it++)
			{
				Storage::real_array coords = (*it)->RealArray(CoordsTag());
				for(