-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFEVTKExport.cpp
More file actions
631 lines (520 loc) · 17.7 KB
/
Copy pathFEVTKExport.cpp
File metadata and controls
631 lines (520 loc) · 17.7 KB
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
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
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
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
#include "FEVTKExport.h"
#include <stdio.h>
#include <FECore/FEModel.h>
#include <FECore/FEMesh.h>
#include <FECore/FEAnalysis.h>
#include "FECore/FECore.h"
#include "FECore/FEPlotData.h"
#include "FECore/FEDomain.h"
#include "FECore/log.h"
#include <vtkCell.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkPointData.h>
#include <vtkIntArray.h>
#include <vtkFloatArray.h>
#include <vtkIdList.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkXMLMultiBlockDataWriter.h>
#include <vtkInformation.h>
#include <vtkSmartPointer.h>
FEVTKExport::FEVTKExport(FEModel* fem)
{
m_fem = fem;
//m_prefix = fem->GetTitle(); //
// initialize json based meta file following Paraview format
// https://gitlab.kitware.com/paraview/paraview/blob/v5.5.0/Documentation/release/ParaView-5.5.0.md#json-based-new-meta-file-format-for-series-added
FILE* fp = fopen((m_prefix + ".vtm.series").c_str(), "wt");
fprintf(fp, "{\n");
fprintf(fp, " \"file-series-version\" : \"1.0\",\n");
fprintf(fp, " \"files\" : [\n");
fclose(fp);
}
FEVTKExport::~FEVTKExport(void)
{
// finalize json based meta file
FILE* fp = fopen((m_prefix + ".vtm.series").c_str(), "a");
fprintf(fp, "\n"); // finish last entry
fprintf(fp, " ]\n");
fprintf(fp, "}\n");
fclose(fp);
}
bool FEVTKExport::Save()
{
// get current step and time
int step = m_ndump;
m_ndump++; // increment number of dumped solutions
double time = m_fem->GetTime().currentTime;
// set vtk file name
char szfile[255];
sprintf(szfile, "%s%3.3d.vtm", m_prefix.c_str(), step);
// add this step to the meta file
FILE* fp = fopen((m_prefix + ".vtm.series").c_str(), "a");
if (m_ndump > 1) fprintf(fp, ",\n"); // finish last entry
fprintf(fp, " { \"name\" : \"%s\", \"time\" : %g }", szfile, time);
fclose(fp);
// create MultiBlockDataSets
// root
vtkSmartPointer<vtkMultiBlockDataSet> root =
vtkSmartPointer<vtkMultiBlockDataSet>::New();
// domains
vtkSmartPointer<vtkMultiBlockDataSet> domains =
vtkSmartPointer<vtkMultiBlockDataSet>::New();
root->SetBlock(0, domains);
root->GetMetaData((unsigned int)0)->Set(vtkCompositeDataSet::NAME(), "Domains");
// surfaces
vtkSmartPointer<vtkMultiBlockDataSet> surfaces =
vtkSmartPointer<vtkMultiBlockDataSet>::New();
root->SetBlock(1, surfaces);
root->GetMetaData((unsigned int)1)->Set(vtkCompositeDataSet::NAME(), "Surfaces");
// add domains
if (!AddDomains(domains)) return false;
// add surfaces
if (!AddSurfaces(surfaces)) return false;
// write multiBlockDataSet using XML writer
vtkSmartPointer<vtkXMLMultiBlockDataWriter> writer =
vtkSmartPointer<vtkXMLMultiBlockDataWriter>::New();
writer->SetFileName(szfile);
writer->SetInputData(root);
writer->Write();
// all done!
return true;
}
bool FEVTKExport::AddDomains(vtkSmartPointer<vtkMultiBlockDataSet> multiBlockDataSet)
{
FEMesh& mesh = m_fem->GetMesh();
// loop over all domains
int NDOMS = mesh.Domains();
for (int i = 0; i < NDOMS; ++i)
{
FEDomain& dom = mesh.Domain(i);
// create an unstructured grid for this domain
vtkSmartPointer<vtkUnstructuredGrid> unstructuredGrid =
vtkSmartPointer<vtkUnstructuredGrid>::New();
// convert domain nodes to vtk points and insert into vtk grid
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
for (int n = 0; n < dom.Nodes(); n++) {
vec3d& r = dom.Node(n).m_r0;
points->InsertNextPoint(r.x, r.y, r.z);
}
unstructuredGrid->SetPoints(points);
// insert elements into vtk grid one by one
vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
for (int e = 0; e < dom.Elements(); ++e)
{
FEElement el = dom.ElementRef(e);
// local (domain) connectivity
vtkSmartPointer<vtkIdList> cell = vtkSmartPointer<vtkIdList>::New();
vector<int>& enodes = el.m_lnode;
for (std::vector<int>::iterator it = enodes.begin(); it != enodes.end(); ++it)
cell->InsertNextId(*it);
// insert as correct vtk type
int vtk_type = -1;
switch (el.Type()) {
//FE_HEX8G8,
//FE_HEX8RI,
//FE_HEX8G1,
//FE_HEX20G8,
//FE_HEX20G27,
//FE_HEX27G27,
//FE_TET4G1,
//FE_TET4G4,
//FE_TET5G4,
//FE_TET10G1,
//FE_TET10G4,
//FE_TET10G8,
//FE_TET10GL11,
//FE_TET10G4RI1,
//FE_TET10G8RI4,
//FE_TET15G4,
//FE_TET15G8,
//FE_TET15G11,
//FE_TET15G15,
//FE_TET15G15RI4,
//FE_TET20G15,
//FE_PENTA6G6,
//FE_PENTA15G8,
//FE_PENTA15G21,
//FE_PYRA5G8,
//FE_PYRA13G8,
case FE_HEX8RI: vtk_type = VTK_HEXAHEDRON; break;
case FE_HEX8G1: vtk_type = VTK_HEXAHEDRON; break;
case FE_HEX8G8: vtk_type = VTK_HEXAHEDRON; break;
case FE_TET4G1: vtk_type = VTK_TETRA; break;
case FE_TET4G4: vtk_type = VTK_TETRA; break;
default: feLogEx(m_fem, "Unknown/unsupported volume element type!"); return false; break;
}
unstructuredGrid->InsertNextCell(vtk_type, cell);
} // elements, e
// --- N O D E D A T A ---
// write displacement - not a FEPlotData object>??
{
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName("displacement");
vtkArray->SetNumberOfComponents(3);
vtkArray->SetNumberOfTuples(dom.Nodes());
for (int n = 0; n < dom.Nodes(); n++) {
vec3d dr = dom.Node(n).m_rt - dom.Node(n).m_r0;
vtkArray->SetTuple3(n, dr.x, dr.y, dr.z);
}
unstructuredGrid->GetPointData()->AddArray(vtkArray);
}
for (list<string>::const_iterator var_name = point_data_fields.begin();
var_name != point_data_fields.end(); ++var_name) {
// create the plot variable
FEPlotData* pd = fecore_new<FEPlotData>(var_name->c_str(), m_fem);
// check that it is a valid nodal plot variable
assert(pd->RegionType() == FE_REGION_NODE); // must be evaluated on the entire mesh :(
// verify one value per node
assert(pd->StorageFormat() == FMT_NODE);
// get number of components
int ndata = pd->VarSize(pd->DataType());
// allocate data buffer
FEDataStream val;
val.reserve(ndata * dom.Nodes());
// evaluate on the all nodes.. TODO: fix this
if (pd->Save(mesh, val))
{
// create vtk array
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName(var_name->c_str());
vtkArray->SetNumberOfComponents(ndata);
// transfer from buffer
switch (pd->DataType()) {
case PLT_FLOAT: // scalar
for (int j = 0; j < dom.Nodes(); j++) {
int k = dom.NodeIndex(j); // global index of local node
vtkArray->InsertNextTuple1(val[k]);
}
break;
case PLT_VEC3F: // 3 comp vector
vtkArray->SetComponentName(0, "X");
vtkArray->SetComponentName(1, "Y");
vtkArray->SetComponentName(2, "Z");
for (int j = 0; j < dom.Nodes(); j++) {
int k = 3 * dom.NodeIndex(j); // global index of local node
vtkArray->InsertNextTuple3(
val[k], val[k + 1], val[k + 2]
);
}
break;
case PLT_MAT3FS: // symmetric 3x3 matrix
vtkArray->SetComponentName(0, "XX");
vtkArray->SetComponentName(1, "YY");
vtkArray->SetComponentName(2, "ZZ");
vtkArray->SetComponentName(3, "XY");
vtkArray->SetComponentName(4, "YZ");
vtkArray->SetComponentName(5, "XZ");
for (int j = 0; j < dom.Nodes(); j++) {
int k = 6 * dom.NodeIndex(j); // global index of local node
vtkArray->InsertNextTuple6(
val[k], val[k + 1], val[k + 2],
val[k + 3], val[k + 4], val[k + 5]
);
}
break;
case PLT_MAT3FD: // diagonal 3x3 matrix
vtkArray->SetComponentName(0, "XX");
vtkArray->SetComponentName(1, "YY");
vtkArray->SetComponentName(2, "ZZ");
for (int j = 0; j < dom.Nodes(); j++) {
int k = 3 * dom.NodeIndex(j); // global index of local node
vtkArray->InsertNextTuple3(
val[k], val[k + 1], val[k + 2]
);
}
break;
default:
feLogEx(m_fem, "Unknown/unsupported data type of plot variable!");
return false;
break;
}
// add to grid
unstructuredGrid->GetPointData()->AddArray(vtkArray);
} else {
feLogEx(m_fem, "Unable to write node data: '%s'\n", var_name->c_str());
}
} // var_name iterator
// --- E L E M E N T C E L L D A T A ---
// Write Material ID, then we can identify rigid domains
{
vtkSmartPointer<vtkIntArray> vtkArray =
vtkSmartPointer<vtkIntArray>::New();
vtkArray->SetName("MatID");
vtkArray->SetNumberOfComponents(1);
for (int e = 0; e < dom.Elements(); ++e)
{
FEElement el = dom.ElementRef(e);
vtkArray->InsertNextTuple1(el.GetMatID());
}
unstructuredGrid->GetCellData()->AddArray(vtkArray);
}
// data fields
for (list<string>::const_iterator var_name = cell_data_fields.begin();
var_name != cell_data_fields.end(); ++var_name) {
// create the plot variable
FEPlotData* pd = fecore_new<FEPlotData>(var_name->c_str(), m_fem);
// verify domain plot variable
assert(pd->RegionType() == FE_REGION_DOMAIN);
// verify one value per element
assert(pd->StorageFormat() == FMT_ITEM);
// get number of components
int ndata = pd->VarSize(pd->DataType());
// allocate data buffer
FEDataStream val;
val.reserve(ndata * dom.Elements());
// evaluate on the mesh nodes
// may fail if material does not support plotdata, eg. rigid material does not compute stresses
if (pd->Save(dom, val))
{
// create vtk array
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName(var_name->c_str());
vtkArray->SetNumberOfComponents(ndata);
// transfer from buffer
switch (pd->DataType()) {
case PLT_FLOAT: // scalar
for (int j = 0; j < val.size(); j += 1)
vtkArray->InsertNextTuple1(val[j]);
break;
case PLT_VEC3F: // 3 comp vector
vtkArray->SetComponentName(0, "X");
vtkArray->SetComponentName(1, "Y");
vtkArray->SetComponentName(2, "Z");
for (int j = 0; j < val.size(); j += 3)
vtkArray->InsertNextTuple3(
val[j], val[j + 1], val[j + 2]
);
break;
case PLT_MAT3FS: // symmetric 3x3 matrix
vtkArray->SetComponentName(0, "XX");
vtkArray->SetComponentName(1, "YY");
vtkArray->SetComponentName(2, "ZZ");
vtkArray->SetComponentName(3, "XY");
vtkArray->SetComponentName(4, "YZ");
vtkArray->SetComponentName(5, "XZ");
for (int j = 0; j < val.size(); j += 6)
vtkArray->InsertNextTuple6(
val[j], val[j + 1], val[j + 2],
val[j + 3], val[j + 4], val[j + 5]
);
break;
case PLT_MAT3FD: // diagonal 3x3 matrix
vtkArray->SetComponentName(0, "XX");
vtkArray->SetComponentName(1, "YY");
vtkArray->SetComponentName(2, "ZZ");
for (int j = 0; j < val.size(); j += 3)
vtkArray->InsertNextTuple3(
val[j], val[j + 1], val[j + 2]
);
break;
default:
feLogEx(m_fem, "Unknown/unsupported data type of plot variable!");
return false;
break;
}
// add to grid
unstructuredGrid->GetCellData()->AddArray(vtkArray);
}
else {
feLogEx(m_fem, "Unable to write element data '%s' for domain '%s'\n", var_name->c_str(), dom.GetName().c_str());
}
} // var_name iterator
multiBlockDataSet->SetBlock(i, unstructuredGrid);
multiBlockDataSet->GetMetaData((unsigned int)i)->Set(vtkCompositeDataSet::NAME(), dom.GetName().c_str());
} // domain, i
return true;
}
bool FEVTKExport::AddSurfaces(vtkSmartPointer<vtkMultiBlockDataSet> multiBlockDataSet)
{
// Each surface is stored in its own file.
// Surfaces are duplicated whenever referenced in multiple contact-pairs.
// Merge dublicate surfaces before export.
// get "surfaces"
FEMesh& m = m_fem->GetMesh();
int NS = m.Surfaces();
// first create a list of surface names
// because surfaces are duplicated whenever referenced in a surface?/contact?-pair
std::vector<std::string> names;
for (int i = 0; i < NS; ++i)
names.push_back(m.Surface(i).GetName());
// find unique names
std::vector<std::string> unique_names;
std::sort(names.begin(), names.end()); // need to be sorted before using unique
std::unique_copy(names.begin(), names.end(), std::back_inserter(unique_names));
// loop over unique surface names
for (int i = 0; i < unique_names.size(); ++i)
{
// get the name
const std::string name = unique_names[i];
// get reference surface, count elements and size of data
FESurface* Si = m.FindSurface(name); // what surface will this return???
// write points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
for (int n = 0; n < Si->Nodes(); n++)
{
FENode node = Si->Node(n);
points->InsertNextPoint(node.m_r0.x, node.m_r0.y, node.m_r0.z);
}
// create a polydata object and add points to it
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
// create elements one by one
vtkSmartPointer<vtkCellArray> cells =
vtkSmartPointer<vtkCellArray>::New();
for (int e = 0; e < Si->Elements(); e++)
{
FEElement el = Si->Element(e);
vector<int>& enodes = el.m_lnode;
vtkSmartPointer<vtkIdList> cell = vtkSmartPointer<vtkIdList>::New();
for (std::vector<int>::iterator it = enodes.begin(); it != enodes.end(); ++it)
cell->InsertNextId(*it);
// insert as correct vtk type
int vtk_type = -1;
switch (el.Type()) {
//FE_QUAD4G4,
//FE_QUAD4NI,
//FE_TRI3G1,
//FE_TRI3G3,
//FE_TRI3G7,
//FE_TRI3NI,
//FE_TRI6G3,
//FE_TRI6G4,
//FE_TRI6G7,
//// FE_TRI6MG7,
//FE_TRI6GL7,
//FE_TRI6NI,
//FE_TRI7G3,
//FE_TRI7G4,
//FE_TRI7G7,
//FE_TRI7GL7,
//FE_TRI10G7,
//FE_TRI10G12,
//FE_QUAD8G9,
//FE_QUAD8NI,
//FE_QUAD9G9,
//FE_QUAD9NI,
case FE_QUAD4G4: vtk_type = VTK_QUAD; break;
case FE_QUAD4NI: vtk_type = VTK_QUAD; break;
case FE_TRI3G1: vtk_type = VTK_TRIANGLE; break;
case FE_TRI3G3: vtk_type = VTK_TRIANGLE; break;
case FE_TRI3G7: vtk_type = VTK_TRIANGLE; break;
case FE_TRI3NI: vtk_type = VTK_TRIANGLE; break;
default: feLogEx(m_fem, "Unknown/unsupported surface element type!"); return false; break;
}
// inserting directly into polydata cause seg error (ulike for vtugrid)
cells->InsertNextCell(cell);
}
polydata->SetPolys(cells);
// --- S U R F A C E P O I N T D A T A ---
{
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName("displacement");
vtkArray->SetNumberOfComponents(3);
vtkArray->SetNumberOfTuples(Si->Nodes());
for (int n = 0; n < Si->Nodes(); n++)
{
FENode node = Si->Node(n);
vec3d dr = node.m_rt - node.m_r0;
vtkArray->SetTuple3(n, dr.x, dr.y, dr.z);
}
polydata->GetPointData()->AddArray(vtkArray);
}
// --- S U R F A C E C E L L D A T A ---
{
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName("surface normal");
vtkArray->SetNumberOfComponents(3);
vtkArray->SetNumberOfTuples(Si->Elements());
// use normal in displaced element (natural coord (0.5,0.5) is center)
for (int e = 0; e < Si->Elements(); e++)
{
vec3d n = Si->SurfaceNormal(Si->Element(e), 0.5, 0.5);
vtkArray->SetTuple3(e, n.x, n.y, n.z);
}
polydata->GetCellData()->AddArray(vtkArray);
}
for (list<string>::const_iterator var_name = surface_data_fields.begin();
var_name != surface_data_fields.end(); ++var_name) {
// create the plot variable
FEPlotData* pd = fecore_new<FEPlotData>(var_name->c_str(), m_fem);
// verify region
assert(pd->RegionType() == FE_REGION_SURFACE);
// verify..? cell data not point data?
assert(pd->StorageFormat() == FMT_ITEM);
// get number of components
int ndata = pd->VarSize(pd->DataType());
// total size of data
int nsize = ndata * Si->Elements();
// allocate buffer and initialize to zero
double* merge_val = new double[nsize];
for (int k = 0; k < nsize; ++k) {
merge_val[k] = 0.0;
}
// loop over all surfaces, match name and merge
for (int j = 0; j < NS; ++j)
{
FESurface& Sj = m.Surface(j);
// continue if names does not match
if (name.compare(Sj.GetName()) != 0)
continue;
// check that number of elements matches
assert(Si->Elements() == Sj.Elements());
// get data for this surface
FEDataStream val; val.reserve(nsize);
if (pd->Save(Sj, val))
{
// merge using addition
// TODO: for some variables ie contact gap we should take min value
for (int k = 0; k < nsize; ++k) {
merge_val[k] += val[k];
}
}
else {
feLogEx(m_fem, "Unable to write surface data '%s' for surface '%s'\n", var_name->c_str(), name.c_str());
}
} // Surface(j), j
// create vtk array
vtkSmartPointer<vtkFloatArray> vtkArray =
vtkSmartPointer<vtkFloatArray>::New();
vtkArray->SetName(var_name->c_str());
vtkArray->SetNumberOfComponents(ndata);
// transfer from buffer
switch (pd->DataType()) {
case PLT_FLOAT: // scalar
for (int j = 0; j < nsize; j += 1)
vtkArray->InsertNextTuple1(merge_val[j]);
break;
case PLT_VEC3F: // 3 comp vector
vtkArray->SetComponentName(0, "X");
vtkArray->SetComponentName(1, "Y");
vtkArray->SetComponentName(2, "Z");
for (int j = 0; j < nsize; j += 3)
vtkArray->InsertNextTuple3(
merge_val[j], merge_val[j + 1], merge_val[j + 2]
);
break;
default:
feLogEx(m_fem, "Unknown/unsupported data type of plot variable!");
return false;
break;
}
// add to grid
polydata->GetCellData()->AddArray(vtkArray);
// clean up buffer
delete merge_val;
} // plot variable
multiBlockDataSet->SetBlock(i, polydata);
multiBlockDataSet->GetMetaData((unsigned int)i)->Set(vtkCompositeDataSet::NAME(), name.c_str());
} // unique surface names, i
return true;
}