mesh_mkf_file.cpp 9.23 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1
2
3
4
5
6
7
8
9
10
#ifdef _MSC_VER //kill some warnings
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "inmost.h"

#if defined(USE_MESH)

namespace INMOST
{
Kirill Terekhov's avatar
Kirill Terekhov committed
11
12
13
14
	// mesh format by Mohammad Karimi-Fard
	void Mesh::LoadMKF(std::string File)
	{
		int verbosity = 0;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		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 volume_factor, porosity, permiability, zone;
		zone = CreateTag("MATERIAL",DATA_INTEGER,CELL|FACE|ESET,ESET,1);
		volume_factor = CreateTag("VOLUME_FACTOR",DATA_REAL,CELL|FACE,FACE,1);
		porosity = CreateTag("PORO",DATA_REAL,CELL|FACE,FACE,1);
		permiability = CreateTag("PERM",DATA_REAL,CELL|FACE,FACE,9);
		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));

		FILE * f = fopen(File.c_str(),"r");
		if( f == NULL ) 
		{
			std::cout << __FILE__ << ":" << __LINE__ << " cannot open " << File << std::endl;
			throw BadFileName;
		}
		int nbnodes, nbpolygon, nbpolyhedra, nbzones, volcorr, nbfacenodes, nbpolyhedronfaces, num, nbK;
		int report_pace;
		Storage::real K[9],readK[9], poro, vfac;
		std::vector<HandleType> newnodes;
		std::vector<HandleType> newpolygon;
		std::vector<HandleType> newpolyhedron;
Kirill Terekhov's avatar
Kirill Terekhov committed
54
55
56
		std::vector<INMOST_DATA_REAL_TYPE> poro_zones;
		std::vector<INMOST_DATA_REAL_TYPE> factor_zones;
		std::vector<INMOST_DATA_REAL_TYPE> perm_zones;
Kirill Terekhov's avatar
Kirill Terekhov committed
57
58
59
		ElementArray<ElementSet> newsets(this);
		ElementArray<Node> f_nodes(this);
		ElementArray<Face> c_faces(this);
Kirill Terekhov's avatar
Kirill Terekhov committed
60
61
		int ret = fscanf(f," %d %d %d %d %d",&nbnodes,&nbpolygon,&nbpolyhedra,&nbzones,&volcorr);
		if( ret != 5 ) throw BadFile;
Kirill Terekhov's avatar
Kirill Terekhov committed
62
63
64
		newnodes.resize(nbnodes);
		newpolygon.resize(nbpolygon);
		newpolyhedron.resize(nbpolyhedra);
Kirill Terekhov's avatar
Kirill Terekhov committed
65
66
67
68
		poro_zones.resize(nbzones);
		factor_zones.resize(nbzones);
		perm_zones.resize(nbzones*9);
		/*
Kirill Terekhov's avatar
Kirill Terekhov committed
69
70
71
72
73
		newsets.resize(nbzones);
		if( verbosity > 0 ) printf("Creating %d sets for zones.\n",nbzones);
		report_pace = std::max<int>(nbzones/250,1);
		for(int i = 0; i < nbzones; i++)
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
74
75
76
77
78
79
80
81
82
		std::stringstream str;
		str << "ZONE_" << i << "_SET";
		newsets[i] = CreateSetUnique(str.str()).first;
		newsets[i]->Integer(zone) = i;
		if( verbosity > 1 &&  i % report_pace == 0 )
		{
		printf("sets %3.1f%%\r",(i*100.0)/(1.0*nbzones));
		fflush(stdout);
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
83
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
84
		*/
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		if( verbosity > 0 ) printf("Reading %d nodes.\n",nbnodes);
		report_pace = std::max<int>(nbnodes/250,1);
		for(int i = 0; i < nbnodes; i++)
		{
			Storage::real xyz[3];
			if( 3 == fscanf(f," %lf %lf %lf",xyz,xyz+1,xyz+2) )
			{
				int find = -1;
				if( !old_nodes.empty() )
				{
					std::vector<HandleType>::iterator it = std::lower_bound(old_nodes.begin(),old_nodes.end(),xyz,CentroidComparator(this));
					if( it != old_nodes.end() ) 
					{
						Storage::real_array c = RealArrayDF(*it,CoordsTag());
						if( CentroidComparator(this).Compare(xyz,c.data()) == 0 )
							find = static_cast<int>(it - old_nodes.begin());
					}
				}
				if( find == -1 ) newnodes[i] = CreateNode(xyz)->GetHandle();
				else newnodes[i] = old_nodes[find];
			}
			else
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read coordinates from " << File << std::endl;
				throw BadFile;
			}
			if( verbosity > 1 &&  i % report_pace == 0 )
			{
				printf("nodes %3.1f%%\r",(i*100.0)/(1.0*nbnodes));
				fflush(stdout);
			}
		}
		if( verbosity > 0 ) printf("Reading %d faces.\n",nbpolygon);
		report_pace = std::max<int>(nbpolygon/250,1);
		for(int i = 0; i < nbpolygon; i++)
		{
			if( 1 != fscanf(f," %d",&nbfacenodes) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read number of nodes from " << File << std::endl;
				throw BadFile;
			}
			for(int j = 0; j < nbfacenodes; j++)
			{
				if( 1 != fscanf(f," %d", &num) )
				{
					std::cout << __FILE__ << ":" << __LINE__ << " cannot read node number from " << File << std::endl;
					throw BadFile;
				}
				if( num < 0 || num >= nbnodes )
				{
					std::cout << __FILE__ << ":" << __LINE__ << " node number is out of range: " << num << "/" << nbnodes << std::endl;
					throw BadFile;
				}
				f_nodes.push_back(newnodes[num]);
			}
			if( 1 != fscanf(f," %d",&num) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read zone number from " << File << std::endl;
				throw BadFile;
			}
			if( num >= nbzones )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " zone number is out of range: " << num << "/" << nbzones << std::endl;
				throw BadFile;
			}
			Face f = CreateFace(f_nodes).first;
			f->Integer(zone) = num;
Kirill Terekhov's avatar
Kirill Terekhov committed
152
			//if( num >= 0 ) newsets[num]->PutElement(f);
Kirill Terekhov's avatar
Kirill Terekhov committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
			newpolygon[i] = f->GetHandle();
			f_nodes.clear();

			if( verbosity > 1 &&  i % report_pace == 0 )
			{
				printf("faces %3.1f%%\r",(i*100.0)/(1.0*nbpolygon));
				fflush(stdout);
			}
		}
		if( verbosity > 0 ) printf("Reading %d cells.\n",nbpolyhedra);
		report_pace = std::max<int>(nbpolyhedra/250,1);
		for(int i = 0; i < nbpolyhedra; i++)
		{
			if( 1 != fscanf(f," %d",&nbpolyhedronfaces) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read number of faces from " << File << std::endl;
				throw BadFile;
			}
			for(int j = 0; j < nbpolyhedronfaces; j++)
			{
				if( 1 != fscanf(f," %d", &num) )
				{
					std::cout << __FILE__ << ":" << __LINE__ << " cannot read face number from " << File << std::endl;
					throw BadFile;
				}
				if( num < 0 || num >= nbpolygon )
				{
					std::cout << __FILE__ << ":" << __LINE__ << " face number is out of range: " << num << "/" << nbpolygon << std::endl;
					throw BadFile;
				}
				c_faces.push_back(newpolygon[num]);
			}
			if( 1 != fscanf(f," %d",&num) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read face number from " << File << std::endl;
				throw BadFile;
			}
			if( num >= nbzones )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " zone number is out of range: " << num << "/" << nbzones << std::endl;
				throw BadFile;
			}
			Cell c = CreateCell(c_faces).first;
			c->Integer(zone) = num;
			newpolyhedron[i] = c->GetHandle();
Kirill Terekhov's avatar
Kirill Terekhov committed
198
			//if( num >= 0 ) newsets[num]->PutElement(newpolyhedron[i]);
Kirill Terekhov's avatar
Kirill Terekhov committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
			c_faces.clear();

			if( verbosity > 1 &&  i % report_pace == 0 )
			{
				printf("cells %3.1f%%\r",(i*100.0)/(1.0*nbpolyhedra));
				fflush(stdout);
			}

		}
		report_pace = std::max<int>(nbzones/250,1);
		if( verbosity > 0 ) printf("Reading %d zones data.\n",nbzones);
		for(int i = 0; i < nbzones; i++)
		{
			if( 4 != fscanf(f," %d %lf %lf %d",&num,&vfac,&poro,&nbK) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " cannot read zone data from " << File << std::endl;
				throw BadFile;
			}
			if( num < 0 || num >= nbzones )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " zone number out of range " << num << "/" << nbzones << " in file "  << File << std::endl;
				throw BadFile;
			}
			if( !(nbK == 1 || nbK == 2 || nbK == 3 || nbK == 6 || nbK == 9) )
			{
				std::cout << __FILE__ << ":" << __LINE__ << " strange size for permiability tensor: " << nbK << " in file "  << File << std::endl;
				throw BadFile;
			}
			for(int j = 0; j < nbK; j++)
			{
				if( 1 != fscanf(f," %lf",readK+j) )
				{
					std::cout << __FILE__ << ":" << __LINE__ << " cannot read permiability component " << j << "/" << nbK << " from " << File << std::endl;
					throw BadFile;
				}
			}
			memset(K,0,sizeof(Storage::real)*9);
			switch(nbK)
			{
			case 1:
				K[0] = K[4] = K[8] = readK[0];
				break;
			case 2:
				K[0] = readK[0];
				K[4] = readK[1];
				K[8] = 1; //eigenvalue should not be zero
			case 3:
				K[0] = readK[0];
				K[4] = readK[1];
				K[8] = readK[2];
				break;
			case 6:
				K[0] = readK[0];
				K[4] = readK[1];
				K[8] = readK[2];
				K[1] = K[3] = readK[3];
				K[2] = K[6] = readK[4];
				K[5] = K[7] = readK[5];
				break;
			case 9:
				//just copy
				memcpy(K,readK,sizeof(Storage::real)*9);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
262
			/*
Kirill Terekhov's avatar
Kirill Terekhov committed
263
264
			for(ElementSet::iterator it = newsets[num]->Begin(); it != newsets[num]->End(); ++it)
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
265
266
267
			it->Real(volume_factor) = vfac;
			it->Real(porosity) = poro;
			memcpy(it->RealArray(permiability).data(),K,sizeof(Storage::real)*9);
Kirill Terekhov's avatar
Kirill Terekhov committed
268
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
269
270
271
272
273
			*/
			poro_zones[num] = poro;
			factor_zones[num] = vfac;
			for(int l = 0; l < 9; ++l)
				perm_zones[num*9+l] = K[l];
Kirill Terekhov's avatar
Kirill Terekhov committed
274
275
276
277
278
279
			if( verbosity > 1 &&  i % report_pace == 0 )
			{
				printf("data %3.1f%%\r",(i*100.0)/(1.0*nbzones));
				fflush(stdout);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
		for(int i = 0; i < nbpolygon; ++i)
		{
			int z = Integer(newpolygon[i],zone);
			if( z >= 0 )
			{
				Real(newpolygon[i],volume_factor) = factor_zones[z];
				Real(newpolygon[i],porosity) = poro_zones[z];
				memcpy(RealArray(newpolygon[i],permiability).data(),&perm_zones[z*9],sizeof(Storage::real)*9);
			}
		}
		for(int i = 0; i < nbpolyhedra; ++i)
		{
			int z = Integer(newpolyhedron[i],zone);
			if( z >= 0 )
			{
				Real(newpolyhedron[i],volume_factor) = factor_zones[z];
				Real(newpolyhedron[i],porosity) = poro_zones[z];
				memcpy(RealArray(newpolyhedron[i],permiability).data(),&perm_zones[z*9],sizeof(Storage::real)*9);
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
300
301
302
303
304
305
306
		//TEMPORARY
		//for(int i = 0; i < nbzones; i++) newsets[i]->Delete();

		fclose(f);
	}
}

Kirill Terekhov's avatar
Kirill Terekhov committed
307
#endif