model.cpp 14.1 KB
Newer Older
1
#include "inmost_model.h"
2
#if defined(USE_AUTODIFF) && defined(USE_MESH) && defined(USE_SOLVER)
3
4
5
6
7
8
9
10
11

namespace INMOST
{
	
	void Model::AddEntry(std::string name, AbstractEntry & entry)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetEntry(name) == NULL ); // do not overwrite entry with the same name
		Entries.push_back(std::make_pair(name,&entry));
Kirill Terekhov's avatar
updates    
Kirill Terekhov committed
12
13
14
15
16
17
18
	}
	
	void Model::AddFirstEntry(std::string name, AbstractEntry & entry)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetEntry(name) == NULL ); // do not overwrite entry with the same name
		Entries.insert(Entries.begin(),std::make_pair(name,&entry));
19
20
	}
	
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	void Model::AddAfterEntry(std::string name, AbstractEntry & entry, std::string after)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetEntry(name) == NULL ); // do not overwrite entry with the same name
		std::vector< std::pair< std::string, AbstractEntry *> >::iterator find = Entries.end();
		for(std::vector< std::pair< std::string, AbstractEntry *> >::iterator it = Entries.begin(); it != Entries.end(); ++it)
			if( it->first == after )
			{
				find = ++it;
				break;
			}
		Entries.insert(find,std::make_pair(name,&entry));
	}
	
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
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
198
199
	void Model::AddMesh(std::string name, Mesh & m)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetMesh(name) == NULL ); // do not overwrite mesh with the same name
		Meshes.push_back(std::make_pair(name,&m));
	}
	
	void Model::AddSubModel(std::string name, AbstractSubModel & submodel)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetSubModel(name) == NULL ); // do not overwrite submodel with the same name
		SubModels.push_back(std::make_pair(name,&submodel));
	}
	
	void Model::AddOperator(std::string name, AbstractOperator & op)
	{
		assert( !isInitialized() ); // do not add new data after initialization
		assert( GetOperator(name) == NULL ); // do not overwrite submodel with the same name
		Operators.push_back(std::make_pair(name,&op));
	}
	
	Mesh * Model::GetMesh(std::string name)
	{
		for(std::vector< std::pair<std::string,Mesh *> >::iterator it = Meshes.begin();
			it != Meshes.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	const Mesh * Model::GetMesh(std::string name) const
	{
		for(std::vector< std::pair<std::string,Mesh *> >::const_iterator it = Meshes.begin();
			it != Meshes.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	std::vector<std::string> Model::GetMeshesNames() const
	{
		std::vector<std::string> ret;
		for(std::vector< std::pair<std::string,Mesh *> >::const_iterator it = Meshes.begin();
			it != Meshes.end(); ++it)
			ret.push_back(it->first);
		return ret;
	}
	
	AbstractEntry * Model::GetEntry(std::string name)
	{
		for(std::vector< std::pair<std::string,AbstractEntry *> >::iterator it = Entries.begin();
			it != Entries.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	const AbstractEntry * Model::GetEntry(std::string name) const
	{
		for(std::vector< std::pair<std::string,AbstractEntry *> >::const_iterator it = Entries.begin();
			it != Entries.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	std::vector<std::string> Model::GetEntriesNames() const
	{
		std::vector<std::string> ret;
		for(std::vector< std::pair<std::string,AbstractEntry *> >::const_iterator it = Entries.begin();
			it != Entries.end(); ++it)
			ret.push_back(it->first);
		return ret;
	}
	
	AbstractSubModel * Model::GetSubModel(std::string name)
	{
		for(std::vector< std::pair<std::string,AbstractSubModel *> >::iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	const AbstractSubModel * Model::GetSubModel(std::string name) const
	{
		for(std::vector< std::pair<std::string,AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	std::vector<std::string> Model::GetSubModelsNames() const
	{
		std::vector<std::string> ret;
		for(std::vector< std::pair<std::string,AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			ret.push_back(it->first);
		return ret;
	}
	
	AbstractOperator * Model::GetOperator(std::string name)
	{
		for(std::vector< std::pair<std::string,AbstractOperator *> >::iterator it = Operators.begin();
			it != Operators.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	const AbstractOperator * Model::GetOperator(std::string name) const
	{
		for(std::vector< std::pair<std::string,AbstractOperator *> >::const_iterator it = Operators.begin();
			it != Operators.end(); ++it)
			if( it->first == name )
				return it->second;
		return NULL;
	}
	
	std::vector<std::string> Model::GetOperatorsNames() const
	{
		std::vector<std::string> ret;
		for(std::vector< std::pair<std::string,AbstractOperator *> >::const_iterator it = Operators.begin();
			it != Operators.end(); ++it)
			ret.push_back(it->first);
		return ret;
	}
	
	bool Model::PrepareEntries()
	{
		bool success = true;
		//first initialize submodels
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->PrepareEntries(*this);
		//second initialize operators
		for(std::vector< std::pair<std::string, AbstractOperator *> >::iterator it = Operators.begin();
			it != Operators.end(); ++it)
			success &= it->second->PrepareEntries(*this);
		return success;
	}
	
	bool Model::Initialize()
	{
		bool success = true;
		//first initialize submodels
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->Initialize(*this);
		//second initialize operators
		for(std::vector< std::pair<std::string, AbstractOperator *> >::iterator it = Operators.begin();
			it != Operators.end(); ++it)
			success &= it->second->Initialize(*this);
		//third register all the entries
		for(std::vector< std::pair<std::string, AbstractEntry *> >::iterator it = Entries.begin();
			it != Entries.end(); ++it)
		{
			it->second->reg_index = aut.RegisterEntry(*it->second);
			it->second->SetOffsetTag(aut.GetEntry(it->second->reg_index).GetOffsetTag());
		}
		initialized = success;
		return success;
	}
	
Kirill Terekhov's avatar
Update    
Kirill Terekhov committed
200
201
202
203
204
205
206
207
208
	bool Model::PrepareIterations()
	{
		bool success = true;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->PrepareIterations();
		return success;
	}
	
209
210
211
212
213
	bool Model::FillResidual(Residual & R) const
	{
		bool success = true;
		R.Clear();
		Automatizator::MakeCurrent(&aut);
214
		double total_time = Timer(), model_time = 0;
215
216
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
217
218
		{
			model_time = Timer();
219
			success &= it->second->FillResidual(R);
220
221
222
223
224
			model_time = Timer() - model_time;
			//~ std::cout << it->first << ":" << model_time << " ";
		}
		total_time = Timer() - total_time;
		//~ std::cout << "total:" << total_time << std::endl;
225
226
227
228
		Automatizator::RemoveCurrent();
		return success;
	}
	
229
	bool Model::UpdateSolution(const Sparse::Vector & sol, double alpha)
230
231
232
233
	{
		bool success = true;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
234
			success &= it->second->UpdateSolution(sol,alpha);
235
236
		for(std::vector< std::pair<std::string, AbstractOperator *> >::const_iterator it = Operators.begin();
			it != Operators.end(); ++it)
237
			success &= it->second->UpdateSolution(sol,alpha);
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
		return success;
	}
	
	bool Model::UpdateTimeStep()
	{
		bool success = true;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->UpdateTimeStep();
		for(std::vector< std::pair<std::string, AbstractOperator *> >::const_iterator it = Operators.begin();
			it != Operators.end(); ++it)
			success &= it->second->UpdateTimeStep();
		return success;
	}
	
	bool Model::SetTimeStep(double dt)
	{
		bool success = true;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->SetTimeStep(dt);
		return success;
		
	}
	
	bool Model::RestoreTimeStep()
	{
		bool success = true;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			success &= it->second->RestoreTimeStep();
		return success;
	}
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
	
	double Model::UpdateMultiplier(const Sparse::Vector & sol) const
	{
		double alpha = 1;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			alpha = std::min(alpha,it->second->UpdateMultiplier(sol));
#if defined(USE_MPI)
		double tmp = alpha;
		MPI_Allreduce(&tmp,&alpha,1,MPI_DOUBLE,MPI_MIN,MPI_COMM_WORLD);
#endif
		return alpha;
	}
	
	double Model::AdjustTimeStep(double dt) const
	{
		double ret = dt;
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			ret = std::min(ret,it->second->AdjustTimeStep(dt));
#if defined(USE_MPI)
		double tmp = ret;
		MPI_Allreduce(&tmp,&ret,1,MPI_DOUBLE,MPI_MIN,MPI_COMM_WORLD);
#endif
		return ret;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
	
	void Model::Adaptation(Mesh & m) const
	{
		array<HandleType> old_cells;
		for(int k = 0; k < m.CellLastLocalID(); ++k) if( m.isValidCell(k) )
		{
			Cell c = m.CellByLocalID(k);
			if( c.Hidden() ) old_cells.push_back(c.GetHandle());
		}
		//std::cout << "old cells: " << old_cells.size() << std::endl;
		SearchKDTree tree(&m,old_cells.data(),old_cells.size());
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->Adaptation(m,tree);
	}
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
	
	void Model::ReportErrors(const Residual & R) const
	{
		for(std::vector< std::pair< std::string, AbstractEntry *> >::const_iterator it = Entries.begin(); it != Entries.end(); ++it)
		{
			ElementType etypes = it->second->GetElementType();
			Mesh * m = it->second->GetMeshLink();
			//if( m->GetProcessorRank() == 0 )
			//	std::cout << "Errors for entry " << it->first << std::endl;
			for(ElementType etype = NODE; etype <= MESH; etype = NextElementType(etype) ) if ( etype & etypes )
			{
				if( !m->HaveGlobalID(etype) ) m->AssignGlobalID(etype); //to report element number
				//account for variable size!
				std::vector<double> err_comp(it->second->Size(),0.0);
				//std::vector<Element> err_elem(it->second->Size());
				std::vector<double> err_int(it->second->Size(),0.0);
				double max_err = 0, int_err = 0;
				Element e;
				for(Mesh::iteratorElement jt = m->BeginElement(etype); jt != m->EndElement(); ++jt) if( jt->GetStatus() != Element::Ghost )
				{
					rpMatrix err = R.Value(it->second->Index(jt->self()));
					double block_err = err.FrobeniusNorm();
					if( block_err > max_err )
					{
						e = jt->self();
						max_err = block_err;
					}
					int_err += block_err*block_err;
340
					INMOST_DATA_ENUM_TYPE N = err.Rows()*err.Cols();
341
342
					if( N > it->second->Size() ) continue; //No account for variable size
					//~ std::cout << jt->LocalID() << " block err " << block_err << " comp err ";
343
					for(INMOST_DATA_ENUM_TYPE k = 0; k < N; ++k)
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
					{
						//~ std::cout << err.data()[k] << " ";
						err_int[k] += err.data()[k]*err.data()[k];
						if( fabs(err.data()[k]) > err_comp[k] )
						{
							err_comp[k] = fabs(err.data()[k]);
							//err_elem[k] = jt->self();
						}
					}
					//~ std::cout << std::endl;
				}
				m->Integrate(&err_int[0],it->second->Size());
				m->AggregateMax(&err_comp[0],it->second->Size());
				int_err = m->Integrate(int_err);
				max_err = m->AggregateMax(max_err); //we need MPI_Allreduce to zero proc to know index of element with largest error
				if( m->GetProcessorRank() == 0 )
				{
					std::cout << std::setw(30) << it->first << " element type " << ElementTypeName(etype) << " error integral " << std::setw(12) << sqrt(int_err) << " maximal " << std::setw(12) << max_err << std::endl;// " on element " << e.GlobalID() << std::endl;
					for(int k = 0; k < (int)err_int.size(); ++k)
					{
						std::cout << "\t\t" << std::setw(2) << k << " error integral " << std::setw(12) << sqrt(err_int[k]) << " maximal " << std::setw(12) << err_comp[k] << std::endl;
					}
				}
			}
		}
	}
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
	
	void Model::CellRefinement(Cell & old_cell, ElementArray<Cell> & new_cells)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->CellRefinement(old_cell,new_cells);
	}
	void Model::FaceRefinement(Face & old_face, ElementArray<Face> & new_faces)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->FaceRefinement(old_face,new_faces);
	}
	void Model::EdgeRefinement(Edge & old_edge, ElementArray<Edge> & new_edges)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->EdgeRefinement(old_edge,new_edges);
	}

	
	void Model::CellCoarsening(ElementArray<Cell> & old_cells, Cell & new_cell)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->CellCoarsening(old_cells,new_cell);
	}
	void Model::FaceCoarsening(ElementArray<Face> & old_faces, Face & new_face)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->FaceCoarsening(old_faces,new_face);
	}
	void Model::EdgeCoarsening(ElementArray<Edge> & old_edges, Edge & new_edge)
	{
		for(std::vector< std::pair<std::string, AbstractSubModel *> >::const_iterator it = SubModels.begin();
			it != SubModels.end(); ++it)
			it->second->EdgeCoarsening(old_edges,new_edge);
	}
409
}
410
#endif