Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions tutorials/mesh_handle/t8_mesh_step5_element_data.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "t8_mesh_tutorials_common.hxx" /** Adaption function definition used for this tutorial. */
#include <memory>
#include <span>
#include <vector>

/** The data that we want to store for each element.
* In this example we want to store the element's level and volume. */
Expand Down Expand Up @@ -119,27 +120,19 @@ template <t8_mesh_handle::T8MeshType TMeshClass>
static void
output_data_to_vtu (const TMeshClass &mesh, const char *prefix)
{
t8_locidx_t num_elements = mesh.get_num_local_elements ();
/* We need to allocate a new array to store the volumes on their own.
* This array has one entry per local element. */
double *element_volumes = T8_ALLOC (double, num_elements);
/* The number of user-defined data fields to write. */
int num_data = 1;
/* For each user-defined data field we need one t8_vtk_data_field_t variable */
t8_vtk_data_field_t vtk_data;
/* Set the type of this variable. Since we have one value per element, we pick T8_VTK_SCALAR. */
vtk_data.type = T8_VTK_SCALAR;
/* The name of the field as should be written to the file. */
strcpy (vtk_data.description, "Element volume");
vtk_data.data = element_volumes;
/* Copy the element's volumes from our data array to the output array. */
for (t8_locidx_t ielem = 0; ielem < num_elements; ++ielem) {
element_volumes[ielem] = mesh[ielem].get_element_data ().volume;
/* We collect the volumes with one entry per local element. */
std::vector<double> element_volumes;
element_volumes.reserve (mesh.get_num_local_elements ());
/* Copy the element's volumes from the element data. */
for (const auto &elem : mesh) {
element_volumes.push_back (elem.get_element_data ().volume);
}
/* To write user-defined data, we need the extended output function write_mesh_to_vtk_ext.
* Despite writing user data, it also offers more control over which properties to write. */
t8_mesh_handle::write_mesh_to_vtk_ext (mesh, prefix, num_data, &vtk_data);
T8_FREE (element_volumes);
/* To write user-defined data, we use the output function write_mesh_to_vtk_ext_data.
* Each data field consists of its name, as it should be written to the file, and its values.
* Scalar fields are passed in the first list, vector fields in the optional second list.
* Further arguments offer more control over which properties to write, as in write_mesh_to_vtk_ext.
*/
t8_mesh_handle::write_mesh_to_vtk_ext_data (mesh, prefix, { { "Element volume", element_volumes } });
}

/** Entry point of the program. */
Expand Down
22 changes: 8 additions & 14 deletions tutorials/mesh_handle/t8_mesh_stepA_competences.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#include <mesh_handle/mesh_io.hxx> /** Used to export mesh to vtk files. */
#include <mesh_handle/concepts.hxx> /** Include this to use c++ concepts related to the mesh handle. This can be used to constrain the template parameters to only allow mesh handle classes. */
#include <t8_vector/t8_vec.hxx> /** t8code vector dataclass. */
#include <cstring>
#include <vector>

using namespace t8_mesh_handle; /** Using the namespace to avoid the t8_mesh_handle:: prefix everywhere and shorten the code. */

Expand Down Expand Up @@ -217,24 +217,18 @@ main (int argc, char** argv)
demonstrate_custom_competence (*custom_mesh);

/* Collect the squared volume of each local element (computed by our custom competence) for the vtk output. */
double* squared_volumes = T8_ALLOC (double, custom_mesh->get_num_local_elements ());
int ielem = 0;
std::vector<double> squared_volumes;
squared_volumes.reserve (custom_mesh->get_num_local_elements ());
for (const auto& elem : *custom_mesh) {
squared_volumes[ielem++] = elem.get_squared_volume ();
squared_volumes.push_back (elem.get_squared_volume ());
}

/* Wrap the data in a vtk data field, one scalar value per element. */
t8_vtk_data_field_t vtk_data;
vtk_data.type = T8_VTK_SCALAR;
strcpy (vtk_data.description, "Squared volume");
vtk_data.data = squared_volumes;

/* Output the custom competence data to vtu. */
write_mesh_to_vtk_ext (*custom_mesh, prefix_mesh_with_data, 1, &vtk_data);
/* Output the custom competence data to vtu.
* The data field is given by its name and its values, one scalar value per element. */
t8_mesh_handle::write_mesh_to_vtk_ext_data (*custom_mesh, prefix_mesh_with_data,
{ { "Squared volume", squared_volumes } });
t8_global_productionf (" [mesh_stepA] Wrote mesh and squared volume data to %s*.\n", prefix_mesh_with_data);

/* Free the memory. */
T8_FREE (squared_volumes);
} /* End of mesh scope. */
/* Finalizing. */
sc_finalize ();
Expand Down
Loading