mesh_gmsh_file.cpp 21.9 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
#ifdef _MSC_VER //kill some warnings
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "inmost.h"
//gmsh states

#if defined(USE_MESH)

#define GMSH_VERNONE 0
#define GMSH_VER1 1
#define GMSH_VER2 2

#define GMSH_NONE 0
#define GMSH_NODES_TOT 1
#define GMSH_NODES_NUM 2
#define GMSH_NODES_X 3
#define GMSH_NODES_Y 4
#define GMSH_NODES_Z 5
#define GMSH_ELEMENTS_TOT 6
#define GMSH_ELEMENTS_NUM 7
#define GMSH_ELEMENTS_TYPE 8
#define GMSH_ELEMENTS_REGPHYS 9
#define GMSH_ELEMENTS_REGELEM 10
#define GMSH_ELEMENTS_NUMTAGS 11
#define GMSH_ELEMENTS_TAGS 12
#define GMSH_ELEMENTS_NUMNODES 13
#define GMSH_ELEMENTS_NODELIST 14
#define GMSH_FORMAT 15
#define GMSH_SKIP_KEYWORD 16

namespace INMOST
{
  void Mesh::LoadMSH(std::string File)
  {
    int verbosity = 0;
		for(INMOST_DATA_ENUM_TYPE k = 0; k < file_options.size(); ++k)
		{
			if( file_options[k].first == "VERBOSITY" )
			{
				verbosity = atoi(file_options[k].second.c_str());
				if( verbosity < 0 || verbosity > 2 )
				{
					printf("%s:%d Unknown verbosity option: %s\n",__FILE__,__LINE__,file_options[k].second.c_str());
					verbosity = 1;
				}
			}
		}

		Tag gmsh_tags;
		char readline[2048], * p, * pend;
		std::vector<HandleType> newnodes;
		std::vector<HandleType> newelems;
54
		double xyz[3];
Kirill Terekhov's avatar
Kirill Terekhov committed
55
56
57
58
		int text_start, text_end, state = GMSH_NONE, ver = GMSH_VERNONE;
		int nnodes, ncells, nchars, nodenum, elemnum, elemtype, numtags, elemnodes, temp;
		int ascii, float_size, verlow, verhigh;
		char skip_keyword[2048];
59
60
		std::vector<int> elemtags;
		std::vector<int> nodelist;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
171
172
173
		ElementArray<Node> c_nodes(this);
		FILE * f = fopen(File.c_str(),"r");
		if( f == NULL )
		{
			std::cout << __FILE__ << ":" << __LINE__ << " cannot open " << File << std::endl;
			throw BadFileName;
		}
		std::vector<HandleType> old_nodes(NumberOfNodes());
		{
			unsigned qq = 0;
			for(Mesh::iteratorNode it = BeginNode(); it != EndNode(); ++it)
				old_nodes[qq++] = *it;
		}
		if( !old_nodes.empty() ) 
			std::sort(old_nodes.begin(),old_nodes.end(),CentroidComparator(this));
		int line = 0;
		unsigned report_pace;
		while( fgets(readline,2048,f) != NULL )
		{
			line++;
			if( readline[strlen(readline)-1] == '\n' ) readline[strlen(readline)-1] = '\0';
			text_end = static_cast<int>(strlen(readline));
			for(text_start = 0; isspace(readline[text_start]) && text_start < text_end; text_start++);
			if( text_start == text_end ) continue;
			for(text_end = text_end-1; isspace(readline[text_end]) && text_end > text_start; text_end--);
			readline[text_end+1] = '\0';
			p = readline + text_start;
			pend = readline + text_end + 1;
			switch(state)
			{
			case GMSH_NONE:
				if(!strcmp(p,"$NOD"))
				{
					ver = GMSH_VER1;
					state = GMSH_NODES_TOT;
					elemtags.resize(2);
					gmsh_tags = CreateTag("GMSH_TAGS",DATA_INTEGER,CELL|FACE|EDGE|NODE,FACE|EDGE|NODE,2);
				}
				else if( !strcmp(p,"$ELM") )
				{
					if( ver == GMSH_NONE )
					{
						std::cout << __FILE__ << ":" << __LINE__ <<  " bad keyword sequence in " << File << " line " << line << std::endl;
						throw BadFile;
					}
					state = GMSH_ELEMENTS_TOT;
				}
				else if( !strcmp(p,"$MeshFormat") )
				{
					ver = GMSH_VER2;
					state = GMSH_FORMAT;
					gmsh_tags = CreateTag("GMSH_TAGS",DATA_INTEGER,CELL|FACE|EDGE|NODE,FACE|EDGE|NODE);
				}
				else if( !strcmp(p,"$Nodes") )
				{
					ver = GMSH_VER2;
					state = GMSH_NODES_TOT;
					gmsh_tags = CreateTag("GMSH_TAGS",DATA_INTEGER,CELL|FACE|EDGE|NODE,FACE|EDGE|NODE);
				}
				else if( !strcmp(p,"$Elements") )
				{
					if( ver == GMSH_NONE )
					{
						std::cout << __FILE__ << ":" << __LINE__ <<  " bad keyword sequence in " << File << " line " << line <<std::endl;
						throw BadFile;
					}
					state = GMSH_ELEMENTS_TOT;
				}
				else
				{
					if( p[0] == '$' )
					{
						skip_keyword[0] = '$';
						skip_keyword[1] = 'E';
						skip_keyword[2] = 'n';
						skip_keyword[3] = 'd';
						for(int q = 1; !(p[q] == '\0' || isspace(p[q])); q++)
						{
							skip_keyword[q+3] = p[q];
							skip_keyword[q+4] = '\0';
						}
						state = GMSH_SKIP_KEYWORD;
						std::cout << __FILE__ << ":" << __LINE__ << " skipping unknown keyword " << skip_keyword+4 << " in " << File << " line " << line << std::endl;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ <<  " Unexpected line for " << File << " format: " << p << " line " << line <<std::endl;
						throw BadFile;
					}
				}
				break;
			case GMSH_SKIP_KEYWORD:
				if( !strncmp(p,skip_keyword,sizeof(skip_keyword) ) )
				{
					state = GMSH_NONE;
				}
				break;
			case GMSH_NODES_TOT:
				if( 1 == sscanf(p,"%d%n",&nnodes,&nchars) )
				{
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					newnodes.resize(nnodes);
					state = GMSH_NODES_NUM;
					report_pace = std::max<unsigned>(nnodes/250,1);
					if( verbosity > 0 ) printf("Reading %d nodes.\n",nnodes);
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading number of nodes in " << File << std::endl;
					throw BadFile;
				}
				if( p >= pend ) break;
174
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
			case GMSH_NODES_NUM:
read_node_num_link:
				if( 1 == sscanf(p,"%d%n",&nodenum,&nchars) )
				{
					--nodenum;
					if( nodenum >= static_cast<int>(newnodes.size()) ) std::cout << __FILE__ << ":" << __LINE__ << " node number is bigger then total number of nodes " << nodenum << " / " << newnodes.size() << " line " << line <<std::endl;
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					state = GMSH_NODES_X;
				}
				else
				{
					if( ver == GMSH_VER1 && !strcmp(p,"$ENDNOD"))
					{
						state = GMSH_NONE;
						if( nnodes ) std::cout << __FILE__ << ":" << __LINE__ << " number of node records mismatch with reported number of nodes " << nnodes << " in " << File << " line " << line <<std::endl;
						break;
					}
					else if( ver == GMSH_VER2 && !strcmp(p,"$EndNodes") )
					{
						state = GMSH_NONE;
						if( nnodes ) std::cout << __FILE__ << ":" << __LINE__ << " number of node records mismatch with reported number of nodes " << nnodes << " in " << File << " line " << line <<std::endl;
						break;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading node number in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
				}
				if( p >= pend ) break;
206
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
207
208
209
210
211
212
213
214
215
216
217
218
219
			case GMSH_NODES_X:
				if( 1 == sscanf(p,"%lf%n",xyz,&nchars) )
				{
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					state = GMSH_NODES_Y;
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading X-coord in " << File << " line " << line <<std::endl;
					throw BadFile;
				}
				if( p >= pend ) break;
220
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
221
222
223
224
225
226
227
228
229
230
231
232
233
			case GMSH_NODES_Y:
				if( 1 == sscanf(p,"%lf%n",xyz+1,&nchars) )
				{
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					state = GMSH_NODES_Z;
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading Y-coord in " << File << " line " << line <<std::endl;
					throw BadFile;
				}
				if( p >= pend ) break;
234
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
235
236
237
238
239
240
			case GMSH_NODES_Z:
				if( 1 == sscanf(p,"%lf%n",xyz+2,&nchars) )
				{
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					int find = -1;
241
242
					INMOST_DATA_REAL_TYPE rxyz[3];
					rxyz[0] = xyz[0], rxyz[1] = xyz[1], rxyz[2] = xyz[2];		
Kirill Terekhov's avatar
Kirill Terekhov committed
243
244
					if( !old_nodes.empty() )
					{
245
						std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),rxyz,CentroidComparator(this));
Kirill Terekhov's avatar
Kirill Terekhov committed
246
247
248
						if( it != old_nodes.end() ) 
						{
							Storage::real_array c = RealArrayDF(*it,CoordsTag());
249
							if( CentroidComparator(this).Compare(rxyz,c.data()) == 0 )
Kirill Terekhov's avatar
Kirill Terekhov committed
250
251
252
								find = static_cast<int>(it - old_nodes.begin());
						}
					}
253
					if( find == -1 ) newnodes[nodenum] = CreateNode(rxyz)->GetHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					else newnodes[nodenum] = old_nodes[find];
					nnodes--;
					if( verbosity > 1 && (newnodes.size()-nnodes)%report_pace == 0 )
					{
						printf("%3.1f%%\r",((newnodes.size()-nnodes)*100.0)/(newnodes.size()*1.0));
						fflush(stdout);
					}
					state = GMSH_NODES_NUM;
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading Z-coord in " << File << " line " << line <<std::endl;
					throw BadFile;
				}
				if( p < pend ) goto read_node_num_link; else break;
			case GMSH_ELEMENTS_TOT:
				if( 1 == sscanf(p,"%d%n",&ncells,&nchars) )
				{
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					newelems.resize(ncells);
					state = GMSH_ELEMENTS_NUM;
					report_pace = std::max<unsigned>(ncells/250,1);
					printf("Reading %d elements\n",ncells);
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading total number of elements in " << File << " line " << line <<std::endl;
					throw BadFile;
				}
				if( p >= pend ) break;
285
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			case GMSH_ELEMENTS_NUM:
read_elem_num_link:
				if( 1 == sscanf(p,"%d%n",&elemnum,&nchars) )
				{
					--elemnum;
					//std::cout << __FILE__ << ":" << __LINE__ << " element number: " << elemnum << " line " << line << std::endl;
					if( elemnum >= static_cast<int>(newelems.size()) ) std::cout << __FILE__ << ":" << __LINE__ << " element number is bigger then total number of elements " << elemnum << " / " << newelems.size() << " line " << line <<std::endl;
					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					state = GMSH_ELEMENTS_TYPE;
				}
				else
				{
					if( ver == GMSH_VER1 && !strcmp(p,"$ENDELM"))
					{
						if( ncells ) std::cout << __FILE__ << ":" << __LINE__ << " number of element records mismatch with reported number of elements " << ncells << " in " << File << " line " << line <<std::endl;
						state = GMSH_NONE;
						break;
					}
					else if( ver == GMSH_VER2 && !strcmp(p,"$EndElements") )
					{
						if( ncells ) std::cout << __FILE__ << ":" << __LINE__ << " number of element records mismatch with reported number of elements " << ncells << " in " << File << " line " << line <<std::endl;
						state = GMSH_NONE;
						break;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading element number in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
				}
				if( p >= pend ) break;
318
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
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
			case GMSH_ELEMENTS_TYPE:
				if( 1 == sscanf(p,"%d%n",&elemtype,&nchars) )
				{
					const int type_nodes[19] =
					{
						2,3,4,4,8,6,5,3,6,9,10,
						27,18,14,1,8,20,15,13
					};
					//std::cout << __FILE__ << ":" << __LINE__ << " element type: " << elemtype << " line " << line << std::endl;
					if( elemtype < 1 || elemtype > 19 )
					{
						std::cout << __FILE__ << ":" << __LINE__ << " bad element type " << elemtype << " expected in [1,19] range" << " line " << line <<std::endl;
						throw BadFile;
					}

					p += nchars;
					while(isspace(*p) && p < pend) ++p;
					if( ver == GMSH_VER1 )
						state = GMSH_ELEMENTS_REGPHYS;
					else if( ver == GMSH_VER2 )
						state = GMSH_ELEMENTS_NUMTAGS;

					elemnodes = type_nodes[elemtype-1];
					nodelist.resize(elemnodes);
				}
				else
				{
					std::cout << __FILE__ << ":" << __LINE__ << " error reading element type in " << File << " got " << p << " instead " << " line " << line <<std::endl;
					throw BadFile;
				}
				if( p >= pend ) break;
350
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
			case GMSH_ELEMENTS_REGPHYS:
				if( state == GMSH_ELEMENTS_REGPHYS )
				{
					if( 1 == sscanf(p,"%d%n",&elemtags[0],&nchars) )
					{
						//std::cout << __FILE__ << ":" << __LINE__ << " i should not be here " << std::endl;
						p += nchars;
						while(isspace(*p) && p < pend) ++p;
						state = GMSH_ELEMENTS_REGELEM;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading physical tag in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
					if( p >= pend ) break;
				}
368
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
			case GMSH_ELEMENTS_REGELEM:
				if( state == GMSH_ELEMENTS_REGELEM )
				{
					if( 1 == sscanf(p,"%d%n",&elemtags[1],&nchars) )
					{
						//std::cout << __FILE__ << ":" << __LINE__ << " i should not be here " << std::endl;
						p += nchars;
						while(isspace(*p) && p < pend) ++p;
						state = GMSH_ELEMENTS_NUMNODES;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading element tag in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
					if( p >= pend ) break;
				}
386
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
			case GMSH_ELEMENTS_NUMNODES:
				if( state == GMSH_ELEMENTS_NUMNODES )
				{
					if( 1 == sscanf(p,"%d%n",&temp,&nchars) )
					{
						//std::cout << __FILE__ << ":" << __LINE__ << " i should not be here " << std::endl;
						if( temp != elemnodes )
						{
							std::cout << __FILE__ << ":" << __LINE__ << " bad number of nodes " << temp << " for type " << elemtype << " expected " << elemnodes << " line " << line <<std::endl;
							throw BadFile;
						}
						p += nchars;
						while(isspace(*p) && p < pend) ++p;
						state = GMSH_ELEMENTS_NODELIST;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading number of nodes of element in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
					if( p >= pend ) break;
				}
409
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
			case GMSH_ELEMENTS_NUMTAGS:
				if( state == GMSH_ELEMENTS_NUMTAGS )
				{
					if( 1 == sscanf(p,"%d%n",&numtags,&nchars) )
					{
						//std::cout << __FILE__ << ":" << __LINE__ << " numtags: " << numtags << " line " << line << std::endl;
						elemtags.resize(numtags);
						p += nchars;
						while(isspace(*p) && p < pend) ++p;
						state = GMSH_ELEMENTS_TAGS;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading number of tags " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
					if( p >= pend ) break;
				}
428
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
			case GMSH_ELEMENTS_TAGS:
				if( state == GMSH_ELEMENTS_TAGS )
				{
					while( numtags > 0 && p < pend )
					{
						if( 1 == sscanf(p,"%d%n",&elemtags[elemtags.size()-numtags],&nchars) )
						{
							//std::cout << __FILE__ << ":" << __LINE__ << " tag[" << elemtags.size()-numtags << "] = " << elemtags[elemtags.size()-numtags] << " line " << line << std::endl;
							p += nchars;
							while(isspace(*p) && p < pend) ++p;
							if( --numtags == 0 ) state = GMSH_ELEMENTS_NODELIST;
						}
						else
						{
							std::cout << __FILE__ << ":" << __LINE__ << " error reading element tag in " << File << " got " << p << " instead " << " line " << line <<std::endl;
							throw BadFile;
						}
					}
					if( p >= pend ) break;
				}
449
				//FALLTHROUGH
Kirill Terekhov's avatar
Kirill Terekhov committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
			case GMSH_ELEMENTS_NODELIST:
				while( elemnodes > 0 && p < pend )
				{
					if( 1 == sscanf(p,"%d%n",&nodelist[nodelist.size()-elemnodes],&nchars) )
					{
						--nodelist[nodelist.size()-elemnodes];
						if( nodelist[nodelist.size()-elemnodes] < 0 || nodelist[nodelist.size()-elemnodes] >= static_cast<int>(newnodes.size()) )
						{
							std::cout << __FILE__ << ":" << __LINE__ << " got node " << nodelist[nodelist.size()-elemnodes] << " but must be within [0:" << newnodes.size()-1 << "] in file " << File << " line " << line << std::endl;
							throw BadFile;
						}
						p += nchars;
						while(isspace(*p) && p < pend) ++p;

						//std::cout << __FILE__ << ":" << __LINE__ << " node[" << nodelist.size()-elemnodes << "] = " << nodelist[nodelist.size()-elemnodes] << " line " << line << std::endl;



						if( --elemnodes == 0 ) 
						{
470
							for(size_t k = 0; k < nodelist.size(); ++k)
Kirill Terekhov's avatar
Kirill Terekhov committed
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
								c_nodes.push_back(newnodes[nodelist[k]]);

							//Create new element
							switch(elemtype)
							{
							case 1: newelems[elemnum] = CreateEdge(c_nodes).first->GetHandle(); break; //edge
							case 2: newelems[elemnum] = CreateFace(c_nodes).first->GetHandle(); break; //triangle
							case 3: newelems[elemnum] = CreateFace(c_nodes).first->GetHandle(); break; //quad
							case 4: //tetra
								{
									const integer nodesnum[12] = {0,2,1,0,1,3,1,2,3,0,3,2};
									const integer sizes[4] = {3,3,3,3};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,4).first->GetHandle();
								}
								break;
							case 5: //hex
								{
									//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 nodesnum[24] = {0,3,2,1,4,5,6,7,0,4,7,3,1,2,6,5,0,1,5,4,2,3,7,6};
									const integer sizes[6] = {4,4,4,4,4,4};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,6).first->GetHandle();
								}
								break;
							case 6: //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 nodesnum[18] = {0,3,5,2,1,2,5,4,0,1,4,3,3,4,5,0,2,1};
									const integer sizes[5] = {4,4,4,3,3};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							case 7: //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};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							case 8: //second order edge
								{
									//treat as just an edge
									newelems[elemnum] = CreateEdge(ElementArray<Node>(this,c_nodes.data(),c_nodes.data()+2)).first->GetHandle();
								}
								break;
							case 9: //second order tri with nodes on edges
								{
									newelems[elemnum] = CreateFace(ElementArray<Node>(this,c_nodes.data(),c_nodes.data()+3)).first->GetHandle();
								}
								break;
							case 10: //second order quad with nodes on edges and in center
								{
									newelems[elemnum] = CreateFace(ElementArray<Node>(this,c_nodes.data(),c_nodes.data()+4)).first->GetHandle();
								}
								break;
							case 11: // second order tet with nodes on edges
								{
									const integer nodesnum[12] = {0,2,1,0,1,3,1,2,3,0,3,2};
									const integer sizes[4] = {3,3,3,3};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,4).first->GetHandle();
								}
								break;
							case 12: // second order hex with nodes in centers of edges and faces and in center of volume
								{
									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};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,6).first->GetHandle();
								}
								break;
							case 13: // second order prism  with nodes in centers of edges and quad faces
								{
									const integer nodesnum[18] = {0,3,5,2,1,2,5,4,0,1,4,3,3,4,5,0,2,1};
									const integer sizes[5] = {4,4,4,3,3};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							case 14: // second order pyramid with nodes in centers of edges and quad faces
								{
									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};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							case 15: // vertex
								{
									newelems[elemnum] = c_nodes.at(0);
								}
								break;
							case 16: // second order quad with nodes on edges
								{
									newelems[elemnum] = CreateFace(ElementArray<Node>(this,c_nodes.data(),c_nodes.data()+4)).first->GetHandle();
								}
								break;
							case 17: // second order hex with nodes on edges
								{
									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};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,6).first->GetHandle();
								}
								break;
							case 18: // second order prism with nodes on edges
								{
									const integer nodesnum[18] = {0,3,5,2,1,2,5,4,0,1,4,3,3,4,5,0,2,1};
									const integer sizes[5] = {4,4,4,3,3};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							case 19: // second order pyramid with nodes on edges
								{
									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};
									newelems[elemnum] = CreateCell(c_nodes,nodesnum,sizes,5).first->GetHandle();
								}
								break;
							}

							Storage::integer_array ptags = IntegerArray(newelems[elemnum],gmsh_tags);

							if( ver == GMSH_VER2 )
							{
								ptags.replace(ptags.begin(),ptags.end(),elemtags.begin(),elemtags.end());
								elemtags.clear();
							}
							else
							{
								ptags[0] = elemtags[0];
								ptags[1] = elemtags[1];
							}

							c_nodes.clear();
							state = GMSH_ELEMENTS_NUM;
							ncells--;
							if( verbosity > 1 && (newelems.size()-ncells)%report_pace == 0 )
							{
								printf("%3.1f%%\r",((newelems.size()-ncells)*100.0)/(newelems.size()*1.0));
								fflush(stdout);
							}
						}
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " error reading node list for element in " << File << " got " << p << " instead " << " line " << line <<std::endl;
						throw BadFile;
					}
				}
				if( p < pend ) goto read_elem_num_link; else break;
			case GMSH_FORMAT:
				if( 4 == sscanf(p, "%1d.%1d %d %d\n",&verlow,&verhigh,&ascii,&float_size) )
				{
					if( !(verlow == 2 && verhigh == 0) && verbosity > 0)
					{
						std::cout << __FILE__ << ":" << __LINE__ << " version of file " << File << " is " << verlow << "." << verhigh << " expected 2.0 ";
						std::cout << " in " << File << " line " << line << std::endl;
					}
					if( ascii != 0 )
					{
						std::cout << __FILE__ << ":" << __LINE__ << " file " << File << " is binary, gmsh binary is not currently supported " << " line " << line <<std::endl;
						throw BadFile;
					}
				}
				else
				{
					if( !strcmp(p,"$EndMeshFormat") )
					{
						state = GMSH_NONE;
						break;
					}
					else
					{
						std::cout << __FILE__ << ":" << __LINE__ << " unexpected line content " << p << " in file " << File << " line " << line <<std::endl;
					}
				}
				break;
			}
		}

		if( state != GMSH_NONE )
		{
			std::cout << __FILE__ ":" << __LINE__ << " probably file " << File << " is not complitely written " << " line " << line <<std::endl;
		}

		fclose(f);
	}
}

655
#endif