/[escript]/trunk/finley/src/Mesh_saveVTK.c
ViewVC logotype

Contents of /trunk/finley/src/Mesh_saveVTK.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1312 - (show annotations)
Mon Sep 24 06:18:44 2007 UTC (12 years, 1 month ago) by ksteube
File MIME type: text/plain
File size: 40675 byte(s)
The MPI branch is hereby closed. All future work should be in trunk.

Previously in revision 1295 I merged the latest changes to trunk into trunk-mpi-branch.
In this revision I copied all files from trunk-mpi-branch over the corresponding
trunk files. I did not use 'svn merge', it was a copy.

1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2007 by ACceSS MNRF
7 * Copyright 2007 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 /**************************************************************/
17
18 /* writes data and mesh in a vtk file */
19 /* nodal data needs to be given on FINLEY_NODES or FINLEY_REDUCED_NODES */
20
21 /**************************************************************/
22
23
24 #include "Mesh.h"
25 #include "Assemble.h"
26 #include "vtkCellType.h" /* copied from vtk source directory !!! */
27
28 #define LEN_PRINTED_INT_FORMAT (9+1)
29 #define INT_FORMAT "%d "
30 #define INT_NEWLINE_FORMAT "%d\n"
31 #define FLOAT_SCALAR_FORMAT "%12.6e\n"
32 #define FLOAT_VECTOR_FORMAT "%12.6e %12.6e %12.6e\n"
33 #define FLOAT_TENSOR_FORMAT "%12.6e %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e %12.6e\n"
34 #define LEN_PRINTED_FLOAT_SCALAR_FORMAT (12+1)
35 #define LEN_PRINTED_FLOAT_VECTOR_FORMAT (3*(12+1)+1)
36 #define LEN_PRINTED_FLOAT_TENSOR_FORMAT (9*(12+1)+1)
37 #define NEWLINE "\n"
38 #define LEN_TMP_BUFFER LEN_PRINTED_FLOAT_TENSOR_FORMAT+(MAX_numNodes*LEN_PRINTED_INT_FORMAT+1)+2
39 #define NCOMP_MAX 9
40 #define __STRCAT(dest,chunk,dest_in_use) \
41 { \
42 strcpy(&dest[dest_in_use], chunk); \
43 dest_in_use+=strlen(chunk); \
44 }
45
46 void Finley_Mesh_saveVTK(const char * filename_p,
47 Finley_Mesh *mesh_p,
48 const dim_t num_data,
49 char* *names_p,
50 escriptDataC* *data_pp)
51 {
52 char error_msg[LenErrorMsg_MAX], *txt_buffer=NULL, tmp_buffer[LEN_TMP_BUFFER];
53 double sampleAvg[NCOMP_MAX], *values, rtmp;
54 size_t len_txt_buffer, max_len_names, txt_buffer_in_use;
55 FILE * fileHandle_p = NULL;
56 int mpi_size, i_data, i,j , cellType;
57 dim_t nDim, globalNumPoints, numCells, globalNumCells, numVTKNodesPerElement, myNumPoints, numPointsPerSample, rank, nComp, nCompReqd, shape, NN, numCellFactor, myNumCells, max_name_len;
58 bool_t do_write, *isCellCentered=NULL,write_celldata=FALSE,write_pointdata=FALSE;
59 bool_t set_scalar=FALSE,set_vector=FALSE, set_tensor=FALSE;
60 index_t myFirstNode, myLastNode, *globalNodeIndex, k, *node_index, myFirstCell;
61 #ifdef PASO_MPI
62 int ierr;
63 int amode = MPI_MODE_CREATE | MPI_MODE_WRONLY | MPI_MODE_SEQUENTIAL;
64 MPI_File mpi_fileHandle_p;
65 MPI_Status mpi_status;
66 MPI_Request mpi_req;
67 MPI_Info mpi_info=MPI_INFO_NULL;
68 #endif
69 Paso_MPI_rank my_mpi_rank;
70 int nodetype=FINLEY_NODES;
71 int elementtype=FINLEY_UNKNOWN;
72 char elemTypeStr[32];
73 Finley_NodeMapping *nodeMapping=NULL;
74 Finley_ElementFile* elements=NULL;
75 ElementTypeId TypeId;
76
77
78 /****************************************/
79 /* */
80 /* tags in the vtk file */
81
82 char* tags_header="<?xml version=\"1.0\"?>\n" \
83 "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n" \
84 "<UnstructuredGrid>\n" \
85 "<Piece NumberOfPoints=\"%d\" NumberOfCells=\"%d\">\n" \
86 "<Points>\n" \
87 "<DataArray NumberOfComponents=\"%d\" type=\"Float64\" format=\"ascii\">\n";
88 char *tag_End_DataArray = "</DataArray>\n";
89 char* tag_End_PointData = "</PointData>\n";
90 char* tag_End_CellData = "</CellData>\n";
91 char *footer = "</Piece>\n</UnstructuredGrid>\n</VTKFile>\n";
92 char* tags_End_Points_and_Start_Conn = "</DataArray>\n</Points>\n<Cells>\n<DataArray Name=\"connectivity\" type=\"Int32\" format=\"ascii\">\n" ;
93 char* tags_End_Conn_and_Start_Offset = "</DataArray>\n<DataArray Name=\"offsets\" type=\"Int32\" format=\"ascii\">\n";
94 char* tags_End_Offset_and_Start_Type = "</DataArray>\n<DataArray Name=\"types\" type=\"UInt8\" format=\"ascii\">\n";
95 char* tag_Float_DataArray="<DataArray Name=\"%s\" type=\"Float64\" NumberOfComponents=\"%d\" format=\"ascii\">\n";
96 char *tags_End_Type_And_Cells = "</DataArray>\n</Cells>\n";
97
98 int VTK_QUADRATIC_HEXAHEDRON_INDEX[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 17, 18, 19, 12, 13, 14, 15 };
99 /* if there is no mesh we just return */
100 if (mesh_p==NULL) return;
101
102 my_mpi_rank = mesh_p->Nodes->MPIInfo->rank;
103 mpi_size = mesh_p->Nodes->MPIInfo->size;
104 nDim = mesh_p->Nodes->numDim;
105
106 if (! ( (nDim ==2) || (nDim == 3) ) ) {
107 Finley_setError(IO_ERROR, "saveVTK: spatial dimension 2 or 3 is supported only.");
108 return;
109 }
110 /*************************************************************************************/
111
112 /* open the file and check handle */
113
114 if (mpi_size > 1) {
115 #ifdef PASO_MPI
116 /* Collective Call */
117 #ifdef MPIO_HINTS
118 MPI_Info_create(&mpi_info);
119 /* MPI_Info_set(mpi_info, "striping_unit", "424288"); */
120 /* MPI_Info_set(mpi_info, "striping_factor", "16"); */
121 /* MPI_Info_set(mpi_info, "collective_buffering", "true"); */
122 /* MPI_Info_set(mpi_info, "cb_block_size", "131072"); */
123 /* MPI_Info_set(mpi_info, "cb_buffer_size", "1048567"); */
124 /* MPI_Info_set(mpi_info, "cb_nodes", "8"); */
125 /* MPI_Info_set(mpi_info, "access_style", "write_once, sequential"); */
126
127 /*XFS only */
128 /* MPI_Info_set(mpi_info, "direct_write", "true"); */
129 #endif
130 ierr=MPI_File_open(mesh_p->Nodes->MPIInfo->comm, (char*)filename_p, amode,mpi_info, &mpi_fileHandle_p);
131 if (! ierr) {
132 sprintf(error_msg, "saveVTK: File %s could not be opened for writing.", filename_p);
133 Finley_setError(IO_ERROR,error_msg);
134 } else {
135 MPI_File_set_view(mpi_fileHandle_p,MPI_DISPLACEMENT_CURRENT,MPI_CHAR, MPI_CHAR, "native" , mpi_info);
136 }
137 #endif
138 } else {
139 fileHandle_p = fopen(filename_p, "w");
140 if (fileHandle_p==NULL) {
141 sprintf(error_msg, "saveVTK: File %s could not be opened for writing.", filename_p);
142 Finley_setError(IO_ERROR,error_msg);
143 }
144 }
145 if (! Paso_MPIInfo_noError(mesh_p->Nodes->MPIInfo) ) return;
146 /*************************************************************************************/
147
148 /* find the mesh type to be written */
149
150 isCellCentered=TMPMEMALLOC(num_data,bool_t);
151 max_len_names=0;
152 if (!Finley_checkPtr(isCellCentered)) {
153 for (i_data=0;i_data<num_data;++i_data) {
154 elementtype=FINLEY_UNKNOWN;
155 nodetype=FINLEY_NODES;
156 if (! isEmpty(data_pp[i_data])) {
157 switch(getFunctionSpaceType(data_pp[i_data]) ) {
158 case FINLEY_NODES:
159 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES : FINLEY_NODES;
160 isCellCentered[i_data]=FALSE;
161 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
162 elementtype=FINLEY_ELEMENTS;
163 } else {
164 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
165 }
166 break;
167 case FINLEY_REDUCED_NODES:
168 nodetype = FINLEY_REDUCED_NODES;
169 isCellCentered[i_data]=FALSE;
170 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
171 elementtype=FINLEY_ELEMENTS;
172 } else {
173 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
174 }
175 break;
176 case FINLEY_ELEMENTS:
177 case FINLEY_REDUCED_ELEMENTS:
178 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES :FINLEY_NODES;
179 isCellCentered[i_data]=TRUE;
180 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_ELEMENTS) {
181 elementtype=FINLEY_ELEMENTS;
182 } else {
183 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
184 }
185 break;
186 case FINLEY_FACE_ELEMENTS:
187 case FINLEY_REDUCED_FACE_ELEMENTS:
188 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES :FINLEY_NODES;
189 isCellCentered[i_data]=TRUE;
190 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_FACE_ELEMENTS) {
191 elementtype=FINLEY_FACE_ELEMENTS;
192 } else {
193 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
194 }
195 break;
196 case FINLEY_POINTS:
197 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES : FINLEY_NODES;
198 isCellCentered[i_data]=TRUE;
199 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_POINTS) {
200 elementtype=FINLEY_POINTS;
201 } else {
202 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
203 }
204 break;
205 case FINLEY_CONTACT_ELEMENTS_1:
206 case FINLEY_REDUCED_CONTACT_ELEMENTS_1:
207 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES :FINLEY_NODES;
208 isCellCentered[i_data]=TRUE;
209 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_CONTACT_ELEMENTS_1) {
210 elementtype=FINLEY_CONTACT_ELEMENTS_1;
211 } else {
212 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
213 }
214 break;
215 case FINLEY_CONTACT_ELEMENTS_2:
216 case FINLEY_REDUCED_CONTACT_ELEMENTS_2:
217 nodetype = (nodetype == FINLEY_REDUCED_NODES) ? FINLEY_REDUCED_NODES :FINLEY_NODES;
218 isCellCentered[i_data]=TRUE;
219 if (elementtype==FINLEY_UNKNOWN || elementtype==FINLEY_CONTACT_ELEMENTS_1) {
220 elementtype=FINLEY_CONTACT_ELEMENTS_1;
221 } else {
222 Finley_setError(TYPE_ERROR,"saveVTK: cannot write given data in single file.");
223 }
224 break;
225 default:
226 sprintf(error_msg,"saveVTK: unknown function space type %d",getFunctionSpaceType(data_pp[i_data]));
227 Finley_setError(TYPE_ERROR,error_msg);
228 }
229 if (isCellCentered[i_data]) {
230 write_celldata=TRUE;
231 } else {
232 write_pointdata=TRUE;
233 }
234 max_len_names =MAX(max_len_names,strlen(names_p[i_data]));
235 }
236 }
237 }
238 if (Finley_noError()) {
239
240 /***************************************/
241
242 /* select number of points and the mesh component */
243
244 if (nodetype == FINLEY_REDUCED_NODES) {
245 myFirstNode = Finley_NodeFile_getFirstReducedNode(mesh_p->Nodes);
246 myLastNode = Finley_NodeFile_getLastReducedNode(mesh_p->Nodes);
247 globalNumPoints = Finley_NodeFile_getGlobalNumReducedNodes(mesh_p->Nodes);
248 globalNodeIndex= Finley_NodeFile_borrowGlobalReducedNodesIndex(mesh_p->Nodes);
249 } else {
250 myFirstNode = Finley_NodeFile_getFirstNode(mesh_p->Nodes);
251 myLastNode = Finley_NodeFile_getLastNode(mesh_p->Nodes);
252 globalNumPoints = Finley_NodeFile_getGlobalNumNodes(mesh_p->Nodes);
253 globalNodeIndex= Finley_NodeFile_borrowGlobalNodesIndex(mesh_p->Nodes);
254 }
255 myNumPoints = myLastNode - myFirstNode;
256 if (elementtype==FINLEY_UNKNOWN) elementtype=FINLEY_ELEMENTS;
257 switch(elementtype) {
258 case FINLEY_ELEMENTS:
259 elements=mesh_p->Elements;
260 break;
261 case FINLEY_FACE_ELEMENTS:
262 elements=mesh_p->FaceElements;
263 break;
264 case FINLEY_POINTS:
265 elements=mesh_p->Points;
266 break;
267 case FINLEY_CONTACT_ELEMENTS_1:
268 elements=mesh_p->ContactElements;
269 break;
270 }
271 if (elements==NULL) {
272 Finley_setError(SYSTEM_ERROR,"saveVTK: undefined element file");
273 } else {
274 /* map finley element type to VTK element type */
275 numCells = elements->numElements;
276 globalNumCells = Finley_ElementFile_getGlobalNumElements(elements);
277 myNumCells= Finley_ElementFile_getMyNumElements(elements);
278 myFirstCell= Finley_ElementFile_getFirstElement(elements);
279 NN = elements->numNodes;
280 if (nodetype==FINLEY_REDUCED_NODES || nodetype==FINLEY_REDUCED_NODES) {
281 TypeId = elements->LinearReferenceElement->Type->TypeId;
282 } else {
283 TypeId = elements->ReferenceElement->Type->TypeId;
284 }
285 switch(TypeId) {
286 case Point1:
287 case Line2Face:
288 case Line3Face:
289 case Point1_Contact:
290 case Line2Face_Contact:
291 case Line3Face_Contact:
292 numCellFactor=1;
293 cellType = VTK_VERTEX;
294 numVTKNodesPerElement = 1;
295 strcpy(elemTypeStr, "VTK_VERTEX");
296 break;
297
298 case Line2:
299 case Tri3Face:
300 case Rec4Face:
301 case Line2_Contact:
302 case Tri3_Contact:
303 case Tri3Face_Contact:
304 case Rec4Face_Contact:
305 numCellFactor=1;
306 cellType = VTK_LINE;
307 numVTKNodesPerElement = 2;
308 strcpy(elemTypeStr, "VTK_LINE");
309 break;
310
311 case Tri3:
312 case Tet4Face:
313 case Tet4Face_Contact:
314 numCellFactor=1;
315 cellType = VTK_TRIANGLE;
316 numVTKNodesPerElement = 3;
317 strcpy(elemTypeStr, "VTK_TRIANGLE");
318 break;
319
320 case Rec4:
321 case Hex8Face:
322 case Rec4_Contact:
323 case Hex8Face_Contact:
324 numCellFactor=1;
325 cellType = VTK_QUAD;
326 numVTKNodesPerElement = 4;
327 strcpy(elemTypeStr, "VTK_QUAD");
328 break;
329
330 case Tet4:
331 numCellFactor=1;
332 cellType = VTK_TETRA;
333 numVTKNodesPerElement = 4;
334 strcpy(elemTypeStr, "VTK_TETRA");
335 break;
336
337 case Hex8:
338 numCellFactor=1;
339 cellType = VTK_HEXAHEDRON;
340 numVTKNodesPerElement = 8;
341 strcpy(elemTypeStr, "VTK_HEXAHEDRON");
342 break;
343
344 case Line3:
345 case Tri6Face:
346 case Rec8Face:
347 case Line3_Contact:
348 case Tri6Face_Contact:
349 case Rec8Face_Contact:
350 numCellFactor=1;
351 cellType = VTK_QUADRATIC_EDGE;
352 numVTKNodesPerElement = 3;
353 strcpy(elemTypeStr, "VTK_QUADRATIC_EDGE");
354 break;
355
356 case Tri6:
357 case Tet10Face:
358 case Tri6_Contact:
359 case Tet10Face_Contact:
360 numCellFactor=1;
361 cellType = VTK_QUADRATIC_TRIANGLE;
362 numVTKNodesPerElement = 6;
363 strcpy(elemTypeStr, "VTK_QUADRATIC_TRIANGLE");
364 break;
365
366 case Rec8:
367 case Hex20Face:
368 case Rec8_Contact:
369 case Hex20Face_Contact:
370 numCellFactor=1;
371 cellType = VTK_QUADRATIC_QUAD;
372 numVTKNodesPerElement = 8;
373 strcpy(elemTypeStr, "VTK_QUADRATIC_QUAD");
374 break;
375
376 case Tet10:
377 numCellFactor=1;
378 cellType = VTK_QUADRATIC_TETRA;
379 numVTKNodesPerElement = 10;
380 strcpy(elemTypeStr, "VTK_QUADRATIC_TETRA");
381 break;
382
383 case Hex20:
384 numCellFactor=1;
385 cellType = VTK_QUADRATIC_HEXAHEDRON;
386 numVTKNodesPerElement = 20;
387 strcpy(elemTypeStr, "VTK_QUADRATIC_HEXAHEDRON");
388 break;
389
390 default:
391 sprintf(error_msg, "saveVTK: Element type %s is not supported by VTK",elements->ReferenceElement->Type->Name);
392 Finley_setError(VALUE_ERROR,error_msg);
393 }
394 }
395 }
396 /***************************************/
397
398 /***************************************/
399 /* */
400 /* allocate text buffer */
401 /* */
402 max_name_len=0;
403 for (i_data =0 ;i_data<num_data;++i_data) max_name_len=MAX(max_name_len,strlen(names_p[i_data]));
404 len_txt_buffer= strlen(tags_header) + 3 * LEN_PRINTED_INT_FORMAT + (30+3*max_name_len); /* header */
405 if (mpi_size > 1) len_txt_buffer=MAX(len_txt_buffer, myNumPoints * LEN_TMP_BUFFER);
406 if (mpi_size > 1) len_txt_buffer=MAX(len_txt_buffer, numCellFactor*myNumCells*(LEN_PRINTED_INT_FORMAT*numVTKNodesPerElement+1));
407 len_txt_buffer=MAX(len_txt_buffer,200+3*max_len_names);
408 len_txt_buffer=MAX(len_txt_buffer, strlen(tag_Float_DataArray) + LEN_PRINTED_INT_FORMAT + max_len_names);
409 if (mpi_size > 1) len_txt_buffer=MAX(len_txt_buffer, numCellFactor*myNumCells*LEN_PRINTED_FLOAT_TENSOR_FORMAT);
410 if (mpi_size > 1) len_txt_buffer=MAX(len_txt_buffer, myNumPoints*LEN_PRINTED_FLOAT_TENSOR_FORMAT);
411 txt_buffer=TMPMEMALLOC(len_txt_buffer+1,char);
412 Finley_checkPtr(txt_buffer);
413
414 if (Finley_noError()) {
415
416 /* select number of points and the mesh component */
417
418 sprintf(txt_buffer,tags_header,globalNumPoints,numCellFactor*globalNumCells,3);
419
420 if (mpi_size > 1) {
421 if ( my_mpi_rank == 0) {
422 #ifdef PASO_MPI
423 MPI_File_iwrite_shared(mpi_fileHandle_p,txt_buffer,strlen(txt_buffer),MPI_CHAR,&mpi_req);
424 MPI_Wait(&mpi_req,&mpi_status);
425 #endif
426 }
427 } else {
428 fprintf(fileHandle_p,txt_buffer);
429 }
430
431 /* write the nodes */
432
433 if (mpi_size > 1) {
434
435 txt_buffer[0] = '\0';
436 txt_buffer_in_use=0;
437 if (nDim==2) {
438 for (i = 0; i < mesh_p->Nodes->numNodes; i++) {
439 if ( (myFirstNode <= globalNodeIndex[i]) && (globalNodeIndex[i] < myLastNode) ) {
440 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,
441 mesh_p->Nodes->Coordinates[INDEX2(0, i, nDim)],
442 mesh_p->Nodes->Coordinates[INDEX2(1, i, nDim)],
443 0.);
444 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
445 }
446 }
447 } else {
448 for (i = 0; i < mesh_p->Nodes->numNodes; i++) {
449 if ( (myFirstNode <= globalNodeIndex[i]) && (globalNodeIndex[i] < myLastNode) ) {
450 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,
451 mesh_p->Nodes->Coordinates[INDEX2(0, i, nDim)],
452 mesh_p->Nodes->Coordinates[INDEX2(1, i, nDim)],
453 mesh_p->Nodes->Coordinates[INDEX2(2, i, nDim)]);
454 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
455 }
456 }
457
458 }
459 #ifdef PASO_MPI
460 MPI_File_write_ordered(mpi_fileHandle_p, txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
461 #endif
462 } else {
463 if (nDim==2) {
464 for (i = 0; i < mesh_p->Nodes->numNodes; i++) {
465 if ( (myFirstNode <= globalNodeIndex[i]) && (globalNodeIndex[i] < myLastNode) ) {
466 fprintf(fileHandle_p,FLOAT_VECTOR_FORMAT,
467 mesh_p->Nodes->Coordinates[INDEX2(0, i, nDim)],
468 mesh_p->Nodes->Coordinates[INDEX2(1, i, nDim)],
469 0.);
470 }
471 }
472 } else {
473 for (i = 0; i < mesh_p->Nodes->numNodes; i++) {
474 if ( (myFirstNode <= globalNodeIndex[i]) && (globalNodeIndex[i] < myLastNode) ) {
475 fprintf(fileHandle_p,FLOAT_VECTOR_FORMAT,
476 mesh_p->Nodes->Coordinates[INDEX2(0, i, nDim)],
477 mesh_p->Nodes->Coordinates[INDEX2(1, i, nDim)],
478 mesh_p->Nodes->Coordinates[INDEX2(2, i, nDim)]);
479 }
480 }
481
482 }
483 }
484
485 /* close the Points and open connectivity */
486
487 if (mpi_size > 1) {
488 if ( my_mpi_rank == 0) {
489 #ifdef PASO_MPI
490 MPI_File_iwrite_shared(mpi_fileHandle_p, tags_End_Points_and_Start_Conn, strlen(tags_End_Points_and_Start_Conn), MPI_CHAR, &mpi_req);
491 MPI_Wait(&mpi_req,&mpi_status);
492 #endif
493 }
494 } else {
495 fprintf(fileHandle_p,tags_End_Points_and_Start_Conn);
496 }
497
498 /* write the cells */
499 if (nodetype == FINLEY_REDUCED_NODES) {
500 node_index=elements->ReferenceElement->Type->linearNodes;
501 } else if (VTK_QUADRATIC_HEXAHEDRON==cellType) {
502 node_index=VTK_QUADRATIC_HEXAHEDRON_INDEX;
503 } else if (numVTKNodesPerElement!=NN) {
504 node_index=elements->ReferenceElement->Type->geoNodes;
505 } else {
506 node_index=NULL;
507 }
508
509 if ( mpi_size > 1) {
510 txt_buffer[0] = '\0';
511 txt_buffer_in_use=0;
512 if (node_index == NULL) {
513 for (i = 0; i < numCells; i++) {
514 if (elements->Owner[i] == my_mpi_rank) {
515 for (j = 0; j < numVTKNodesPerElement; j++) {
516 sprintf(tmp_buffer,INT_FORMAT,globalNodeIndex[elements->Nodes[INDEX2(j, i, NN)]]);
517 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use)
518 }
519 __STRCAT(txt_buffer,NEWLINE,txt_buffer_in_use)
520 }
521 }
522 } else {
523 for (i = 0; i < numCells; i++) {
524 if (elements->Owner[i] == my_mpi_rank) {
525 for (j = 0; j < numVTKNodesPerElement; j++) {
526 sprintf(tmp_buffer,INT_FORMAT,globalNodeIndex[elements->Nodes[INDEX2(node_index[j], i, NN)]]);
527 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use)
528 }
529 __STRCAT(txt_buffer,NEWLINE,txt_buffer_in_use)
530 }
531 }
532 }
533 #ifdef PASO_MPI
534 MPI_File_write_ordered(mpi_fileHandle_p,txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
535 #endif
536 } else {
537 if (node_index == NULL) {
538 for (i = 0; i < numCells; i++) {
539 for (j = 0; j < numVTKNodesPerElement; j++) {
540 fprintf(fileHandle_p,INT_FORMAT,globalNodeIndex[elements->Nodes[INDEX2(j, i, NN)]]);
541 }
542 fprintf(fileHandle_p,NEWLINE);
543 }
544 } else {
545 for (i = 0; i < numCells; i++) {
546 for (j = 0; j < numVTKNodesPerElement; j++) {
547 fprintf(fileHandle_p,INT_FORMAT,globalNodeIndex[elements->Nodes[INDEX2(node_index[j], i, NN)]]);
548 }
549 fprintf(fileHandle_p,NEWLINE);
550 }
551 }
552
553 }
554
555 /* finalize the connection and start the offset section */
556 if (mpi_size > 1) {
557 if( my_mpi_rank == 0) {
558 #ifdef PASO_MPI
559 MPI_File_iwrite_shared(mpi_fileHandle_p,tags_End_Conn_and_Start_Offset,strlen(tags_End_Conn_and_Start_Offset),MPI_CHAR,&mpi_req);
560 MPI_Wait(&mpi_req,&mpi_status);
561 #endif
562 }
563 } else {
564 fprintf(fileHandle_p,tags_End_Conn_and_Start_Offset);
565 }
566
567 /* write the offsets */
568
569 if ( mpi_size > 1) {
570 txt_buffer[0] = '\0';
571 txt_buffer_in_use=0;
572 for (i=numVTKNodesPerElement*(myFirstCell*numCellFactor+1); i<=numCells*numVTKNodesPerElement*numCellFactor; i+=numVTKNodesPerElement) {
573 sprintf(tmp_buffer, INT_NEWLINE_FORMAT, i);
574 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
575 }
576 #ifdef PASO_MPI
577 MPI_File_write_ordered(mpi_fileHandle_p,txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
578 #endif
579 } else {
580 for (i=numVTKNodesPerElement; i<=numCells*numVTKNodesPerElement*numCellFactor; i+=numVTKNodesPerElement) {
581 fprintf(fileHandle_p, INT_NEWLINE_FORMAT, i);
582 }
583
584 }
585 /* finalize the offset section and start the type section */
586 if ( mpi_size > 1) {
587 if ( my_mpi_rank == 0) {
588 #ifdef PASO_MPI
589 MPI_File_iwrite_shared(mpi_fileHandle_p,tags_End_Offset_and_Start_Type,strlen(tags_End_Offset_and_Start_Type),MPI_CHAR,&mpi_req);
590 MPI_Wait(&mpi_req,&mpi_status);
591 #endif
592 }
593 } else {
594 fprintf(fileHandle_p,tags_End_Offset_and_Start_Type);
595 }
596
597
598 /* write element type */
599 sprintf(tmp_buffer, INT_NEWLINE_FORMAT, cellType);
600 if ( mpi_size > 1) {
601 txt_buffer[0] = '\0';
602 txt_buffer_in_use=0;
603 for (i=0; i<numCells*numCellFactor; i++) __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
604 #ifdef PASO_MPI
605 MPI_File_write_ordered(mpi_fileHandle_p,txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
606 #endif
607 } else {
608 for (i=0; i<numCells*numCellFactor; i++) fprintf(fileHandle_p, tmp_buffer);
609 }
610 /* finalize cell information */
611 if ( mpi_size > 1) {
612 if ( my_mpi_rank == 0) {
613 #ifdef PASO_MPI
614 MPI_File_iwrite_shared(mpi_fileHandle_p,tags_End_Type_And_Cells,strlen(tags_End_Type_And_Cells),MPI_CHAR,&mpi_req);
615 MPI_Wait(&mpi_req,&mpi_status);
616 #endif
617 }
618 } else {
619 fprintf(fileHandle_p,tags_End_Type_And_Cells);
620 }
621 }
622
623 /* Write cell data */
624 if (write_celldata && Finley_noError()) {
625 /* mark the active data arrays */
626 txt_buffer[0] = '\0';
627 set_scalar=FALSE,set_vector=FALSE, set_tensor=FALSE;
628 strcat(txt_buffer, "<CellData");
629 for (i_data =0 ;i_data<num_data;++i_data) {
630 if (! isEmpty(data_pp[i_data]) && isCellCentered[i_data]) {
631 /* if the rank == 0: --> scalar data */
632 /* if the rank == 1: --> vector data */
633 /* if the rank == 2: --> tensor data */
634
635 switch(getDataPointRank(data_pp[i_data])) {
636 case 0:
637 if (! set_scalar) {
638 strcat(txt_buffer," Scalars=\"");
639 strcat(txt_buffer,names_p[i_data]);
640 strcat(txt_buffer,"\"");
641 set_scalar=TRUE;
642 }
643 break;
644 case 1:
645 if (! set_vector) {
646 strcat(txt_buffer," Vectors=\"");
647 strcat(txt_buffer,names_p[i_data]);
648 strcat(txt_buffer,"\"");
649 set_vector=TRUE;
650 }
651 break;
652 case 2:
653 if (! set_tensor) {
654 strcat(txt_buffer," Tensors=\"");
655 strcat(txt_buffer,names_p[i_data]);
656 strcat(txt_buffer,"\"");
657 set_tensor=TRUE;
658 }
659 break;
660 default:
661 sprintf(error_msg, "saveVTK: data %s: Vtk can't handle objects with rank greater than 2.",names_p[i_data]);
662 Finley_setError(VALUE_ERROR,error_msg);
663 return;
664 }
665 }
666 }
667 strcat(txt_buffer, ">\n");
668 if ( mpi_size > 1) {
669 if ( my_mpi_rank == 0) {
670 #ifdef PASO_MPI
671 MPI_File_iwrite_shared(mpi_fileHandle_p,txt_buffer,strlen(txt_buffer),MPI_CHAR,&mpi_req);
672 MPI_Wait(&mpi_req,&mpi_status);
673 #endif
674 }
675 } else {
676 fprintf(fileHandle_p,txt_buffer);
677 }
678 /* write the arrays */
679 for (i_data =0 ;i_data<num_data;++i_data) {
680 if (! isEmpty(data_pp[i_data]) && isCellCentered[i_data]) {
681 txt_buffer[0] = '\0';
682 txt_buffer_in_use=0;
683 numPointsPerSample=getNumDataPointsPerSample(data_pp[i_data]);
684 rank = getDataPointRank(data_pp[i_data]);
685 nComp = getDataPointSize(data_pp[i_data]);
686 nCompReqd=1; /* the number of components mpi_required by vtk */
687 shape=0;
688 if (rank == 0) {
689 nCompReqd = 1;
690 } else if (rank == 1) {
691 shape=getDataPointShape(data_pp[i_data], 0);
692 if (shape>3) {
693 Finley_setError(VALUE_ERROR, "saveVTK: rank 1 object must have less then 4 components");
694 }
695 nCompReqd = 3;
696 } else {
697 shape=getDataPointShape(data_pp[i_data], 0);
698 if (shape>3 || shape != getDataPointShape(data_pp[i_data], 1)) {
699 Finley_setError(VALUE_ERROR, "saveVTK: rank 2 object must have less then 4x4 components and must have a square shape");
700 }
701 nCompReqd = 9;
702 }
703 if (Finley_noError()) {
704 sprintf(txt_buffer,tag_Float_DataArray,names_p[i_data], nCompReqd);
705 if ( mpi_size > 1) {
706 if ( my_mpi_rank == 0) {
707 #ifdef PASO_MPI
708 MPI_File_iwrite_shared(mpi_fileHandle_p,txt_buffer,strlen(txt_buffer),MPI_CHAR,&mpi_req);
709 MPI_Wait(&mpi_req,&mpi_status);
710 #endif
711 }
712 } else {
713 fprintf(fileHandle_p,txt_buffer);
714 }
715 for (i=0; i<numCells; i++) {
716 if (elements->Owner[i] == my_mpi_rank) {
717 values = getSampleData(data_pp[i_data], i);
718 /* averaging over the number of points in the sample */
719 for (k=0; k<MIN(nComp,NCOMP_MAX); k++) {
720 if (isExpanded(data_pp[i_data])) {
721 rtmp = 0.;
722 for (j=0; j<numPointsPerSample; j++) rtmp += values[INDEX2(k,j,nComp)];
723 sampleAvg[k] = rtmp/numPointsPerSample;
724 } else {
725 sampleAvg[k] = values[k];
726 }
727 }
728 /* if the number of mpi_required components is more than the number
729 * of actual components, pad with zeros
730 */
731 /* probably only need to get shape of first element */
732 /* write the data different ways for scalar, vector and tensor */
733 if (nCompReqd == 1) {
734 sprintf(tmp_buffer,FLOAT_SCALAR_FORMAT,sampleAvg[0]);
735 } else if (nCompReqd == 3) {
736 if (shape==1) {
737 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,sampleAvg[0],0.,0.);
738 } else if (shape==2) {
739 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,sampleAvg[0],sampleAvg[1],0.);
740 } else if (shape==3) {
741 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,sampleAvg[0],sampleAvg[1],sampleAvg[2]);
742 }
743 } else if (nCompReqd == 9) {
744 if (shape==1) {
745 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,sampleAvg[0],0.,0.,
746 0.,0.,0.,
747 0.,0.,0.);
748 } else if (shape==2) {
749 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,sampleAvg[0],sampleAvg[1],0.,
750 sampleAvg[2],sampleAvg[3],0.,
751 0.,0.,0.);
752 } else if (shape==3) {
753 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,sampleAvg[0],sampleAvg[1],sampleAvg[2],
754 sampleAvg[3],sampleAvg[4],sampleAvg[5],
755 sampleAvg[6],sampleAvg[7],sampleAvg[8]);
756 }
757 }
758 if ( mpi_size > 1) {
759 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
760 } else {
761 fprintf(fileHandle_p,tmp_buffer);
762 }
763 }
764 }
765 if ( mpi_size > 1) {
766 #ifdef PASO_MPI
767 MPI_File_write_ordered(mpi_fileHandle_p,txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
768 #endif
769 if ( my_mpi_rank == 0) {
770 #ifdef PASO_MPI
771 MPI_File_iwrite_shared(mpi_fileHandle_p,tag_End_DataArray,strlen(tag_End_DataArray),MPI_CHAR,&mpi_req);
772 MPI_Wait(&mpi_req,&mpi_status);
773 #endif
774 }
775 } else {
776 fprintf(fileHandle_p,tag_End_DataArray);
777 }
778 }
779 }
780 }
781 if ( mpi_size > 1) {
782 if ( my_mpi_rank == 0) {
783 #ifdef PASO_MPI
784 MPI_File_iwrite_shared(mpi_fileHandle_p,tag_End_CellData,strlen(tag_End_CellData),MPI_CHAR,&mpi_req);
785 MPI_Wait(&mpi_req,&mpi_status);
786 #endif
787 }
788 } else {
789 fprintf(fileHandle_p,tag_End_CellData);
790 }
791 }
792 /* point data */
793 if (write_pointdata && Finley_noError()) {
794 /* mark the active data arrays */
795 set_scalar=FALSE,set_vector=FALSE, set_tensor=FALSE;
796 txt_buffer[0] = '\0';
797 strcat(txt_buffer, "<PointData");
798 for (i_data =0 ;i_data<num_data;++i_data) {
799 if (! isEmpty(data_pp[i_data]) && !isCellCentered[i_data]) {
800 /* if the rank == 0: --> scalar data */
801 /* if the rank == 1: --> vector data */
802 /* if the rank == 2: --> tensor data */
803
804 switch(getDataPointRank(data_pp[i_data])) {
805 case 0:
806 if (! set_scalar) {
807 strcat(txt_buffer," Scalars=\"");
808 strcat(txt_buffer,names_p[i_data]);
809 strcat(txt_buffer,"\"");
810 set_scalar=TRUE;
811 }
812 break;
813 case 1:
814 if (! set_vector) {
815 strcat(txt_buffer," Vectors=\"");
816 strcat(txt_buffer,names_p[i_data]);
817 strcat(txt_buffer,"\"");
818 set_vector=TRUE;
819 }
820 break;
821 case 2:
822 if (! set_tensor) {
823 strcat(txt_buffer," Tensors=\"");
824 strcat(txt_buffer,names_p[i_data]);
825 strcat(txt_buffer,"\"");
826 set_tensor=TRUE;
827 }
828 break;
829 default:
830 sprintf(error_msg, "saveVTK: data %s: Vtk can't handle objects with rank greater than 2.",names_p[i_data]);
831 Finley_setError(VALUE_ERROR,error_msg);
832 return;
833 }
834 }
835 }
836 strcat(txt_buffer, ">\n");
837 if ( mpi_size > 1) {
838 if ( my_mpi_rank == 0) {
839 #ifdef PASO_MPI
840 MPI_File_iwrite_shared(mpi_fileHandle_p,txt_buffer,strlen(txt_buffer),MPI_CHAR,&mpi_req);
841 MPI_Wait(&mpi_req,&mpi_status);
842 #endif
843 }
844 } else {
845 fprintf(fileHandle_p,txt_buffer);
846 }
847 /* write the arrays */
848 for (i_data =0 ;i_data<num_data;++i_data) {
849 if (! isEmpty(data_pp[i_data]) && !isCellCentered[i_data]) {
850 txt_buffer[0] = '\0';
851 txt_buffer_in_use=0;
852 numPointsPerSample=getNumDataPointsPerSample(data_pp[i_data]);
853 rank = getDataPointRank(data_pp[i_data]);
854 nComp = getDataPointSize(data_pp[i_data]);
855 if (getFunctionSpaceType(data_pp[i_data]) == FINLEY_REDUCED_NODES) {
856 nodeMapping=mesh_p->Nodes->reducedNodesMapping;
857 } else {
858 nodeMapping=mesh_p->Nodes->nodesMapping;
859 }
860 nCompReqd=1; /* the number of components mpi_required by vtk */
861 shape=0;
862 if (rank == 0) {
863 nCompReqd = 1;
864 } else if (rank == 1) {
865 shape=getDataPointShape(data_pp[i_data], 0);
866 if (shape>3) {
867 Finley_setError(VALUE_ERROR, "saveVTK: rank 1 object must have less then 4 components");
868 }
869 nCompReqd = 3;
870 } else {
871 shape=getDataPointShape(data_pp[i_data], 0);
872 if (shape>3 || shape != getDataPointShape(data_pp[i_data], 1)) {
873 Finley_setError(VALUE_ERROR, "saveVTK: rank 2 object must have less then 4x4 components and must have a square shape");
874 }
875 nCompReqd = 9;
876 }
877 if (Finley_noError()) {
878 sprintf(txt_buffer,tag_Float_DataArray,names_p[i_data], nCompReqd);
879 if ( mpi_size > 1) {
880 if ( my_mpi_rank == 0) {
881 #ifdef PASO_MPI
882 MPI_File_iwrite_shared(mpi_fileHandle_p,txt_buffer,strlen(txt_buffer),MPI_CHAR,&mpi_req);
883 MPI_Wait(&mpi_req,&mpi_status);
884 #endif
885 }
886 } else {
887 fprintf(fileHandle_p,txt_buffer);
888 }
889 for (i=0; i<mesh_p->Nodes->numNodes; i++) {
890 k=globalNodeIndex[i];
891 if ( (myFirstNode <= k) && (k < myLastNode) ) {
892 values = getSampleData(data_pp[i_data], nodeMapping->target[i]);
893 /* if the number of mpi_required components is more than the number
894 * of actual components, pad with zeros
895 */
896 /* probably only need to get shape of first element */
897 /* write the data different ways for scalar, vector and tensor */
898 if (nCompReqd == 1) {
899 sprintf(tmp_buffer,FLOAT_SCALAR_FORMAT,values[0]);
900 } else if (nCompReqd == 3) {
901 if (shape==1) {
902 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,values[0],0.,0.);
903 } else if (shape==2) {
904 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,values[0],values[1],0.);
905 } else if (shape==3) {
906 sprintf(tmp_buffer,FLOAT_VECTOR_FORMAT,values[0],values[1],values[2]);
907 }
908 } else if (nCompReqd == 9) {
909 if (shape==1) {
910 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,values[0],0.,0.,
911 0.,0.,0.,
912 0.,0.,0.);
913 } else if (shape==2) {
914 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,values[0],values[1],0.,
915 values[2],values[3],0.,
916 0.,0.,0.);
917 } else if (shape==3) {
918 sprintf(tmp_buffer,FLOAT_TENSOR_FORMAT,values[0],values[1],values[2],
919 values[3],values[4],values[5],
920 values[6],values[7],values[8]);
921 }
922 }
923 if ( mpi_size > 1) {
924 __STRCAT(txt_buffer,tmp_buffer,txt_buffer_in_use);
925 } else {
926 fprintf(fileHandle_p,tmp_buffer);
927 }
928 }
929 }
930 if ( mpi_size > 1) {
931 #ifdef PASO_MPI
932 MPI_File_write_ordered(mpi_fileHandle_p,txt_buffer,txt_buffer_in_use, MPI_CHAR, &mpi_status);
933 #endif
934 if ( my_mpi_rank == 0) {
935 #ifdef PASO_MPI
936 MPI_File_iwrite_shared(mpi_fileHandle_p,tag_End_DataArray,strlen(tag_End_DataArray),MPI_CHAR,&mpi_req);
937 MPI_Wait(&mpi_req,&mpi_status);
938 #endif
939 }
940 } else {
941 fprintf(fileHandle_p,tag_End_DataArray);
942 }
943 }
944 }
945 }
946 if ( mpi_size > 1) {
947 if ( my_mpi_rank == 0) {
948 #ifdef PASO_MPI
949 MPI_File_iwrite_shared(mpi_fileHandle_p,tag_End_CellData,strlen(tag_End_PointData),MPI_CHAR,&mpi_req);
950 MPI_Wait(&mpi_req,&mpi_status);
951 #endif
952 }
953 } else {
954 fprintf(fileHandle_p,tag_End_PointData);
955 }
956 }
957 if (Finley_noError()) {
958 if ( mpi_size > 1) {
959 if ( my_mpi_rank == 0) {
960 #ifdef PASO_MPI
961 MPI_File_iwrite_shared(mpi_fileHandle_p,footer,strlen(footer),MPI_CHAR,&mpi_req);
962 MPI_Wait(&mpi_req,&mpi_status);
963 #ifdef MPIO_HINTS
964 MPI_Info_free(&mpi_info);
965 #undef MPIO_HINTS
966 #endif
967 MPI_File_close(&mpi_fileHandle_p);
968 #endif
969 }
970 } else {
971 fprintf(fileHandle_p,footer);
972 fclose(fileHandle_p);
973 }
974 }
975 TMPMEMFREE(isCellCentered);
976 TMPMEMFREE(txt_buffer);
977 return;
978 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26