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

KISS-111, add api for writing double variables with mesh locations #7

Merged
merged 10 commits into from
Mar 24, 2022
1 change: 0 additions & 1 deletion include/UGrid/Operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ namespace ugrid
if (dimensions.find(attribute_value_string) == dimensions.end())
{
return false;
std::string attribute_value_string;
}
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions include/UGrid/UGridEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ namespace ugrid
return m_entity_name;
}

/// @brief Gets a vector of nc variables
/// @param attribute_name The attribute name
/// @return The vector of nc variables
std::vector<netCDF::NcVar> get_topology_attribute_variable(const std::string& attribute_name)
{
return m_topology_attribute_variables.at(attribute_name);
}

/// @brief Gets the names of all data variables associated with a specific location
/// @param location_string The location string (e.g. node, edge or face)
/// @return The variables names
Expand Down
42 changes: 42 additions & 0 deletions include/UGridApi/UGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ namespace ugridapi
Exception = 1,
};

/// @brief MeshLocations locations
enum class MeshLocations
{
Faces = 0, ///< Faces
Nodes = 1, ///< Nodes
Edges = 2, ///< Edges
};

static std::unordered_map<MeshLocations, std::string> locations_attribute_names{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need Doxygen documentation here?

{MeshLocations::Faces, "face"},
{MeshLocations::Nodes, "node"},
{MeshLocations::Edges, "edge"},

};

/// @brief Gets pointer to the exception message.
/// @param[out] error_message The pointer to the latest error message
/// @returns Error code
Expand Down Expand Up @@ -141,6 +156,15 @@ namespace ugridapi
/// @return Error code
UGRID_API int ug_topology_get_data_variables_names(int file_id, int topology_type, int topology_id, int location, char* data_variables_names_result);

/// @brief Get the names of data variables for a specific topology on a specific location
/// @param file_id [in] The file id
/// @param topology_type [in] The topology type
/// @param topology_id [in] The topology id
/// @param location [in] The location on the topology (e.g. node, edge or face)
/// @param variable_name [in] The variable name
/// @return Error code
UGRID_API int ug_topology_define_double_variable_on_location(int file_id, int topology_type, int topology_id, int location, char const* variable_name);

/// @brief Get the number of attributes of a specific variable
/// @param file_id [in] The file id
/// @param variable_name [in] The variable name
Expand Down Expand Up @@ -308,6 +332,9 @@ namespace ugridapi
/// @return Error code
UGRID_API int ug_mesh2d_get(int file_id, int topology_id, Mesh2D& mesh2d_api);

/// TODO: DOCUMENT ME
UGRID_API int ug_mesh2d_variable_double_on_location_define(int file_id, int topology_id, int location, char const* variable_name, char const* att_name, int const* attribute_values, int num_values);

/// @brief Defines a new contact topology
/// @param file_id [in] The file id
/// @param contacts_api [in] The structure containing the contact data
Expand Down Expand Up @@ -377,6 +404,21 @@ namespace ugridapi
/// @return Error code
UGRID_API int ug_attribute_global_char_define(int file_id, char const* att_name, char const* attribute_values, int num_values);

/// @brief Gets an int indicating the edge location type
/// @param[out] type The int indicating the edge location type
/// @returns Error code
UGRID_API int ug_get_edges_location_type(int& type);

/// @brief Gets an int indicating the node location type
/// @param[out] type The int indicating the node location type
/// @returns Error code
UGRID_API int ug_get_nodes_location_type(int& type);

/// @brief Gets an int indicating the faces location type
/// @param[out] type The int indicating the face location type
/// @returns Error code
UGRID_API int ug_get_faces_location_type(int& type);

#ifdef __cplusplus
}
#endif
Expand Down
80 changes: 80 additions & 0 deletions src/UGridApi/UGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@ namespace ugridapi
return it->second;
}

static auto get_coordinate_variable_string(int file_id, int topology_id, int topology_type, std::string const& var_name)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string const& var_name ?
or
const std::string& var_name

{
auto topology = get_topology(file_id, topology_id, topology_type);

auto coordinates_vector_variables = topology->get_topology_attribute_variable(var_name);
std::string result;
for (auto i = 0; i < coordinates_vector_variables.size() - 1; ++i)
{
result += coordinates_vector_variables[i].getName() + " ";
}
result += coordinates_vector_variables.back().getName();
return result;
}

UGRID_API int ug_error_get(const char*& error_message)
{
error_message = exceptionMessage;
Expand Down Expand Up @@ -296,6 +310,56 @@ namespace ugridapi
return exit_code;
}

UGRID_API int ug_topology_define_double_variable_on_location(int file_id, int topology_id, int topology_type, int mesh_location, char const* variable_name)
{
int exit_code = Success;
try
{
if (ugrid_states.count(file_id) == 0)
{
throw std::invalid_argument("UGrid: The selected file_id does not exist.");
}

const auto name = ugrid::char_array_to_string(variable_name, ugrid::name_long_length);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable name does not seem to be used. Can it be removed, or should line 326 use name instead of variable_name, or somehing else?


// Get the variable
auto const local_variable_name = ugrid::char_array_to_string(variable_name, ugrid::name_long_length);

auto variable = ugrid_states[file_id].m_ncFile->addVar(local_variable_name, netCDF::NcType::nc_DOUBLE);

auto topology = get_topology(file_id, topology_id, topology_type);

auto const mesh = topology->get_name();

const auto mesh_location_enum = static_cast<MeshLocations>(mesh_location);
auto location = std::string();
auto coordinates = std::string();
if (mesh_location_enum == MeshLocations::Faces)
{
location = "faces";
coordinates = get_coordinate_variable_string(file_id, topology_id, Mesh2dTopology, "face_coordinates");
}
if (mesh_location_enum == MeshLocations::Nodes)
{
coordinates = get_coordinate_variable_string(file_id, topology_id, Mesh2dTopology, "node_coordinates");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a:
location = "nodes";
here?

}
if (mesh_location_enum == MeshLocations::Edges)
{
location = "edges";
coordinates = get_coordinate_variable_string(file_id, topology_id, Mesh2dTopology, "edge_coordinates");
}

variable.putAtt("mesh", netCDF::NcType::nc_CHAR, mesh.size(), mesh.c_str());
variable.putAtt("location", netCDF::NcType::nc_CHAR, location.size(), location.c_str());
variable.putAtt("coordinates", netCDF::NcType::nc_CHAR, coordinates.size(), coordinates.c_str());
}
catch (...)
{
exit_code = HandleExceptions(std::current_exception());
}
return exit_code;
}

UGRID_API int ug_variable_count_attributes(int file_id, char const* variable_name, int& attributes_count)
{
int exit_code = Success;
Expand Down Expand Up @@ -1032,4 +1096,20 @@ namespace ugridapi
return exit_code;
}

UGRID_API int ug_get_edges_location_type(int& type)
{
type = static_cast<int>(MeshLocations::Edges);
return Success;
}
UGRID_API int ug_get_nodes_location_type(int& type)
{
type = static_cast<int>(MeshLocations::Nodes);
return Success;
}
UGRID_API int ug_get_faces_location_type(int& type)
{
type = static_cast<int>(MeshLocations::Faces);
return Success;
}

} // namespace ugridapi