Skip to content

Bugfix vtk reader data #1431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
14 changes: 10 additions & 4 deletions example/IO/cmesh/vtk/t8_cmesh_read_from_vtk.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ along with t8code; if not, write to the Free Software Foundation, Inc.,
* \param[in] prefix The prefix of the file to read the mesh from
* \param[in] comm The communicator used in this example
* \param[in] values_per_cell The number of values per cell in the mesh.
* \param[in] partition If set, partition the cmesh uniformly.
* \param[in] vtk_file_type The type of the file to read.
* \param[in] user_package_id The package id of the user application
* \param[in] out_prefix The prefix of the output file.
*/
void
t8_forest_construct_from_vtk (const char *prefix, sc_MPI_Comm comm, const int values_per_cell, const int partition,
vtk_file_type_t vtk_file_type, const char *out_prefix)
vtk_file_type_t vtk_file_type, const int user_package_id, const char *out_prefix)
{
/* Read a poly-data file (.ply, .vtp, .obj, .stl, .vtk, .g) and construct a cmesh
* representing the mesh. If there is any cell-data, it will be read too.
* Triangle-strips and polygons will be broken down to multiple triangles. */
t8_cmesh_t cmesh_in = t8_cmesh_vtk_reader (prefix, partition, 0, comm, vtk_file_type);
t8_cmesh_t cmesh_in = t8_cmesh_vtk_reader (prefix, partition, 0, comm, vtk_file_type, user_package_id, 0);
if (cmesh_in == NULL) {
t8_errorf ("Error reading file.\n");
return;
Expand Down Expand Up @@ -123,7 +127,7 @@ main (int argc, char **argv)
const char *out_file;
sc_options_t *opt;
char usage[BUFSIZ], help[BUFSIZ];
int sreturn;
int sreturn, user_application_package_id;
int partition;
int vtk_file_type_int;
vtk_file_type_t vtk_file_type;
Expand Down Expand Up @@ -185,8 +189,10 @@ main (int argc, char **argv)
vtk_file_type = VTK_FILE_ERROR;
break;
}
user_application_package_id = sc_package_register (NULL, SC_LP_DEFAULT, "user_application",
"Dummy user application which accesses cmesh vtk data.");
t8_forest_construct_from_vtk (vtk_file, sc_MPI_COMM_WORLD, num_keys, partition, (vtk_file_type_t) vtk_file_type,
out_file);
user_application_package_id, out_file);
}
sc_options_destroy (opt);
sc_finalize ();
Expand Down
7 changes: 7 additions & 0 deletions src/t8_cmesh/t8_cmesh.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ t8_cmesh_set_attribute (t8_cmesh_t cmesh, const t8_gloidx_t gtree_id, const int
void *const data, const size_t data_size, const int data_persists)
{
T8_ASSERT (t8_cmesh_is_initialized (cmesh));
T8_ASSERT (sc_package_is_registered (package_id));
#if T8_ENABLE_DEBUG
// The key for t8code attributes must be in the range of possible keys.
if (package_id == t8_get_package_id ()) {
T8_ASSERT (key < T8_CMESH_NEXT_POSSIBLE_KEY && key >= 0);
}
#endif
SC_CHECK_ABORT (cmesh->set_from == NULL, "ERROR: Cannot add attributes to cmesh when deriving from another cmesh.\n");

t8_stash_add_attribute (cmesh->stash, gtree_id, package_id, key, data_size, data, !data_persists);
Expand Down
9 changes: 4 additions & 5 deletions src/t8_cmesh/t8_cmesh_examples.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,8 @@ t8_cmesh_new_long_brick_pyramid (sc_MPI_Comm comm, int num_cubes)
}

t8_cmesh_t
t8_cmesh_new_row_of_cubes (t8_locidx_t num_trees, const int set_attributes, const int do_partition, sc_MPI_Comm comm)
t8_cmesh_new_row_of_cubes (t8_locidx_t num_trees, const int set_attributes, const int do_partition, sc_MPI_Comm comm,
const int package_id)
{
T8_ASSERT (num_trees > 0);

Expand Down Expand Up @@ -2793,10 +2794,8 @@ t8_cmesh_new_row_of_cubes (t8_locidx_t num_trees, const int set_attributes, cons
}
/* Set two more dummy attributes - tree_id & num_trees. */
if (set_attributes) {
t8_cmesh_set_attribute (cmesh, tree_id, t8_get_package_id (), T8_CMESH_NEXT_POSSIBLE_KEY, &tree_id,
sizeof (t8_locidx_t), 0);
t8_cmesh_set_attribute (cmesh, tree_id, t8_get_package_id (), T8_CMESH_NEXT_POSSIBLE_KEY + 1, &num_trees,
sizeof (t8_locidx_t), 0);
t8_cmesh_set_attribute (cmesh, tree_id, package_id, 0, &tree_id, sizeof (t8_locidx_t), 0);
t8_cmesh_set_attribute (cmesh, tree_id, package_id, 1, &num_trees, sizeof (t8_locidx_t), 0);
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/t8_cmesh/t8_cmesh_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,16 @@ t8_cmesh_new_long_brick_pyramid (sc_MPI_Comm comm, int num_cubes);

/** Construct \a num_trees many cubes each of length 1 connected along the x-axis
* without any additional attributes than the tree-vertices, or with additional attributes.
* \param [in] num_trees The number of trees along the x-axis
* \param [in] set_attributes If 1, set tree_id and num_trees as additional attribute for each tree.
* \param [in] comm The MPI communicator used to commit the cmesh
* \return A cmesh with \a num_trees many hexahedrons.
* \param [in] num_trees The number of trees along the x-axis
* \param [in] set_attributes If 1, set tree_id and num_trees as additional attribute for each tree.
* \param [in] do_partition Partition the cmesh.
* \param [in] comm The MPI communicator used to commit the cmesh.
* \param [in] package_id The package id to use for the cmesh. Cannot be the t8 or sc package id.
* \return A cmesh with \a num_trees many hexahedrons.
*/
t8_cmesh_t
t8_cmesh_new_row_of_cubes (t8_locidx_t num_trees, const int set_attributes, const int do_partition, sc_MPI_Comm comm);
t8_cmesh_new_row_of_cubes (t8_locidx_t num_trees, const int set_attributes, const int do_partition, sc_MPI_Comm comm,
const int package_id);

/** Construct a quadrangulated disk of given radius.
* \param [in] radius Radius of the sphere.
Expand Down
4 changes: 2 additions & 2 deletions src/t8_cmesh/t8_cmesh_vtk_reader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ T8_EXTERN_C_BEGIN ();

t8_cmesh_t
t8_cmesh_vtk_reader (const char *filename, const int partition, const int main_proc, sc_MPI_Comm comm,
const vtk_file_type_t vtk_file_type)
const vtk_file_type_t vtk_file_type, const int package_id, const int starting_key)
{
return t8_vtk_reader_cmesh (filename, partition, main_proc, comm, vtk_file_type);
return t8_vtk_reader_cmesh (filename, partition, main_proc, comm, vtk_file_type, package_id, starting_key);
}

T8_EXTERN_C_END ();
4 changes: 3 additions & 1 deletion src/t8_cmesh_vtk_reader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ T8_EXTERN_C_BEGIN ();
* \param[in] main_proc The main reading processor
* \param[in] comm An mpi-communicator
* \param[in] vtk_file_type A vtk-filetype that is readable by t8code.
* \param[in] package_id The package id of the application. It is generated with the usage of \ref sc_package_register.
* \param[in] starting_key If the application already registered attributes, the starting key is used so that the existing attributes are not overwritten.
* \return A committed cmesh.
*/
t8_cmesh_t
t8_cmesh_vtk_reader (const char *filename, const int partition, const int main_proc, sc_MPI_Comm comm,
const vtk_file_type_t vtk_file_type);
const vtk_file_type_t vtk_file_type, const int package_id, const int starting_key);

T8_EXTERN_C_END ();

Expand Down
17 changes: 11 additions & 6 deletions src/t8_vtk/t8_vtk_reader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ t8_get_dimension (vtkSmartPointer<vtkDataSet> vtkGrid)

static void
t8_vtk_iterate_cells (vtkSmartPointer<vtkDataSet> vtkGrid, t8_cmesh_t cmesh, const t8_gloidx_t first_tree,
[[maybe_unused]] sc_MPI_Comm comm)
[[maybe_unused]] sc_MPI_Comm comm, const int package_id, const int starting_key)
{
double **tuples = NULL;
size_t *data_size = NULL;
Expand Down Expand Up @@ -284,7 +284,7 @@ t8_vtk_iterate_cells (vtkSmartPointer<vtkDataSet> vtkGrid, t8_cmesh_t cmesh, con
const t8_gloidx_t cell_id = cell_it->GetCellId ();
vtkDataArray *data = cell_data->GetArray (dtype);
data->GetTuple (cell_id, tuples[dtype]);
t8_cmesh_set_attribute (cmesh, tree_id, t8_get_package_id (), dtype + 1, tuples[dtype], data_size[dtype], 0);
t8_cmesh_set_attribute (cmesh, tree_id, package_id, dtype + starting_key, tuples[dtype], data_size[dtype], 0);
}
tree_id++;
}
Expand Down Expand Up @@ -341,8 +341,11 @@ t8_vtk_partition (t8_cmesh_t cmesh, const int mpirank, const int mpisize, t8_glo

t8_cmesh_t
t8_vtkGrid_to_cmesh (vtkSmartPointer<vtkDataSet> vtkGrid, const int partition, const int main_proc,
const int distributed_grid, sc_MPI_Comm comm)
const int distributed_grid, sc_MPI_Comm comm, const int package_id, const int starting_key)
{
T8_ASSERT (package_id != t8_get_package_id ());
T8_ASSERT (package_id != sc_get_package_id ());
T8_ASSERT (sc_package_is_registered (package_id));
t8_cmesh_t cmesh;
int mpisize;
int mpirank;
Expand Down Expand Up @@ -388,7 +391,7 @@ t8_vtkGrid_to_cmesh (vtkSmartPointer<vtkDataSet> vtkGrid, const int partition, c
* - We use a parallel file-type and use a partitioned read, every proc translates its chunk of the grid.
*/
if (!partition || mpirank == main_proc || distributed_grid) {
t8_vtk_iterate_cells (vtkGrid, cmesh, first_tree, comm);
t8_vtk_iterate_cells (vtkGrid, cmesh, first_tree, comm, package_id, starting_key);
}

if (cmesh != NULL) {
Expand Down Expand Up @@ -499,13 +502,15 @@ t8_vtk_reader_pointSet (const char *filename, const int partition, const int mai
t8_cmesh_t
t8_vtk_reader_cmesh ([[maybe_unused]] const char *filename, [[maybe_unused]] const int partition,
[[maybe_unused]] const int main_proc, [[maybe_unused]] sc_MPI_Comm comm,
[[maybe_unused]] const vtk_file_type_t vtk_file_type)
[[maybe_unused]] const vtk_file_type_t vtk_file_type, [[maybe_unused]] const int package_id,
[[maybe_unused]] const int starting_key)
{
#if T8_ENABLE_VTK
vtkSmartPointer<vtkDataSet> vtkGrid = t8_vtk_reader (filename, partition, main_proc, comm, vtk_file_type);
if (vtkGrid != NULL) {
const int distributed_grid = (vtk_file_type & VTK_PARALLEL_FILE) && partition;
t8_cmesh_t cmesh = t8_vtkGrid_to_cmesh (vtkGrid, partition, main_proc, distributed_grid, comm);
t8_cmesh_t cmesh
= t8_vtkGrid_to_cmesh (vtkGrid, partition, main_proc, distributed_grid, comm, package_id, starting_key);
T8_ASSERT (cmesh != NULL);
return cmesh;
}
Expand Down
18 changes: 12 additions & 6 deletions src/t8_vtk/t8_vtk_reader.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ T8_EXTERN_C_BEGIN ();

/**
* Given a pointer to a vtkDataSet a cmesh representing the vtkDataSet is
* constructed and can be shared over the processes.
* constructed and can be shared over the processes. The vtk data arrays will be associated with the trees.
*
* \param[in] vtkGrid A pointer to a vtkDataSet
* \param[in] partition Flag if the cmesh should be partitioned
* \param[in] main_proc The main reading process
* \param[in] distributed_grid Flag if the vtkGrid is distributed over several procs.
* \param[in] comm The communicator.
* \return t8_cmesh_t
* \param[in] package_id The package id of the application. It is generated with the usage of \ref sc_package_register.
* \param[in] starting_key If the application already registered attributes, the starting key is used so that the existing attributes are not overwritten.
* \return The committed cmesh
*/
t8_cmesh_t
t8_vtkGrid_to_cmesh (vtkSmartPointer<vtkDataSet> vtkGrid, const int partition, const int main_proc,
const int distributed_grid, sc_MPI_Comm comm);
const int distributed_grid, sc_MPI_Comm comm, const int package_id, const int starting_key);

/**
* Given a pointer to a vtkDataSet a vtkPointSet storing a set of points of
Expand Down Expand Up @@ -106,7 +108,9 @@ t8_vtk_reader_pointSet (const char *filename, const int partition, const int mai
* Given a filename to a vtkUnstructuredGrid or vtkPolyData read the file and
* construct a cmesh. This is a two stage process. First the file is read and
* stored in a vtkDataSet using \a t8_vtk_reader and \a t8_file_to_vtkGrid.
* In the second stage a cmesh is constructed from the vtkDataSet using \a t8_vtkGrid_to_cmesh.
* In the second stage a cmesh is constructed from the vtkDataSet using \a t8_vtkGrid_to_cmesh.
* The vtk data arrays will be associated with the cmesh trees and saved as tree attributes using the
* provided user application \a package_id. The keys for the tree attributes start at \a starting_key.
*
* Both stages use the vtk-library, therefore the function is only available if
* t8code is linked against VTK.
Expand All @@ -116,12 +120,14 @@ t8_vtk_reader_pointSet (const char *filename, const int partition, const int mai
* \param[in] partition Flag if the constructed mesh should be partitioned
* \param[in] main_proc The main reading processor
* \param[in] comm An mpi-communicator
* \param[in] vtk_file_type A vtk-filetype that is readable by t8code.
* \param[in] vtk_file_type A vtk-filetype that is readable by t8code.
* \param[in] package_id The package id of the application. It is generated with the usage of \ref sc_package_register.
* \param[in] starting_key If the application already registered attributes, the starting key is used so that the existing attributes are not overwritten.
* \return A committed cmesh.
*/
t8_cmesh_t
t8_vtk_reader_cmesh (const char *filename, const int partition, const int main_proc, sc_MPI_Comm comm,
const vtk_file_type_t vtk_file_type);
const vtk_file_type_t vtk_file_type, const int package_id, const int starting_key);

T8_EXTERN_C_END ();

Expand Down
Loading