SMIAdapter.cc 16.9 KB
Newer Older
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "SMIAdapter.h"
#include "Mesh.h"
#include "Traverse.h"
#include "ElInfo.h"
#include "Flag.h"
#include "FiniteElemSpace.h"
#include "BasisFunction.h"
#include "DOFVector.h"
#include "DOFAdmin.h"
#include "ElementRegion_ED.h"
#include "SurfaceRegion_ED.h"
#include "Flag.h"

namespace AMDiS {

  SMIAdapter::SMIAdapter(int              smiApplicationId,
			 int              smiMeshId,
			 FiniteElemSpace *feSpace,
			 int              elementRegion,
			 int              surfaceRegion,
			 bool (*elementFct)(ElInfo *elInfo),
			 bool (*surfaceFct)(ElInfo *elInfo, int side)) 
    : smiApplicationId_(smiApplicationId), 
      smiMeshId_(smiMeshId),
      feSpace_(feSpace),
      elementRegion_(elementRegion),
      surfaceRegion_(surfaceRegion),
      addNeighbourInfo_(false),
      newNodeIndex_(NULL),
      oldNodeIndex_(NULL),
      newElementIndex_(NULL),
      oldElementIndex_(NULL),
      elementFct_(elementFct),
      surfaceFct_(surfaceFct)
  {
    TEST_EXIT(!surfaceFct_)("surfaceFct not yet supported\n");
    TEST_EXIT(elementFct_ == NULL || elementRegion_ == -1)
      ("don't use elementRegion AND elementFct\n");

    Mesh *mesh = feSpace_->getMesh();
    int dim = mesh->getDim();

    if(surfaceRegion > -1) {
      dim -= 1;
    }

    TEST_EXIT(dim > 0 && dim < 4)("invalid dim\n");

    elementType_ = dim * 10 + 1; // linear elements only

    int smiError = SMI_Add_application(smiApplicationId_,
				       SMI_APP_MODE_DEFAULT);

    smiError = SMI_Add_mesh(smiApplicationId_,
			    smiMeshId_, SMI_MESH_MODE_DEFAULT);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Add_mesh() failed with error %d\n", smiError);

    smiError = SMI_Begin_write_transaction(smiApplicationId_,
					   smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    smiError = SMI_Add_elem_type(smiApplicationId_,
				 smiMeshId_,
				 elementType_,
				 dim + 1);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Add_elem_type() failed with error %d\n", smiError);

    smiError = SMI_End_write_transaction(smiApplicationId_,
					 smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::addQuantity(int quantityId, DOFVector<double> *dofVector) {
    TEST_EXIT(dofVectors_[quantityId].size() == 0)
      ("quantityId already added\n");

    dofVectors_[quantityId].push_back(dofVector);

    static double defaultValue = 0.0;

    int smiError;

    smiError = SMI_Begin_write_transaction(smiApplicationId_,
					   smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    smiError = SMI_Add_quantity(smiApplicationId_,
				smiMeshId_,
				quantityId,
				SMI_LOC_NODE,
				SMI_TYPE_DOUBLE,
				1,
				&defaultValue);
      
    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Add_quantity faild with error %d\n", smiError);

    smiError = SMI_End_write_transaction(smiApplicationId_,
					 smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::addQuantity(int quantityId,
			       int quantityDim,
			       DOFVector<double> **dofVector) 
  {
    TEST_EXIT(dofVectors_[quantityId].size() == 0)
      ("quantityId already added\n");

    dofVectors_[quantityId].resize(quantityDim);

    int comp;
    for(comp = 0; comp < quantityDim; comp++) {
      dofVectors_[quantityId][comp] = dofVector[comp];
    }

    TEST_EXIT(quantityDim < 4)("quantityDim > 3\n");

    static double defaultValue[3] = {0.0, 0.0, 0.0};

    int smiError;

    smiError = SMI_Begin_write_transaction(smiApplicationId_,
					   smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    smiError = SMI_Add_quantity(smiApplicationId_,
				smiMeshId_,
				quantityId,
				SMI_LOC_NODE,
				SMI_TYPE_DOUBLE,
				quantityDim,
				defaultValue);
      
    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Add_quantity faild with error %d\n", smiError);

    smiError = SMI_End_write_transaction(smiApplicationId_,
					 smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::transferMeshToSMI()
  {
    int smiError = SMI_Begin_write_transaction(smiApplicationId_,
					       smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    smiError = SMI_Clear_mesh(smiApplicationId_,
			      smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Clear_mesh() failed with error %d\n", smiError);

    int i, j;
    Mesh *mesh = feSpace_->getMesh();
    int dim = mesh->getDim();
    DOFAdmin *admin = feSpace_->getAdmin();
    const BasisFunction *basFcts = feSpace_->getBasisFcts();
    int numBasFcts = basFcts->getNumber();

    Element *element;
    ElementData *elementData;
    ElementData *regionData;
    int region;
    int side;
    int numNewNodes;
    int numNodes;
    bool validElement;
188
    double *nodeCoords = new double[numBasFcts * dim]; 
189
190
191
192
193
194
195
196

    DOFVector<char> alreadyAdded(feSpace_, "already added nodes");
    alreadyAdded.set(0);

    int elementCounter[1] = { 0 };
    int elementIndex;

    DimVec<double> *bary = NULL;
197
    WorldVector<double> world;
198
199
200

    const DegreeOfFreedom *elementDofs = NULL;
    DegreeOfFreedom dof;
201
202
    DegreeOfFreedom *nodeIndices = new int[numBasFcts];
    DegreeOfFreedom *newNodeIndices = new int[numBasFcts];
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

    Flag fillFlag = Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS;
  
    if(addNeighbourInfo_) {
      fillFlag |= Mesh::FILL_NEIGH;
    }

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, fillFlag);

    while(elInfo) {
    
      element = elInfo->getElement();
      elementData = element->getElementData();
      elementDofs = basFcts->getLocalIndices(element, admin, NULL);

      // check element region
      if(elementRegion_ > -1) {
	regionData = elementData->getElementData(ELEMENT_REGION);
	if(regionData) {
	  region = dynamic_cast<ElementRegion_ED*>(regionData)->getRegion();
	} else {
	  region = -1;
	}
	validElement = (region == elementRegion_);
      } else {
	validElement = true;
      }

      if(elementFct_) {
	validElement = (*elementFct_)(elInfo);
      }
    
      if(validElement) {

	// check surface region
	if(surfaceRegion_ > -1) {
	  regionData = elementData->getElementData(SURFACE_REGION);
	  while(regionData) {
	    region = dynamic_cast<SurfaceRegion_ED*>(regionData)->getRegion();

	    // surface region found ?
	    if(region == surfaceRegion_) {
	      // add surface element
	      side = dynamic_cast<SurfaceRegion_ED*>(regionData)->getSide();
	      numNewNodes = 0;
	      numNodes = 0;
	      for(i = 0; i < numBasFcts; i++) {
		bary = basFcts->getCoords(i);
		if((*bary)[side] == 0) {
		  dof = elementDofs[i];

		  if(newNodeIndex_) {
		    dof = (*(newNodeIndex_))[dof] - 1;
		    TEST_EXIT(dof >= 0)("invalid node index\n");
		  }
		
		  if(!alreadyAdded[dof]) {
		    newNodeIndices[numNewNodes] = dof;
262
		    elInfo->coordToWorld(*bary, world);
263
		    for(j = 0; j < dim; j++) {
264
		      nodeCoords[numNewNodes * dim + j] = world[j];
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
		    }
		    numNewNodes++;
		    alreadyAdded[dof] = 1;
		  }
		  nodeIndices[numNodes] = dof;
		  numNodes++;
		}
	      }

	      smiError = SMI_Add_nodes(smiApplicationId_,
				       smiMeshId_,
				       numNewNodes,
				       newNodeIndices,
				       nodeCoords,
				       dim);

	      TEST_EXIT(smiError == SMI_OK)
		("SMI_Add_nodes faild with error %d\n", smiError);

	      smiError = SMI_Add_elems(smiApplicationId_,
				       smiMeshId_,
				       1,
				       &elementType_,
				       dim+1,
				       nodeIndices,
				       elementCounter);

	      (elementCounter[0])++;

	      TEST_EXIT(smiError == SMI_OK)
		("SMI_Add_elems faild with error %d\n", smiError);
	    }
	    regionData = regionData->getDecorated(SURFACE_REGION);
	  }
	} else { 
	  // add volume element


	  numNewNodes = 0;
	  for(i = 0; i < numBasFcts; i++) {
	    bary = basFcts->getCoords(i);
	    dof = elementDofs[i];

	    if(newNodeIndex_) {
	      dof = (*(newNodeIndex_))[dof] - 1;
	      TEST_EXIT(dof >= 0)("invalid node index\n");
	    }

	    if(!alreadyAdded[dof]) {
	      newNodeIndices[numNewNodes] = dof;
315
	      elInfo->coordToWorld(*bary, world);
316
	      for(j = 0; j < dim; j++) {
317
		nodeCoords[numNewNodes * dim + j] = world[j];
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
	      }
	      numNewNodes++;
	      alreadyAdded[dof] = 1;
	    }
	  }

	  smiError = SMI_Add_nodes(smiApplicationId_,
				   smiMeshId_,
				   numNewNodes,
				   newNodeIndices,
				   nodeCoords,
				   dim);

	  TEST_EXIT(smiError == SMI_OK)
	    ("SMI_Add_nodes faild with error %d\n", smiError);

	  elementIndex = element->getIndex();

	  if(newElementIndex_) {
	    elementIndex = (*(newElementIndex_))[elementIndex] - 1;
	    TEST_EXIT(elementIndex >= 0)("invalid element index\n");
	  }

	  smiError = SMI_Add_elems(smiApplicationId_,
				   smiMeshId_,
				   1,
				   &elementType_,
				   dim+1,
				   const_cast<int*>(elementDofs),
				   &elementIndex);

	  if(addNeighbourInfo_) {
	    DimVec<int> neighbours(dim, NO_INIT);
	    DimVec<int> oppVertices(dim, NO_INIT);
	    for(i = 0; i < dim + 1; i++) {
	      if(elInfo->getNeighbour(i)) {
		neighbours[i] = elInfo->getNeighbour(i)->getIndex();
		if(newElementIndex_) {
		  neighbours[i] = (*(newElementIndex_))[neighbours[i]] - 1;
		  TEST_EXIT(neighbours[i] >= 0)("invalid element index\n");
		}
		oppVertices[i] = elInfo->getOppVertex(i);
	      } else {
		neighbours[i] = -1;
		oppVertices[i] = -1;
	      }
	    }
	    SMI_Set_quantity_values(smiApplicationId_,
				    smiMeshId_,
				    SMI_NEIGH_QUANTITY,
				    SMI_TYPE_INT,
				    dim+1,
				    1,
				    &elementIndex,
				    neighbours.getValArray());
	    SMI_Set_quantity_values(smiApplicationId_,
				    smiMeshId_,
				    SMI_OPP_V_QUANTITY,
				    SMI_TYPE_INT,
				    dim+1,
				    1,
				    &elementIndex,
				    oppVertices.getValArray());
	  }


	  (elementCounter[0])++;
	
	  TEST_EXIT(smiError == SMI_OK)
	    ("SMI_Add_elems faild with error %d\n", smiError);
	}
      }
    
      elInfo = stack.traverseNext(elInfo);
    }
  
394
395
396
    delete [] nodeCoords;
    delete [] nodeIndices;
    delete [] newNodeIndices;
397

398
    smiError = SMI_End_write_transaction(smiApplicationId_, smiMeshId_);
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

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::transferQuantitiesToSMI(int quantityID)
  {
    int smiError;
  
    smiError = SMI_Begin_write_transaction(smiApplicationId_,
					   smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    int  numNodes;
    int *nodeIndices;

    smiError = SMI_Get_all_nodes(smiApplicationId_,
				 smiMeshId_,
				 &numNodes,
				 &nodeIndices);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Get_all_nodes() failed with error %d\n", smiError);

425
    if (quantityID == -1) {
426
427
      std::map<int, std::vector<DOFVector<double>*> >::iterator quantityIt;
      std::map<int, std::vector<DOFVector<double>*> >::iterator quantityEnd = 
428
429
430
431
432
	  dofVectors_.end();

      for(quantityIt = dofVectors_.begin(); quantityIt != quantityEnd; ++quantityIt) {
	int quantityDim = static_cast<int>(quantityIt->second.size());

433
	double *values = new double[numNodes * quantityDim];
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

	int i, comp;
	for(i = 0; i < numNodes; i++) {
	  DegreeOfFreedom index = nodeIndices[i];
	  if(oldNodeIndex_) {
	    index = (*(oldNodeIndex_))[index] - 1;
	  }
	  for(comp = 0; comp < quantityDim; comp++) {
	    DOFVector<double> *dofVector = (quantityIt->second)[comp];
	    values[i * quantityDim + comp] = (*dofVector)[index];
	  }
	}

	smiError = SMI_Set_quantity_values(smiApplicationId_,
					   smiMeshId_,
					   (*quantityIt).first,
					   SMI_TYPE_DOUBLE,
					   quantityDim,
					   numNodes,
					   nodeIndices,
					   values);

	TEST_EXIT(smiError == SMI_OK)
	  ("SMI_Set_quantity_values() failed with error %d\n", smiError);
      
459
	delete [] values;
460
461
462
463
      }
    } else {
      int quantityDim = static_cast<int>(dofVectors_[quantityID].size());

464
      double *values = new double[numNodes * quantityDim];
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

      int i, comp;
      for(i = 0; i < numNodes; i++) {
	DegreeOfFreedom index = nodeIndices[i];
	if(oldNodeIndex_) {
	  index = (*(oldNodeIndex_))[index] - 1;
	}
	for(comp = 0; comp < quantityDim; comp++) {
	  DOFVector<double> *dofVector = dofVectors_[quantityID][comp];
	  values[i*quantityDim + comp] = (*dofVector)[index];
	}
      }
	
      smiError = SMI_Set_quantity_values(smiApplicationId_,
					 smiMeshId_,
					 quantityID,
					 SMI_TYPE_DOUBLE,
					 quantityDim,
					 numNodes,
					 nodeIndices,
					 values);

      TEST_EXIT(smiError == SMI_OK)
	("SMI_Set_quantity_values() failed with error %d\n", smiError);

490
      delete [] values;
491
492
    }

493
    smiError = SMI_End_write_transaction(smiApplicationId_, smiMeshId_);
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

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::getQuantitiesFromSMI(int quantityID)
  {
    int smiError;
  
    smiError = SMI_Begin_read_transaction(smiApplicationId_,
					  smiMeshId_);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Begin_modification() failed with error %d\n", smiError);

    int  numNodes;
    int *nodeIndices;

    smiError = SMI_Get_all_nodes(smiApplicationId_,
				 smiMeshId_,
				 &numNodes,
				 &nodeIndices);

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_Get_all_nodes() failed with error %d\n", smiError);

520
    if (quantityID == -1) {
521
522
      std::map<int, std::vector<DOFVector<double>*> >::iterator quantityIt;
      std::map<int, std::vector<DOFVector<double>*> >::iterator quantityEnd = 
523
524
525
526
	  dofVectors_.end();
      for(quantityIt = dofVectors_.begin(); quantityIt != quantityEnd; ++quantityIt) {
	int quantityDim = static_cast<int>(quantityIt->second.size());

527
	double *values = new double[numNodes * quantityDim];
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

	smiError = SMI_Get_quantity_values(smiApplicationId_,
					   smiMeshId_,
					   (*quantityIt).first,
					   SMI_TYPE_DOUBLE,
					   quantityDim,
					   numNodes,
					   nodeIndices,
					   values);

	TEST_EXIT(smiError == SMI_OK)
	  ("SMI_Set_quantity_values() failed with error %d\n", smiError);

	int i, comp;
	for(i = 0; i < numNodes; i++) {
	  DegreeOfFreedom index = nodeIndices[i];
	  if(oldNodeIndex_) {
	    index = (*(oldNodeIndex_))[index] - 1;
	  }
	  for(comp = 0; comp < quantityDim; comp++) {
	    DOFVector<double> *dofVector = (quantityIt->second)[comp];
	    (*dofVector)[index] = values[i * quantityDim + comp];
	  }
	}

553
	delete [] values;
554
555
556
557
      }
    } else {
      int quantityDim = static_cast<int>(dofVectors_[quantityID].size());

558
      double *values = new double[numNodes * quantityDim];
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

      smiError = SMI_Get_quantity_values(smiApplicationId_,
					 smiMeshId_,
					 quantityID,
					 SMI_TYPE_DOUBLE,
					 quantityDim,
					 numNodes,
					 nodeIndices,
					 values);

      TEST_EXIT(smiError == SMI_OK)
	("SMI_Set_quantity_values() failed with error %d\n", smiError);
    
      int i, comp;
      for(i = 0; i < numNodes; i++) {
	DegreeOfFreedom index = nodeIndices[i];
	if(oldNodeIndex_) {
	  index = (*(oldNodeIndex_))[index] - 1;
	}
	for(comp = 0; comp < quantityDim; comp++) {
	  DOFVector<double> *dofVector = dofVectors_[quantityID][comp];
	  (*dofVector)[index] = values[i * quantityDim + comp];
	}
      }

584
      delete [] values;
585
586
    }

587
    smiError = SMI_End_read_transaction(smiApplicationId_, smiMeshId_);
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

    TEST_EXIT(smiError == SMI_OK)
      ("SMI_End_modification() failed with error %d\n", smiError);
  }

  void SMIAdapter::addNeighbourInfo() 
  {
    TEST_EXIT(surfaceRegion_ == -1)
      ("no neighbour info available for surface meshes\n");

    int dim = feSpace_->getMesh()->getDim();

    if(SMI_OK != SMI_Get_quantity_info(smiApplicationId_,
				       smiMeshId_,
				       SMI_NEIGH_QUANTITY,
				       NULL, NULL, NULL))
      {
	int defaultValue[3] = {-1, -1, -1};

	SMI_Begin_write_transaction(smiApplicationId_,
				    smiMeshId_);

	SMI_Add_quantity(smiApplicationId_,
			 smiMeshId_,
			 SMI_NEIGH_QUANTITY,
			 SMI_LOC_ELEM,
			 SMI_TYPE_INT,
			 dim+1,
			 defaultValue);

	SMI_Add_quantity(smiApplicationId_,
			 smiMeshId_,
			 SMI_OPP_V_QUANTITY,
			 SMI_LOC_ELEM,
			 SMI_TYPE_INT,
			 dim+1,
			 defaultValue);

	SMI_End_write_transaction(smiApplicationId_,
				  smiMeshId_);
      }

    addNeighbourInfo_ = true;
  }

  void SMIAdapter::getNeighbourInfo(int  elementIndex,
				    int *neighbours,
				    int *oppVertices)
  {
    int i, dim = feSpace_->getMesh()->getDim();

    TEST_EXIT(addNeighbourInfo_)("no neighbour info added\n");
    SMI_Get_quantity_values(smiApplicationId_,
			    smiMeshId_,
			    SMI_NEIGH_QUANTITY,
			    SMI_TYPE_INT,
			    dim+1,
			    1,
			    &elementIndex,
			    neighbours);
    SMI_Get_quantity_values(smiApplicationId_,
			    smiMeshId_,
			    SMI_OPP_V_QUANTITY,
			    SMI_TYPE_INT,
			    dim+1,
			    1,
			    &elementIndex,
			    oppVertices);

    if(oldElementIndex_) {
      for(i = 0; i < dim + 1; i++) {
	neighbours[i] = (*(oldElementIndex_))[neighbours[i]] - 1;
      }
    }
  }

}