diff --git a/bindings/python/src/model/representation/core/external_time_series.cpp b/bindings/python/src/model/representation/core/external_time_series.cpp index ffb9df7fc..a8d261da7 100644 --- a/bindings/python/src/model/representation/core/external_time_series.cpp +++ b/bindings/python/src/model/representation/core/external_time_series.cpp @@ -59,11 +59,21 @@ namespace geode "Write the transferable attributes on the Block meshes of the " "BRep: each unique vertex on the closest BRep unique vertex " "(on all its Block vertices), each cell barycenter on the " - "Block polyhedron with the closest barycenter. Raise an error " - "if a point is farther than the tolerance (GLOBAL_EPSILON " - "relative to the largest coordinate when larger than 1) from " - "its closest Block element, if several points match the same " - "Block element, or if a point is within the tolerance of " - "several Block elements" ); + "Block polyhedron with the closest barycenter. The " + "simulation may cover only some Blocks: a Block is " + "represented in the cell barycenters when one of its " + "polyhedra is matched, in the unique vertices when one of its " + "matched vertices belongs to no other Block. Every element of " + "a represented Block must be matched with a point, the other " + "Blocks are left untouched. Several points matched with the " + "same element are accepted with an error log (the closest one " + "is kept). A point set without transferable attribute is not " + "matched. The tolerance bounds the single precision rounding " + "of the largest absolute coordinates of the points (as " + "simulators often store them). Raise an error, leaving the " + "model unchanged, if a point is farther than the tolerance " + "from its closest Block element, if a point is within the " + "tolerance of several Block elements, or if an element of a " + "represented Block is matched with no point" ); } } // namespace geode diff --git a/bindings/python/tests/model/test-py-external-time-series.py b/bindings/python/tests/model/test-py-external-time-series.py index 730f65213..304d015b9 100644 --- a/bindings/python/tests/model/test-py-external-time-series.py +++ b/bindings/python/tests/model/test-py-external-time-series.py @@ -38,6 +38,7 @@ def pressure(unique_vertex, time): return unique_vertex + time +# Polyhedra are numbered over all the Blocks, in brep.blocks() order def velocity(polyhedron): return [float(polyhedron), 1.0, 2.0] @@ -70,10 +71,9 @@ def create_unique_vertices(brep): return vertices -def create_cell_barycenters(brep, block_id, translation): +def create_cell_barycenters(brep, translation): barycenters = mesh.PointSet3D.create() builder = mesh.PointSetBuilder3D.create(barycenters) - block_mesh = brep.block(block_id).mesh() manager = barycenters.vertex_attribute_manager() values = basic.AttributeValuesArrayDouble3() values.default_value = [0.0, 0.0, 0.0] @@ -82,20 +82,20 @@ def create_cell_barycenters(brep, block_id, translation): VELOCITY, TIMES[1], values, basic.AttributeProperties() ) attribute = manager.find_attribute_variable_arraydouble3(attribute_id) - for polyhedron in range(block_mesh.nb_polyhedra()): - point = builder.create_point( - block_mesh.polyhedron_barycenter(polyhedron) + translation - ) - attribute.set_value(point, velocity(polyhedron)) + for block in brep.blocks(): + block_mesh = block.mesh() + for polyhedron in range(block_mesh.nb_polyhedra()): + point = builder.create_point( + block_mesh.polyhedron_barycenter(polyhedron) + translation + ) + attribute.set_value(point, velocity(point)) return barycenters -def create_time_series(brep, block_id, translation=geometry.Point3D()): +def create_time_series(brep, translation=geometry.Point3D()): time_series = model.ExternalTimeSeries() time_series.set_unique_vertices(create_unique_vertices(brep)) - time_series.set_cell_barycenters( - create_cell_barycenters(brep, block_id, translation) - ) + time_series.set_cell_barycenters(create_cell_barycenters(brep, translation)) return time_series @@ -118,16 +118,61 @@ def check_pressures(brep): raise ValueError("[Test] Wrong pressure on vertex") -def check_velocities(brep, block_id): - block_mesh = brep.block(block_id).mesh() +def check_velocities(brep): + model_polyhedron = 0 + for block in brep.blocks(): + block_mesh = block.mesh() + series = basic.AttributeTimeSeriesArrayDouble3( + block_mesh.polyhedron_attribute_manager(), VELOCITY + ) + if series.nb_time_steps() != 1 or series.time(0) != TIMES[1]: + raise ValueError("[Test] Wrong velocity time steps") + for polyhedron in range(block_mesh.nb_polyhedra()): + if series.value(0, polyhedron) != velocity(model_polyhedron): + raise ValueError("[Test] Wrong velocity on polyhedron") + model_polyhedron += 1 + + +# Barycenters of the first Block only (e.g. a simulation of some regions) +def create_first_block_time_series(brep, nb_missing_polyhedra): + barycenters = create_cell_barycenters(brep, geometry.Point3D()) + nb_kept = next(iter(brep.blocks())).mesh().nb_polyhedra() - nb_missing_polyhedra + to_delete = [point >= nb_kept for point in range(barycenters.nb_vertices())] + mesh.PointSetBuilder3D.create(barycenters).delete_vertices(to_delete) + time_series = model.ExternalTimeSeries() + time_series.set_cell_barycenters(barycenters) + return time_series + + +# The Blocks absent from the simulation are left untouched +def check_single_block(filename): + brep = opengeode.load_one(model.load_brep(filename), filename) + create_first_block_time_series(brep, 0).apply_to(brep) + blocks = list(brep.blocks()) + block_mesh = blocks[0].mesh() series = basic.AttributeTimeSeriesArrayDouble3( block_mesh.polyhedron_attribute_manager(), VELOCITY ) - if series.nb_time_steps() != 1 or series.time(0) != TIMES[1]: - raise ValueError("[Test] Wrong velocity time steps") for polyhedron in range(block_mesh.nb_polyhedra()): if series.value(0, polyhedron) != velocity(polyhedron): - raise ValueError("[Test] Wrong velocity on polyhedron") + raise ValueError("[Test] Wrong velocity on the first Block") + for block in blocks[1:]: + if block.mesh().polyhedron_attribute_manager().time_steps(VELOCITY): + raise ValueError("[Test] The other Blocks should be left untouched") + + +# A Block represented in the simulation must have all its polyhedra matched +def check_single_block_unmatched(filename): + brep = opengeode.load_one(model.load_brep(filename), filename) + try: + create_first_block_time_series(brep, 1).apply_to(brep) + except basic.OpenGeodeDataError: + pass + else: + raise ValueError("[Test] A partially matched Block should be rejected") + for block in brep.blocks(): + if block.mesh().polyhedron_attribute_manager().time_steps(VELOCITY): + raise ValueError("[Test] A rejected matching should write no value") if __name__ == "__main__": @@ -136,20 +181,19 @@ def check_velocities(brep, block_id): data_dir = os.path.abspath(os.path.join(test_dir, "../../../../tests/data")) filename = os.path.join(data_dir, "test_mesh3.og_brep") brep = opengeode.load_one(model.load_brep(filename), filename) - block_id = next(iter(brep.blocks())).id() - time_series = create_time_series(brep, block_id) + time_series = create_time_series(brep) output = "python_time_series." + model.ExternalTimeSeries.native_extension_static() model.save_external_time_series(time_series, output) model.load_external_time_series(output).apply_to(brep) check_pressures(brep) - check_velocities(brep, block_id) + check_velocities(brep) other = opengeode.load_one(model.load_brep(filename), filename) model.load_brep_time_series(other, output) check_pressures(other) wrong = create_time_series( - brep, block_id, geometry.Point3D([100 * basic.GLOBAL_EPSILON, 0.0, 0.0]) + brep, geometry.Point3D([100 * basic.GLOBAL_EPSILON, 0.0, 0.0]) ) try: wrong.apply_to(opengeode.load_one(model.load_brep(filename), filename)) @@ -157,3 +201,6 @@ def check_velocities(brep, block_id): pass else: raise ValueError("[Test] Translated barycenters should be rejected") + + check_single_block(filename) + check_single_block_unmatched(filename) diff --git a/include/geode/model/representation/core/external_time_series.hpp b/include/geode/model/representation/core/external_time_series.hpp index 072381e90..e736c4430 100644 --- a/include/geode/model/representation/core/external_time_series.hpp +++ b/include/geode/model/representation/core/external_time_series.hpp @@ -94,18 +94,28 @@ namespace geode * Write the transferable attributes on the Block meshes of the model: * each unique vertex on the closest model unique vertex (on all its * Block vertices), each cell barycenter on the Block polyhedron with - * the closest barycenter. Block elements matched with no point keep - * no value, Blocks matched with no point are left untouched. + * the closest barycenter. The simulation may cover only some Blocks: + * a Block is represented in the cell barycenters when one of its + * polyhedra is matched, in the unique vertices when one of its + * matched vertices belongs to no other Block. Every element of a + * represented Block must be matched with a point, the other Blocks + * are left untouched. Several points matched with the same element + * are accepted with an error log: the closest one is kept. Both + * point sets are matched before any value is written. * Attribute ids and properties (time steps included) are kept. A * Block time step with the same name and time as an applied one is * replaced. - * The matching tolerance is GLOBAL_EPSILON relative to the largest - * absolute coordinate of the points (when larger than 1), to accept - * points rounded by the simulator (e.g. stored in single precision). + * A point set without transferable attribute is not matched. + * The matching tolerance bounds the rounding of the points to single + * precision (as simulators often store them), computed from their + * largest absolute coordinates. Points computed in double precision + * from rounded coordinates (e.g. barycenters) are accepted, points + * rounded twice may not be. * @exception OpenGeodeException if a point is farther than the - * tolerance from its closest Block element, if several points are - * matched with the same Block element, or if a point is within the - * tolerance of several Block elements + * tolerance from its closest Block element, if a point is within the + * tolerance of several Block elements, or if an element of a + * represented Block is matched with no point. The model is then + * unchanged. */ void apply_to( BRep& model ) const; diff --git a/src/geode/model/representation/core/external_time_series.cpp b/src/geode/model/representation/core/external_time_series.cpp index a102aa974..35ec9c8ab 100644 --- a/src/geode/model/representation/core/external_time_series.cpp +++ b/src/geode/model/representation/core/external_time_series.cpp @@ -25,13 +25,16 @@ #include #include +#include #include #include #include +#include #include #include +#include #include #include #include @@ -90,45 +93,68 @@ namespace } } - // Coordinates are rounded relatively to their magnitude + // Largest rounding of a coordinate stored in single precision, as + // simulators often do: half the gap between consecutive floats around + // the coordinate, magnitudes below 1 being rounded as 1 + double single_precision_rounding( double coordinate ) + { + const auto magnitude = + static_cast< float >( std::max( std::fabs( coordinate ), 1. ) ); + const auto next = std::nextafter( + magnitude, std::numeric_limits< float >::infinity() ); + return ( static_cast< double >( next ) - magnitude ) / 2; + } + + // Points may be rounded to single precision: the tolerance bounds this + // rounding over the point set. Points computed in double precision from + // rounded coordinates (e.g. barycenters of rounded vertices) are within + // the same bound, GLOBAL_EPSILON is added for the double precision + // computations. Points rounded twice (e.g. barycenters of rounded + // vertices written in single precision) are not covered: a wider + // tolerance would reject meshes whose elements are closer than one + // float gap. double matching_tolerance( const geode::PointSet3D& point_set ) { const auto box = point_set.bounding_box(); - double largest_coordinate{ 1 }; - for( const auto& point : { box.min(), box.max() } ) + double squared_rounding{ 0 }; + for( const auto axis : geode::LRange{ 3 } ) { - for( const auto axis : geode::LRange{ 3 } ) - { - largest_coordinate = std::max( - largest_coordinate, std::fabs( point.value( axis ) ) ); - } + const auto largest_coordinate = + std::max( std::fabs( box.min().value( axis ) ), + std::fabs( box.max().value( axis ) ) ); + const auto rounding = + single_precision_rounding( largest_coordinate ); + squared_rounding += rounding * rounding; } - return geode::GLOBAL_EPSILON * largest_coordinate; + return std::sqrt( squared_rounding ) + geode::GLOBAL_EPSILON; } - // Each point is matched with the closest element, elements without - // point (e.g. outside of the simulated domain) are not matched. - // Matching is rejected when ambiguous: several points matched with the - // same element, or a point within the tolerance of several elements. - // Returns the point matched with each element, NO_ID if none. - std::vector< geode::index_t > match_points( - const geode::PointSet3D& point_set, + // Point sets without transferable attribute have nothing to apply + bool has_transferable_attribute( const geode::PointSet3D& point_set ) + { + const auto& manager = point_set.vertex_attribute_manager(); + return absl::c_any_of( + manager.attribute_ids(), [&manager]( const geode::uuid& id ) { + return manager.find_generic_attribute( id ) + ->properties() + .transferable; + } ); + } + + // Each point is matched with the closest element. Matching is rejected + // when a point is within the tolerance of several elements. Several + // points matched with the same element (e.g. a vertex written with + // slightly different coordinates by several datasets) are accepted with + // a warning: the closest one is kept, the first one on a tie. + void match_closest_points( const geode::PointSet3D& point_set, std::string_view point_set_name, - std::vector< geode::Point3D > element_points ) + const geode::NNSearch3D& search, + std::vector< geode::index_t >& matches ) { - std::vector< geode::index_t > matches( - element_points.size(), geode::NO_ID ); - if( point_set.nb_vertices() == 0 ) - { - return matches; - } - geode::OpenGeodeModelException::check_exception( - !element_points.empty(), nullptr, - geode::OpenGeodeException::TYPE::data, - "[ExternalTimeSeries::apply_to] No Block element to match the ", - point_set_name, " with" ); const auto tolerance = matching_tolerance( point_set ); - const geode::NNSearch3D search{ std::move( element_points ) }; + std::vector< double > distances( matches.size() ); + std::vector< bool > shared( matches.size(), false ); + geode::index_t nb_shared{ 0 }; for( const auto vertex : geode::Range{ point_set.nb_vertices() } ) { const auto& point = point_set.point( vertex ); @@ -154,18 +180,165 @@ namespace point_set_name, " [", point.string(), "] is within the tolerance ", tolerance, " of several Block elements" ); + if( matches[element] == geode::NO_ID ) + { + matches[element] = vertex; + distances[element] = distance; + continue; + } + if( !shared[element] ) + { + shared[element] = true; + nb_shared++; + } + if( distance < distances[element] ) + { + matches[element] = vertex; + distances[element] = distance; + } + } + if( nb_shared > 0 ) + { + geode::Logger::error( "[ExternalTimeSeries::apply_to] ", nb_shared, + " Block elements are matched with several points " + "of the ", + point_set_name, ": the closest point is kept" ); + } + } + + // Returns the point matched with each element, NO_ID if none + std::vector< geode::index_t > match_points( + const geode::PointSet3D& point_set, + std::string_view point_set_name, + std::string_view element_name, + std::vector< geode::Point3D > element_points ) + { + std::vector< geode::index_t > matches( + element_points.size(), geode::NO_ID ); + if( element_points.empty() ) + { geode::OpenGeodeModelException::check_exception( - matches[element] == geode::NO_ID, nullptr, + point_set.nb_vertices() == 0, nullptr, geode::OpenGeodeException::TYPE::data, - "[ExternalTimeSeries::apply_to] Points ", matches[element], - " and ", vertex, " of the ", point_set_name, - " are matched with the same Block element [", - search.point( element ).string(), "]" ); - matches[element] = vertex; + "[ExternalTimeSeries::apply_to] No ", element_name, + " to match the ", point_set_name, " with" ); + return matches; + } + const geode::NNSearch3D search{ std::move( element_points ) }; + if( point_set.nb_vertices() != 0 ) + { + match_closest_points( point_set, point_set_name, search, matches ); } return matches; } + // Point matched with each element of each Block (NO_ID if none) and + // whether each Block is represented in the point set + struct BlocksMatches + { + BlocksIndices matches; + std::vector< bool > represented; + }; + + // Names used by the error messages + struct MatchNames + { + std::string_view point_set; + std::string_view element; + }; + + // Required elements of a Block matched with no point + template < typename IsRequired > + std::vector< geode::index_t > unmatched_elements( + absl::Span< const geode::index_t > block_matches, + const IsRequired& is_required ) + { + std::vector< geode::index_t > unmatched; + for( const auto element : geode::Indices{ block_matches } ) + { + if( block_matches[element] == geode::NO_ID + && is_required( element ) ) + { + unmatched.push_back( element ); + } + } + return unmatched; + } + + // A simulation may cover only some Blocks (e.g. some GEOS regions). A + // Block represented in the point set must have all its elements matched + // (the mapping between its elements and their points is then + // bijective). The matches of the other Blocks are removed: they are + // left untouched. + template < typename IsRequired > + void check_represented_blocks( const geode::BRep& model, + const MatchNames& names, + const IsRequired& is_required, + BlocksMatches& blocks_matches ) + { + geode::index_t nb_unmatched{ 0 }; + std::string first_unmatched; + geode::index_t block_index{ 0 }; + for( const auto& block : model.blocks() ) + { + auto& block_matches = blocks_matches.matches[block_index]; + if( !blocks_matches.represented[block_index] ) + { + absl::c_fill( block_matches, geode::NO_ID ); + block_index++; + continue; + } + const auto unmatched = unmatched_elements( block_matches, + [&is_required, block_index]( geode::index_t element ) { + return is_required( block_index, element ); + } ); + if( nb_unmatched == 0 && !unmatched.empty() ) + { + first_unmatched = absl::StrCat( unmatched.front(), " of Block ", + block.name().value_or( "" ), " (", block.id().string(), + ")" ); + } + nb_unmatched += static_cast< geode::index_t >( unmatched.size() ); + block_index++; + } + geode::OpenGeodeModelException::check_exception( nb_unmatched == 0, + nullptr, geode::OpenGeodeException::TYPE::data, + "[ExternalTimeSeries::apply_to] ", nb_unmatched, + " elements of the Blocks represented in the ", names.point_set, + " are matched with no point (e.g. ", names.element, " ", + first_unmatched, ")" ); + } + + bool belongs_to_block_only( const geode::BRep& model, + geode::index_t unique_vertex, + const geode::uuid& block_id ) + { + return absl::c_all_of( model.component_mesh_vertices( unique_vertex ), + [&block_id]( const geode::ComponentMeshVertex& vertex ) { + return vertex.component_id.type + != geode::Block3D::component_type_static() + || vertex.component_id.id == block_id; + } ); + } + + // One of the matched vertices belongs to no other Block + bool is_represented_by_vertices( const geode::BRep& model, + const geode::uuid& block_id, + absl::Span< const geode::index_t > unique_vertices, + absl::Span< const geode::index_t > block_matches ) + { + for( const auto vertex : geode::Indices{ block_matches } ) + { + if( block_matches[vertex] != geode::NO_ID + && belongs_to_block_only( + model, unique_vertices[vertex], block_id ) ) + { + return true; + } + } + return false; + } + // Unique vertex of each Block vertex, NO_ID if none BlocksIndices block_unique_vertices( const geode::BRep& model ) { @@ -211,8 +384,8 @@ namespace model_unique_vertices.push_back( unique_vertex ); } } - const auto point_matches = match_points( - unique_vertices, "unique vertices", std::move( points ) ); + const auto point_matches = match_points( unique_vertices, + "unique vertices", "model unique vertices", std::move( points ) ); std::vector< geode::index_t > matches( model.nb_unique_vertices(), geode::NO_ID ); for( const auto index : geode::Indices{ model_unique_vertices } ) @@ -223,32 +396,53 @@ namespace } // Matching is done on the model unique vertices so that the colocated - // vertices of several Blocks get the same values. + // vertices of several Blocks get the same values. A Block is represented + // in the unique vertices when one of its matched vertices belongs to no + // other Block: the vertices shared with a simulated Block do not + // represent the Blocks around it. // Returns the point matched with each Block vertex, NO_ID if none. BlocksIndices match_block_vertices( const geode::BRep& model, const geode::PointSet3D& unique_vertices ) { - auto matches = block_unique_vertices( model ); + const auto block_vertices = block_unique_vertices( model ); const auto unique_matches = - match_unique_vertices( model, matches, unique_vertices ); - for( auto& block_matches : matches ) + match_unique_vertices( model, block_vertices, unique_vertices ); + BlocksMatches blocks_matches; + blocks_matches.matches.reserve( model.nb_blocks() ); + blocks_matches.represented.reserve( model.nb_blocks() ); + geode::index_t block_index{ 0 }; + for( const auto& block : model.blocks() ) { - for( auto& match : block_matches ) + const auto& vertices = block_vertices[block_index++]; + auto& block_matches = blocks_matches.matches.emplace_back( + vertices.size(), geode::NO_ID ); + for( const auto vertex : geode::Indices{ vertices } ) { - if( match != geode::NO_ID ) + if( vertices[vertex] != geode::NO_ID ) { - match = unique_matches[match]; + block_matches[vertex] = unique_matches[vertices[vertex]]; } } + blocks_matches.represented.push_back( is_represented_by_vertices( + model, block.id(), vertices, block_matches ) ); } - return matches; + check_represented_blocks( + model, { "unique vertices", "vertex" }, + [&block_vertices]( geode::index_t block, geode::index_t vertex ) { + return block_vertices[block][vertex] != geode::NO_ID; + }, + blocks_matches ); + return std::move( blocks_matches.matches ); } + // A Block is represented in the cell barycenters when one of its + // polyhedra is matched. // Returns the point matched with each Block polyhedron, NO_ID if none BlocksIndices match_block_polyhedra( const geode::BRep& model, const geode::PointSet3D& cell_barycenters ) { - BlocksIndices matches; + BlocksMatches blocks_matches; + auto& matches = blocks_matches.matches; matches.reserve( model.nb_blocks() ); std::vector< geode::Point3D > barycenters; std::vector< BlockPolyhedron > polyhedra; @@ -265,15 +459,29 @@ namespace polyhedra.push_back( { block_index, polyhedron } ); } } - const auto point_matches = match_points( - cell_barycenters, "cell barycenters", std::move( barycenters ) ); + const auto point_matches = match_points( cell_barycenters, + "cell barycenters", "Block polyhedra", std::move( barycenters ) ); for( const auto index : geode::Indices{ polyhedra } ) { const auto& block_polyhedron = polyhedra[index]; matches[block_polyhedron.block][block_polyhedron.polyhedron] = point_matches[index]; } - return matches; + blocks_matches.represented.reserve( model.nb_blocks() ); + for( const auto& block_matches : matches ) + { + blocks_matches.represented.push_back( + absl::c_any_of( block_matches, []( geode::index_t point ) { + return point != geode::NO_ID; + } ) ); + } + check_represented_blocks( + model, { "cell barycenters", "polyhedron" }, + []( geode::index_t /*block*/, geode::index_t /*polyhedron*/ ) { + return true; + }, + blocks_matches ); + return std::move( matches ); } // Element filled by each point, std::nullopt if a point fills several @@ -369,22 +577,39 @@ namespace geode cell_barycenters_ = std::move( barycenters ); } + // Both point sets are matched before writing any value: a rejected + // matching leaves the model unchanged void apply_to( const BRep& model ) const { - const auto vertex_matches = - match_block_vertices( model, *unique_vertices_ ); - const auto polyhedron_matches = - match_block_polyhedra( model, *cell_barycenters_ ); + std::optional< BlocksIndices > vertex_matches; + if( has_transferable_attribute( *unique_vertices_ ) ) + { + vertex_matches = + match_block_vertices( model, *unique_vertices_ ); + } + std::optional< BlocksIndices > polyhedron_matches; + if( has_transferable_attribute( *cell_barycenters_ ) ) + { + polyhedron_matches = + match_block_polyhedra( model, *cell_barycenters_ ); + } index_t block_index{ 0 }; for( const auto& block : model.blocks() ) { const auto& mesh = block.mesh(); - apply_matches( unique_vertices_->vertex_attribute_manager(), - vertex_matches[block_index], - mesh.vertex_attribute_manager() ); - apply_matches( cell_barycenters_->vertex_attribute_manager(), - polyhedron_matches[block_index], - mesh.polyhedron_attribute_manager() ); + if( vertex_matches ) + { + apply_matches( unique_vertices_->vertex_attribute_manager(), + vertex_matches.value()[block_index], + mesh.vertex_attribute_manager() ); + } + if( polyhedron_matches ) + { + apply_matches( + cell_barycenters_->vertex_attribute_manager(), + polyhedron_matches.value()[block_index], + mesh.polyhedron_attribute_manager() ); + } block_index++; } } diff --git a/tests/model/test-external-time-series.cpp b/tests/model/test-external-time-series.cpp index bb48ccea0..9528390b6 100644 --- a/tests/model/test-external-time-series.cpp +++ b/tests/model/test-external-time-series.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -40,11 +41,13 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -59,16 +62,25 @@ namespace constexpr auto PRESSURE = "pressure"; constexpr auto VELOCITY = "velocity"; constexpr std::array< double, 2 > TIMES{ 0., 10. }; - // test_mesh3 coordinates go up to 10: the matching tolerance is - // 10 * GLOBAL_EPSILON + // test_mesh3 coordinates go up to 10: the matching tolerance (single + // precision rounding + GLOBAL_EPSILON) is about 2 * GLOBAL_EPSILON constexpr double ROUNDING_TRANSLATION{ geode::GLOBAL_EPSILON }; constexpr double WRONG_TRANSLATION{ 100 * geode::GLOBAL_EPSILON }; + // Georeferenced model: test_mesh3 barycenters are at least 0.19 apart, + // scaled to stay farther apart than single precision rounding of + // typical UTM coordinates (up to 0.28) + constexpr double UTM_SCALE{ 10. }; + const geode::Vector3D UTM_TRANSLATION{ { 500000., 7000000., 3000. } }; + // Beyond the single precision rounding (about 0.25 here), below the + // barycenter spacing + constexpr double UTM_WRONG_TRANSLATION{ 1. }; double pressure( geode::index_t unique_vertex, double time ) { return unique_vertex + time; } + // Polyhedra are numbered over all the Blocks, in model.blocks() order std::array< double, 3 > velocity( geode::index_t polyhedron ) { return { static_cast< double >( polyhedron ), 1., 2. }; @@ -86,13 +98,13 @@ namespace attribute_id ); } - // One point per model unique vertex - std::unique_ptr< geode::PointSet3D > create_unique_vertices( - const geode::BRep& model ) + // One point per model unique vertex, in the order of the Block vertices + std::unique_ptr< geode::PointSet3D > create_unique_vertex_points( + const geode::BRep& model, + std::vector< geode::index_t >& unique_vertices ) { auto vertices = geode::PointSet3D::create(); auto builder = geode::PointSetBuilder3D::create( *vertices ); - std::vector< geode::index_t > unique_vertices; std::vector< bool > visited( model.nb_unique_vertices(), false ); for( const auto& block : model.blocks() ) { @@ -110,6 +122,14 @@ namespace unique_vertices.push_back( unique_vertex ); } } + return vertices; + } + + std::unique_ptr< geode::PointSet3D > create_unique_vertices( + const geode::BRep& model ) + { + std::vector< geode::index_t > unique_vertices; + auto vertices = create_unique_vertex_points( model, unique_vertices ); for( const auto time : TIMES ) { auto attribute = create_time_step< double >( @@ -123,41 +143,54 @@ namespace return vertices; } - // Barycenters of a single Block: the other Blocks get no velocity + // One point per Block polyhedron, Blocks in model.blocks() order std::unique_ptr< geode::PointSet3D > create_cell_barycenters( - const geode::BRep& model, - const geode::uuid& block_id, - const geode::Vector3D& translation ) + const geode::BRep& model, const geode::Vector3D& translation ) { auto barycenters = geode::PointSet3D::create(); auto builder = geode::PointSetBuilder3D::create( *barycenters ); - const auto& mesh = model.block( block_id ).mesh(); auto attribute = create_time_step< std::array< double, 3 > >( barycenters->vertex_attribute_manager(), VELOCITY, TIMES[1] ); - for( const auto polyhedron : geode::Range{ mesh.nb_polyhedra() } ) + for( const auto& block : model.blocks() ) { - const auto point = builder->create_point( - mesh.polyhedron_barycenter( polyhedron ) + translation ); - attribute->set_value( point, velocity( polyhedron ) ); + const auto& mesh = block.mesh(); + for( const auto polyhedron : geode::Range{ mesh.nb_polyhedra() } ) + { + const auto point = builder->create_point( + mesh.polyhedron_barycenter( polyhedron ) + translation ); + attribute->set_value( point, velocity( point ) ); + } } return barycenters; } - geode::ExternalTimeSeries create_time_series( const geode::BRep& model, - const geode::uuid& block_id, - const geode::Vector3D& translation ) + std::unique_ptr< geode::PointSet3D > create_cell_barycenters( + const geode::BRep& model ) + { + return create_cell_barycenters( model, geode::Vector3D{} ); + } + + geode::ExternalTimeSeries create_time_series( + const geode::BRep& model, const geode::Vector3D& translation ) { geode::ExternalTimeSeries time_series; time_series.set_unique_vertices( create_unique_vertices( model ) ); time_series.set_cell_barycenters( - create_cell_barycenters( model, block_id, translation ) ); + create_cell_barycenters( model, translation ) ); return time_series; } - geode::ExternalTimeSeries create_time_series( - const geode::BRep& model, const geode::uuid& block_id ) + geode::ExternalTimeSeries create_time_series( const geode::BRep& model ) { - return create_time_series( model, block_id, geode::Vector3D{} ); + return create_time_series( model, geode::Vector3D{} ); + } + + geode::BRep load_model() + { + const auto model_filename = + absl::StrCat( geode::DATA_PATH, "test_mesh3.og_brep" ); + return geode::load_one( + geode::load_brep( model_filename ), model_filename ); } void check_pressures( const geode::BRep& model ) @@ -191,38 +224,63 @@ namespace } } - void check_velocities( - const geode::BRep& model, const geode::uuid& block_id ) + void check_velocities( const geode::BRep& model ) { - const auto& mesh = model.block( block_id ).mesh(); - const geode::AttributeTimeSeries< std::array< double, 3 > > series{ - mesh.polyhedron_attribute_manager(), VELOCITY - }; - geode::OpenGeodeModelException::test( - series.nb_time_steps() == 1 && series.time( 0 ) == TIMES[1], - "[Test] Wrong velocity time steps" ); - for( const auto polyhedron : geode::Range{ mesh.nb_polyhedra() } ) + geode::index_t model_polyhedron{ 0 }; + for( const auto& block : model.blocks() ) { + const auto& mesh = block.mesh(); + const geode::AttributeTimeSeries< std::array< double, 3 > > series{ + mesh.polyhedron_attribute_manager(), VELOCITY + }; geode::OpenGeodeModelException::test( - series.value( 0, polyhedron ) == velocity( polyhedron ), - "[Test] Wrong velocity on polyhedron ", polyhedron ); + series.nb_time_steps() == 1 && series.time( 0 ) == TIMES[1], + "[Test] Wrong velocity time steps on Block ", + block.id().string() ); + for( const auto polyhedron : geode::Range{ mesh.nb_polyhedra() } ) + { + geode::OpenGeodeModelException::test( + series.value( 0, polyhedron ) + == velocity( model_polyhedron++ ), + "[Test] Wrong velocity on polyhedron ", polyhedron, + " of Block ", block.id().string() ); + } } + } + + // A rejected matching leaves the model unchanged + void check_no_values( const geode::BRep& model ) + { for( const auto& block : model.blocks() ) { - if( block.id() == block_id ) - { - continue; - } + const auto& mesh = block.mesh(); geode::OpenGeodeModelException::test( - block.mesh() - .polyhedron_attribute_manager() - .time_steps( VELOCITY ) - .empty(), + mesh.vertex_attribute_manager().time_steps( PRESSURE ).empty() + && mesh.polyhedron_attribute_manager() + .time_steps( VELOCITY ) + .empty(), "[Test] Block ", block.id().string(), - " should have no velocity" ); + " should have no value after a rejected matching" ); } } + void check_apply_throws( const geode::ExternalTimeSeries& time_series, + geode::BRep& model, + std::string_view message ) + { + bool thrown{ false }; + try + { + time_series.apply_to( model ); + } + catch( const geode::OpenGeodeException& ) + { + thrown = true; + } + geode::OpenGeodeModelException::test( thrown, "[Test] ", message ); + check_no_values( model ); + } + void check_other_model( const geode::ExternalTimeSeries& time_series ) { const auto other_filename = @@ -245,18 +303,15 @@ namespace // Translated barycenters are matched only with a large enough tolerance // Barycenters translated below the tolerance are matched, beyond it // they are rejected - void check_tolerance( const geode::uuid& block_id ) + void check_tolerance() { - const auto model_filename = - absl::StrCat( geode::DATA_PATH, "test_mesh3.og_brep" ); - auto model = geode::load_one( - geode::load_brep( model_filename ), model_filename ); + auto model = load_model(); create_time_series( - model, block_id, geode::Vector3D{ { ROUNDING_TRANSLATION, 0, 0 } } ) + model, geode::Vector3D{ { ROUNDING_TRANSLATION, 0, 0 } } ) .apply_to( model ); - check_velocities( model, block_id ); + check_velocities( model ); const auto wrong_time_series = create_time_series( - model, block_id, geode::Vector3D{ { WRONG_TRANSLATION, 0, 0 } } ); + model, geode::Vector3D{ { WRONG_TRANSLATION, 0, 0 } } ); bool thrown{ false }; try { @@ -271,19 +326,311 @@ namespace "should throw" ); } - // Values of colocated points cannot be told apart - void check_colocated_points( const geode::uuid& block_id ) + template < typename T > + void set_values( geode::PointSet3D& point_set, + std::string_view name, + geode::index_t point, + const T& value ) { - const auto model_filename = - absl::StrCat( geode::DATA_PATH, "test_mesh3.og_brep" ); - auto model = geode::load_one( - geode::load_brep( model_filename ), model_filename ); - auto barycenters = - create_cell_barycenters( model, block_id, geode::Vector3D{} ); - geode::PointSetBuilder3D::create( *barycenters ) - ->create_point( barycenters->point( 0 ) ); + auto& manager = point_set.vertex_attribute_manager(); + for( const auto& step : manager.time_steps( name ) ) + { + manager + .find_attribute< geode::VariableAttribute, T >( + step.attribute_id ) + ->set_value( point, value ); + } + } + + // A point is written twice by the simulator with coordinates too far + // apart to be merged, but both are within the tolerance of the same + // element: the closest one is kept, the first one on a tie + void check_shared_polyhedra() + { + auto model = load_model(); + auto barycenters = create_cell_barycenters( model ); + auto builder = geode::PointSetBuilder3D::create( *barycenters ); + const auto barycenter0 = barycenters->point( 0 ); + builder->set_point( 0, + barycenter0 + geode::Vector3D{ { ROUNDING_TRANSLATION, 0, 0 } } ); + set_values( *barycenters, VELOCITY, 0, std::array{ -1., -1., -1. } ); + const auto closest = builder->create_point( barycenter0 ); + set_values( *barycenters, VELOCITY, closest, velocity( 0 ) ); + const auto tie = builder->create_point( barycenters->point( 1 ) ); + set_values( *barycenters, VELOCITY, tie, std::array{ -1., -1., -1. } ); geode::ExternalTimeSeries time_series; time_series.set_cell_barycenters( std::move( barycenters ) ); + time_series.apply_to( model ); + check_velocities( model ); + } + + void check_shared_vertices() + { + auto model = load_model(); + auto vertices = create_unique_vertices( model ); + auto builder = geode::PointSetBuilder3D::create( *vertices ); + const auto vertex0 = vertices->point( 0 ); + auto& manager = vertices->vertex_attribute_manager(); + std::vector< double > values; + for( const auto& step : manager.time_steps( PRESSURE ) ) + { + values.push_back( manager + .find_attribute< geode::VariableAttribute, double >( + step.attribute_id ) + ->value( 0 ) ); + } + builder->set_point( + 0, vertex0 + geode::Vector3D{ { ROUNDING_TRANSLATION, 0, 0 } } ); + set_values( *vertices, PRESSURE, 0, -1. ); + const auto closest = builder->create_point( vertex0 ); + geode::index_t step_index{ 0 }; + for( const auto& step : manager.time_steps( PRESSURE ) ) + { + manager + .find_attribute< geode::VariableAttribute, double >( + step.attribute_id ) + ->set_value( closest, values[step_index++] ); + } + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( std::move( vertices ) ); + time_series.apply_to( model ); + check_pressures( model ); + } + + void delete_points_from( + geode::PointSet3D& point_set, geode::index_t first ) + { + std::vector< bool > to_delete( point_set.nb_vertices(), false ); + for( const auto point : geode::Range{ first, point_set.nb_vertices() } ) + { + to_delete[point] = true; + } + geode::PointSetBuilder3D::create( point_set ) + ->delete_vertices( to_delete ); + } + + // The vertex values are matched but not written + void check_unmatched_polyhedron() + { + auto model = load_model(); + auto barycenters = create_cell_barycenters( model ); + delete_points_from( *barycenters, barycenters->nb_vertices() - 1 ); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( create_unique_vertices( model ) ); + time_series.set_cell_barycenters( std::move( barycenters ) ); + check_apply_throws( time_series, model, + "Applying with a polyhedron matched with no barycenter should " + "throw" ); + } + + void check_unmatched_vertex() + { + auto model = load_model(); + auto vertices = create_unique_vertices( model ); + delete_points_from( *vertices, vertices->nb_vertices() - 1 ); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( std::move( vertices ) ); + time_series.set_cell_barycenters( create_cell_barycenters( model ) ); + check_apply_throws( time_series, model, + "Applying with a unique vertex matched with no point should " + "throw" ); + } + + // Values of the first Block only (e.g. a simulation of some regions): + // its unique vertices, including the ones shared with the other Block, + // and its polyhedra + geode::ExternalTimeSeries create_first_block_time_series( + const geode::BRep& model, geode::index_t nb_missing_polyhedra ) + { + const auto& first_block = *model.blocks().begin(); + const auto& first_mesh = first_block.mesh(); + absl::flat_hash_set< geode::index_t > first_unique_vertices; + for( const auto vertex : geode::Range{ first_mesh.nb_vertices() } ) + { + first_unique_vertices.insert( + model.unique_vertex( { first_block.component_id(), vertex } ) ); + } + // create_unique_vertices lists the first Block vertices first + auto vertices = create_unique_vertices( model ); + delete_points_from( *vertices, + static_cast< geode::index_t >( first_unique_vertices.size() ) ); + auto barycenters = create_cell_barycenters( model ); + delete_points_from( + *barycenters, first_mesh.nb_polyhedra() - nb_missing_polyhedra ); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( std::move( vertices ) ); + time_series.set_cell_barycenters( std::move( barycenters ) ); + return time_series; + } + + void check_first_block_values( const geode::BRep& model ) + { + bool first{ true }; + for( const auto& block : model.blocks() ) + { + const auto& mesh = block.mesh(); + const auto& vertex_manager = mesh.vertex_attribute_manager(); + const auto& polyhedron_manager = + mesh.polyhedron_attribute_manager(); + if( !first ) + { + geode::OpenGeodeModelException::test( + vertex_manager.time_steps( PRESSURE ).empty() + && polyhedron_manager.time_steps( VELOCITY ).empty(), + "[Test] A Block absent from the simulation should be left " + "untouched" ); + continue; + } + first = false; + const geode::AttributeTimeSeries< double > pressures{ + vertex_manager, PRESSURE + }; + geode::OpenGeodeModelException::test( + pressures.nb_time_steps() == TIMES.size(), + "[Test] Wrong number of pressure time steps on the first " + "Block" ); + for( const auto vertex : geode::Range{ mesh.nb_vertices() } ) + { + const auto unique_vertex = + model.unique_vertex( { block.component_id(), vertex } ); + geode::OpenGeodeModelException::test( + pressures.value( 1, vertex ) + == pressure( unique_vertex, TIMES[1] ), + "[Test] Wrong pressure on vertex ", vertex, + " of the first Block" ); + } + const geode::AttributeTimeSeries< std::array< double, 3 > > + velocities{ polyhedron_manager, VELOCITY }; + geode::OpenGeodeModelException::test( + velocities.nb_time_steps() == 1, + "[Test] Wrong velocity time steps on the first Block" ); + for( const auto polyhedron : geode::Range{ mesh.nb_polyhedra() } ) + { + geode::OpenGeodeModelException::test( + velocities.value( 0, polyhedron ) == velocity( polyhedron ), + "[Test] Wrong velocity on polyhedron ", polyhedron, + " of the first Block" ); + } + } + } + + // The other Block is left untouched, even if it shares vertices with + // the first one + void check_single_block() + { + auto model = load_model(); + create_first_block_time_series( model, 0 ).apply_to( model ); + check_first_block_values( model ); + } + + // A Block represented in the simulation must have all its elements + // matched + void check_single_block_unmatched() + { + auto model = load_model(); + check_apply_throws( create_first_block_time_series( model, 1 ), model, + "Applying the first Block values without one of its polyhedra " + "should throw" ); + } + + // Simulators often write single precision coordinates + geode::Point3D round_to_float( const geode::Point3D& point ) + { + geode::Point3D rounded; + for( const auto axis : geode::LRange{ 3 } ) + { + rounded.set_value( + axis, static_cast< float >( point.value( axis ) ) ); + } + return rounded; + } + + std::unique_ptr< geode::PointSet3D > round_to_float( + const geode::PointSet3D& point_set ) + { + auto rounded = point_set.clone(); + auto builder = geode::PointSetBuilder3D::create( *rounded ); + for( const auto vertex : geode::Range{ rounded->nb_vertices() } ) + { + builder->set_point( + vertex, round_to_float( rounded->point( vertex ) ) ); + } + return rounded; + } + + // Unique vertices without values are not matched, even when they + // could not be + void check_unattributed_vertices() + { + auto model = load_model(); + std::vector< geode::index_t > unique_vertices; + auto vertices = create_unique_vertex_points( model, unique_vertices ); + geode::PointSetBuilder3D::create( *vertices ) + ->create_point( vertices->point( 0 ) ); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( std::move( vertices ) ); + time_series.set_cell_barycenters( create_cell_barycenters( model ) ); + time_series.apply_to( model ); + check_velocities( model ); + } + + geode::BRep load_georeferenced_model() + { + auto model = load_model(); + geode::BRepBuilder model_builder{ model }; + for( const auto& block : model.blocks() ) + { + const auto& mesh = block.mesh(); + const auto mesh_builder = model_builder.block_mesh_builder( block ); + for( const auto vertex : geode::Range{ mesh.nb_vertices() } ) + { + mesh_builder->set_point( vertex, + mesh.point( vertex ) * UTM_SCALE + UTM_TRANSLATION ); + } + } + return model; + } + + // Georeferenced model with only cell values written in single + // precision: the unique vertices carry no value + void check_georeferenced_cell_values() + { + auto model = load_georeferenced_model(); + std::vector< geode::index_t > unique_vertices; + const auto vertices = + create_unique_vertex_points( model, unique_vertices ); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( round_to_float( *vertices ) ); + time_series.set_cell_barycenters( + round_to_float( *create_cell_barycenters( model ) ) ); + time_series.apply_to( model ); + check_velocities( model ); + } + + // Georeferenced model with vertex and cell values written in single + // precision + void check_georeferenced_values() + { + auto model = load_georeferenced_model(); + geode::ExternalTimeSeries time_series; + time_series.set_unique_vertices( + round_to_float( *create_unique_vertices( model ) ) ); + time_series.set_cell_barycenters( + round_to_float( *create_cell_barycenters( model ) ) ); + time_series.apply_to( model ); + check_pressures( model ); + check_velocities( model ); + } + + // Points translated beyond the single precision rounding of UTM + // coordinates are rejected + void check_georeferenced_tolerance() + { + auto model = load_georeferenced_model(); + geode::ExternalTimeSeries time_series; + time_series.set_cell_barycenters( + round_to_float( *create_cell_barycenters( + model, geode::Vector3D{ { 0, UTM_WRONG_TRANSLATION, 0 } } ) ) ); bool thrown{ false }; try { @@ -293,8 +640,9 @@ namespace { thrown = true; } - geode::OpenGeodeModelException::test( - thrown, "[Test] Applying colocated barycenters should throw" ); + geode::OpenGeodeModelException::test( thrown, + "[Test] Applying georeferenced barycenters translated beyond the " + "tolerance should throw" ); } void check_null_point_set() @@ -333,36 +681,37 @@ namespace void test() { geode::OpenGeodeModelLibrary::initialize(); - const auto model_filename = - absl::StrCat( geode::DATA_PATH, "test_mesh3.og_brep" ); - auto model = - geode::load_one( geode::load_brep( model_filename ), model_filename ); + auto model = load_model(); geode::OpenGeodeModelException::test( - model.nb_blocks() > 0, "[Test] Model has no Block" ); - const auto block_id = ( *model.blocks().begin() ).id(); + model.nb_blocks() > 1, "[Test] Model should have several Blocks" ); const auto filename = absl::StrCat( "test_external_time_series.", geode::ExternalTimeSeries::native_extension_static() ); - geode::save_external_time_series( - create_time_series( model, block_id ), filename ); + geode::save_external_time_series( create_time_series( model ), filename ); const auto time_series = geode::load_external_time_series( filename ); time_series.apply_to( model ); check_pressures( model ); - check_velocities( model, block_id ); - create_time_series( model, block_id ).apply_to( model ); + check_velocities( model ); + create_time_series( model ).apply_to( model ); check_pressures( model ); - check_velocities( model, block_id ); + check_velocities( model ); - const auto reloaded_model_filename = - absl::StrCat( geode::DATA_PATH, "test_mesh3.og_brep" ); - auto reloaded_model = geode::load_one( - geode::load_brep( reloaded_model_filename ), reloaded_model_filename ); + auto reloaded_model = load_model(); geode::load_brep_time_series( reloaded_model, filename ); check_pressures( reloaded_model ); - check_velocities( reloaded_model, block_id ); + check_velocities( reloaded_model ); check_other_model( time_series ); - check_tolerance( block_id ); - check_colocated_points( block_id ); + check_tolerance(); + check_shared_polyhedra(); + check_shared_vertices(); + check_unmatched_polyhedron(); + check_unmatched_vertex(); + check_single_block(); + check_single_block_unmatched(); + check_unattributed_vertices(); + check_georeferenced_cell_values(); + check_georeferenced_values(); + check_georeferenced_tolerance(); check_null_point_set(); check_wrong_extension( time_series ); }