sandbox/acastillo/output_fields/vtu/output_vtu.h
#ifndef OUTPUT_VTU_H
#define OUTPUT_VTU_HXML File Formats
preamble: define some useful macros
To make the routines (a little bit) easier to follow, we’ll define the following macros to deal with the reconstruction of the VOF interface
#include "output_vtu_helpers.h"output_vtu(): Exports full 2D (or 3D) fields.
This function writes one VTK XML file which can be read using Paraview. The file stores scalar and vector fields defined at the center points are stored at the cell center of an unstructured grid with the following structure:
|
|
|
|
|
In MPI environments, each task writes its own .vtu file,
linked together by a .pvtu file. Results are recorded in
binary format.
The arguments and their default values are:
- list
- pointer to the list of scalar fields to be exported.
- vlist
- pointer to the list of vector fields to be exported.
- subname
- subname to be used for the output file.
Example Usage
scalar * slist = {a,b};
vector * vlist = {c,d};
char *subname = "domain";
output_vtu(slist, vlist, subname);// Function prototypes
void output_vtu_pid(scalar *list, vector *vlist, char *subname);
#ifdef _MPI
void output_pvtu(scalar *list, vector *vlist, char *subname);
#endif
trace
void output_vtu(scalar *list, vector *vlist, char *subname){
// Check if MPI is defined
@if _MPI
// If MPI is defined, call output_pvtu to handle parallel VTU output
output_pvtu(list, vlist, subname);
@else
// If MPI is not defined, call output_vtu_pid to handle serial VTU output
output_vtu_pid(list, vlist, subname);
@endif
}output_vtu_pid():
writes one .vtu file for the current process
void output_vtu_pid(scalar *list, vector *vlist, char *subname) {
#if defined(_OPENMP)
int num_omp = omp_get_max_threads(); // Get the number of OpenMP threads
omp_set_num_threads(1); // Set the number of OpenMP threads to 1
#endif
char name[111]; // Buffer for file name construction
sprintf(name, "%s.vtu", subname); // Construct the VTU filename
FILE *fp = fopen(name, "w"); // Open the VTU file for writing
if (!fp){
fprintf(stderr, "Error opening file %s for writing.\n", name);
}Define a scalar field for periodic conditions
scalar per_mask[];
foreach () {
per_mask[] = 1.;
}Obtain the number of points, cells, and marker used for connectivity
vertex scalar marker[];
long no_points = 0, no_cells = 0;
foreach_vertex(serial, noauto){
marker[] = no_points;
no_points++; // Increment the number of points
}
foreach (serial, noauto){
if (per_mask[]){
no_cells++; // Increment the number of cells
}
}VTK cell types: VTK_QUAD (in 2D) or VTK_HEXAHEDRON (in 3D)
#if dimension == 2
char type = 9, noffset = 4;
#elif dimension == 3
char type = 12, noffset = 8;
#endifWrite the light data of the VTU file with data blocks specified by offset
long count = 0;
write_vtu_header(fp, no_points, no_cells); // Write the VTU file header
write_scalar_light_data(fp, list, vlist, &count, no_cells); // Write scalar data arrays
write_points_light_data(fp, &count, no_points); // Write points data array
write_cells_light_data(fp, &count, no_cells, no_cells * noffset); // Write cells data arrays
write_vtu_appended(fp); // Write the VTU appended data sectionWrite the heavy data blocks
write_scalar_heavy_data(fp, list, per_mask, no_cells); // Write scalar field data
write_vector_heavy_data(fp, vlist, per_mask, no_cells); // Write vector field data
write_points_heavy_data(fp, no_points); // Write points data
write_cell_offsets(fp, no_cells, noffset); // Write cell offsets
write_cell_types(fp, no_cells, type); // Write cell types
write_cell_connectivity(fp, marker, per_mask, no_cells, noffset); // Write cell connectivityand close the file
fputs("\t\n", fp);
fputs("\t</AppendedData>\n", fp);
fputs("</VTKFile>\n", fp);
fflush(fp); // Flush the file buffer
fclose(fp); // Close the VTU file
#if defined(_OPENMP)
omp_set_num_threads(num_omp); // Restore the original number of OpenMP threads
#endif
}output_pvtu():
if MPI, writes one .pvtu and .vtu for each
process
@ if _MPI
void output_pvtu(scalar *list, vector *vlist, char *subname)
{
char name[112]; // Buffer for file name construction
FILE *fp; // File pointer for file operations
if (pid() == 0){
sprintf(name, "%s.pvtu", subname); // Construct the PVTU filename
fp = fopen(name, "w"); // Open the PVTU file for writing
if (!fp){
fprintf(stderr, "Error opening file %s for writing.\n", name);
MPI_Abort(MPI_COMM_WORLD, 1);
}
// Write sections of the PVTU file
write_pvtu_header(fp); // Write the header
write_scalar_light_pdata(fp, list, vlist); // Write scalar data arrays
write_points_light_pdata(fp); // Write points data array
write_pieces_light_pdata(fp, subname); // Write piece references
// Close the PVTU file
fflush(fp); // Flush the file buffer
fclose(fp); // Close the file
}
MPI_Barrier(MPI_COMM_WORLD);
sprintf(name, "%s_n%3.3d", subname, pid());
output_vtu_pid(list, vlist, name);
}
@endif
/* Include other VTU output modules */
#if dimension > 2
#include "output_vtu_slice.h"
#endif
#include "output_vtu_facets.h"
#include "output_vtu_box.h"postamble: delete macros
#undef shortcut_slice
#undef shortcut_facets
#undef mfacets
#endif
