Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
Apply offset
  • Loading branch information
lucacarniato committed Sep 15, 2023
1 parent 75f8c9d commit cfd304e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
24 changes: 24 additions & 0 deletions include/UGrid/UGridEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ namespace ugrid
/// @return The variables names
[[nodiscard]] std::vector<std::string> get_data_variables_names(std::string const& location_string);

/// @brief This function adjusts the values in an input array based on the difference between the provided `start_index`
/// and the `start_index` stored in the specified NetCDF variable's attribute named "start_index". If the attribute is not found, no adjustments are made to the array.
/// @tparam T The data type of the input array `values`.
/// @param[in] var The NetCDF variable from which to retrieve the "start_index" attribute.
/// @param[in] start_index The desired start index for the array.
/// @param[in] values_size The size of the input array `values`.
/// @param[in,out] values The input array to which the offset is applied.
template <typename T>
static void apply_start_index_offset(const netCDF::NcVar& var, int start_index, int values_size, T* values)
{
const auto varAtt = var.getAtts();
std::string start_index_att_name{"start_index"};
if (varAtt.find(start_index_att_name) != varAtt.end())
{
int variable_start_index;
varAtt.at(start_index_att_name).getValues(&variable_start_index);
int offset = start_index - variable_start_index;
for (auto i = 0; i < values_size; ++i)
{
values[i] += offset;
}
}
}

protected:
/// @brief Method collecting common operations for defining a UGrid entity to file
/// @param entity_name [in] The entity name
Expand Down
16 changes: 12 additions & 4 deletions src/UGrid/Mesh2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ void Mesh2D::get(ugridapi::Mesh2D& mesh2d) const
// Edges
if (auto const it = m_topology_attribute_variables.find("edge_node_connectivity"); mesh2d.edge_nodes != nullptr && it != m_topology_attribute_variables.end())
{
it->second.at(0).getVar(mesh2d.edge_nodes);
const auto var = it->second.at(0);
var.getVar(mesh2d.edge_nodes);
apply_start_index_offset(var, mesh2d.start_index, mesh2d.num_edges, mesh2d.edge_nodes);
}
if (auto const it = m_topology_attribute_variables.find("edge_face_connectivity"); mesh2d.edge_faces != nullptr && it != m_topology_attribute_variables.end())
{
Expand All @@ -313,15 +315,21 @@ void Mesh2D::get(ugridapi::Mesh2D& mesh2d) const
// Faces
if (auto const it = m_topology_attribute_variables.find("face_node_connectivity"); mesh2d.face_nodes != nullptr && it != m_topology_attribute_variables.end())
{
it->second.at(0).getVar(mesh2d.face_nodes);
const auto var = it->second.at(0);
var.getVar(mesh2d.face_nodes);
apply_start_index_offset(var, mesh2d.start_index, mesh2d.num_faces * mesh2d.num_face_nodes_max, mesh2d.face_nodes);
}
if (auto const it = m_topology_attribute_variables.find("face_edge_connectivity"); mesh2d.face_edges != nullptr && it != m_topology_attribute_variables.end())
{
it->second.at(0).getVar(mesh2d.face_edges);
const auto var = it->second.at(0);
var.getVar(mesh2d.face_edges);
apply_start_index_offset(var, mesh2d.start_index, mesh2d.num_faces * mesh2d.num_face_nodes_max, mesh2d.face_edges);
}
if (auto const it = m_topology_attribute_variables.find("face_face_connectivity"); mesh2d.face_faces != nullptr && it != m_topology_attribute_variables.end())
{
it->second.at(0).getVar(mesh2d.face_faces);
const auto var = it->second.at(0);
var.getVar(mesh2d.face_faces);
apply_start_index_offset(var, mesh2d.start_index, mesh2d.num_faces * mesh2d.num_face_nodes_max, mesh2d.face_faces);
}
if (auto const it = m_topology_attribute_variables.find("face_coordinates"); mesh2d.face_x != nullptr && it != m_topology_attribute_variables.end())
{
Expand Down
1 change: 1 addition & 0 deletions tests/api/ApiTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ TEST(ApiTest, InquireAndGet_OneMesh2D_ShouldReadMesh2d)
mesh2d.face_y = face_y.data();
std::vector<int> face_nodes(mesh2d.num_faces * mesh2d.num_face_nodes_max);
mesh2d.face_nodes = face_nodes.data();
mesh2d.start_index = 1;

// Execute
error_code = ug_mesh2d_get(file_id, 0, mesh2d);
Expand Down

0 comments on commit cfd304e

Please sign in to comment.