Skip to content
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

gwb-grid: support for filtering cells #585

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
98 changes: 96 additions & 2 deletions source/gwb-grid/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#include "world_builder/world.h"
#include "world_builder/config.h"

#include <algorithm>


#include "vtu11/vtu11.hpp"
#undef max
#undef min
Expand Down Expand Up @@ -61,6 +64,71 @@
using namespace WorldBuilder::Utilities;



/**
* Filter the cells of a VTU mesh based on a given tag. All tags with smaller value than @p first_tag will be removed
*/
void filter_vtu_mesh(int dim,
int first_tag,
vtu11::Vtu11UnstructuredMesh &input_mesh,
const std::vector<vtu11::DataSetData> &input_data,
vtu11::Vtu11UnstructuredMesh &output_mesh,
std::vector<vtu11::DataSetData> &output_data);

void filter_vtu_mesh(int dim,

Check warning on line 78 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L78

Added line #L78 was not covered by tests
int first_tag,
vtu11::Vtu11UnstructuredMesh &input_mesh,
const std::vector<vtu11::DataSetData> &input_data,
vtu11::Vtu11UnstructuredMesh &output_mesh,
std::vector<vtu11::DataSetData> &output_data)
{
output_data.resize(input_data.size());
const std::int64_t invalid = static_cast<std::uint64_t>(-1);
std::vector<std::int64_t> vertex_index_map(input_mesh.points().size(), invalid);

Check warning on line 87 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L85-L87

Added lines #L85 - L87 were not covered by tests

const unsigned int n_vert_per_cell = (dim==3)?8:4;

Check warning on line 89 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L89

Added line #L89 was not covered by tests

const std::size_t n_cells = input_mesh.types().size();
std::uint64_t dst_cellid = 0;
for (std::size_t cellidx = 0; cellidx <n_cells; ++cellidx)

Check warning on line 93 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L91-L93

Added lines #L91 - L93 were not covered by tests
{
int highest_tag = -1;
for (unsigned int idx=cellidx*n_vert_per_cell; idx<(cellidx+1)*n_vert_per_cell; ++idx)

Check warning on line 96 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L95-L96

Added lines #L95 - L96 were not covered by tests
{
const std::int64_t src_vid = input_mesh.connectivity()[idx];
highest_tag = std::max(highest_tag,static_cast<int>(input_data[2][src_vid]));

Check warning on line 99 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L98-L99

Added lines #L98 - L99 were not covered by tests
}
if (highest_tag < first_tag)
continue;

Check warning on line 102 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L101-L102

Added lines #L101 - L102 were not covered by tests

++dst_cellid;

Check warning on line 104 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L104

Added line #L104 was not covered by tests

for (unsigned int idx=cellidx*n_vert_per_cell; idx<(cellidx+1)*n_vert_per_cell; ++idx)

Check warning on line 106 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L106

Added line #L106 was not covered by tests
{
const std::int64_t src_vid = input_mesh.connectivity()[idx];

Check warning on line 108 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L108

Added line #L108 was not covered by tests

std::int64_t dst_vid = vertex_index_map[src_vid];
if (dst_vid == invalid)

Check warning on line 111 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L110-L111

Added lines #L110 - L111 were not covered by tests
{
dst_vid = output_mesh.points().size()/3;
vertex_index_map[src_vid] = dst_vid;

Check warning on line 114 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L113-L114

Added lines #L113 - L114 were not covered by tests

for (int i=0; i<3; ++i)
output_mesh.points().push_back(input_mesh.points()[src_vid*3+i]);

Check warning on line 117 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L116-L117

Added lines #L116 - L117 were not covered by tests

for (unsigned int d=0; d<input_data.size(); ++d)
output_data[d].push_back(input_data[d][src_vid]);

Check warning on line 120 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L119-L120

Added lines #L119 - L120 were not covered by tests
}

output_mesh.connectivity().push_back(dst_vid);

Check warning on line 123 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L123

Added line #L123 was not covered by tests

}
output_mesh.offsets().push_back(dst_cellid*n_vert_per_cell);

Check warning on line 126 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L126

Added line #L126 was not covered by tests

output_mesh.types().push_back(input_mesh.types()[cellidx]);

Check warning on line 128 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L128

Added line #L128 was not covered by tests
}
}

Check warning on line 130 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L130

Added line #L130 was not covered by tests

/**
* A very simple threadpool class. The threadpool currently only supports a
* parallel for function, to easily parallelize the for function.
Expand Down Expand Up @@ -225,6 +293,10 @@
std::string wb_file;
std::string data_file;

// If set to true, we will output one visualization file per "tag"
// with only the cells corresponding to that tag included.
bool output_by_tag = false;

size_t dim = 3;
size_t compositions = 0;

Expand Down Expand Up @@ -285,6 +357,11 @@
options_vector.erase(options_vector.begin()+static_cast<std::vector<std::string>::difference_type>(i));
options_vector.erase(options_vector.begin()+static_cast<std::vector<std::string>::difference_type>(i));
}
if (options_vector[i] == "--by-tag")
{
output_by_tag = true;
options_vector.erase(options_vector.begin()+static_cast<std::vector<std::string>::difference_type>(i));

Check warning on line 363 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L362-L363

Added lines #L362 - L363 were not covered by tests
}
}

if (options_vector.size() != 2)
Expand Down Expand Up @@ -1505,9 +1582,26 @@
std::cout << "[6/6] Writing the paraview file \r";
std::cout.flush();

vtu11::Vtu11UnstructuredMesh mesh { points, connectivity, offsets, types };
vtu11::writeVtu( file_without_extension + ".vtu", mesh, dataSetInfo, data_set, vtu_output_format );
{
vtu11::Vtu11UnstructuredMesh mesh { points, connectivity, offsets, types };
vtu11::writeVtu( file_without_extension + ".vtu", mesh, dataSetInfo, data_set, vtu_output_format );

if (output_by_tag)
{
std::vector<double> filtered_points;
std::vector<vtu11::VtkIndexType> filtered_connectivity;
std::vector<vtu11::VtkIndexType> filtered_offsets;
std::vector<vtu11::VtkCellType> filtered_types;

Check warning on line 1594 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L1591-L1594

Added lines #L1591 - L1594 were not covered by tests

vtu11::Vtu11UnstructuredMesh filtered_mesh {filtered_points, filtered_connectivity, filtered_offsets, filtered_types};
std::vector<vtu11::DataSetData> filtered_data_set;

Check warning on line 1597 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L1596-L1597

Added lines #L1596 - L1597 were not covered by tests

int first_tag = 0;
filter_vtu_mesh(dim, first_tag, mesh, data_set, filtered_mesh, filtered_data_set);

Check warning on line 1600 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L1599-L1600

Added lines #L1599 - L1600 were not covered by tests

vtu11::writeVtu( file_without_extension + ".filtered.vtu", filtered_mesh, dataSetInfo, filtered_data_set, vtu_output_format );
}

Check warning on line 1603 in source/gwb-grid/main.cc

View check run for this annotation

Codecov / codecov/patch

source/gwb-grid/main.cc#L1602-L1603

Added lines #L1602 - L1603 were not covered by tests
}
std::cout << " \r";
std::cout.flush();
}
Expand Down
Loading