Commit ad392890 authored by Lukas Riedel's avatar Lukas Riedel

Move utility functions into new header 'common/utility.hh'

* Add function 'is_none' for checking if user input is 'none'
* Move functions 'to_lower' and 'get_grid_extensions' to new header
parent c9c4817e
......@@ -4,16 +4,9 @@
#include <memory>
#include <array>
#include <vector>
#include <iostream>
#include <iomanip>
#include <type_traits>
#include <map>
#include <string>
#include <mpi.h>
#include <dune/common/timer.hh>
#include <dune/common/ios_state.hh>
#include <dune/common/fvector.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/parametertree.hh>
......@@ -26,7 +19,6 @@
#include <dune/grid/utility/structuredgridfactory.hh>
#include <dune/dorie/common/grid_mapper.hh>
#include <dune/dorie/common/h5file.hh>
namespace Dune {
namespace Dorie {
......@@ -67,6 +59,12 @@ private:
Map _boundary_index_map;
public:
/// Build the grid and retrieve mapping information
/**
* \param config User config settings
* \param helper Instance of the MPI helper
*/
explicit GridCreator (const Dune::ParameterTree& config,
const Dune::MPIHelper& helper):
_helper(helper),
......@@ -186,7 +184,6 @@ private:
std::shared_ptr<Grid>
create_rect_grid () const
{
Dune::Timer timer;
const auto level = _config.get<int>("grid.initialLevel");
const auto upperRight = _config.get<Dune::FieldVector<double, _dim>>("grid.extensions");
const auto elements = _config.get<std::array<unsigned int, _dim>>("grid.cells");
......
......@@ -6,7 +6,6 @@
#include <numeric>
#include <memory>
#include <map>
#include <optional>
#include <dune/common/fvector.hh>
#include <dune/common/parametertree.hh>
......@@ -15,6 +14,7 @@
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/h5file.hh>
#include <dune/dorie/common/utility.hh>
namespace Dune {
namespace Dorie {
......@@ -29,52 +29,6 @@ enum class GridMode {
rectangular
};
/// Retrieve the grid extensions as a position vector
/** Iterate over all vertices of the coarsest level grid view, determine the
* locally largest extensions and compute the maximum across all processes.
*
* \param grid Shared pointer to the grid
* \note This assumes the grid has rectangular shape and its origin
* is the origin of Euklidian space.
* \ingroup Grid
*/
template<typename Grid>
auto get_grid_extensions (const std::shared_ptr<Grid> grid)
-> Dune::FieldVector<typename Grid::ctype, Grid::dimension>
{
Dune::FieldVector<typename Grid::ctype, Grid::dimension> ret;
std::fill(ret.begin(), ret.end(), 0.0);
// iterate over all grid vertices
auto gv = grid->levelGridView(0);
for (auto&& vertex : vertices(gv)) {
const auto pos = vertex.geometry().center();
if (pos.two_norm() > ret.two_norm()) {
ret = pos;
}
}
// compute the maximum of all processes
auto& comm = grid->comm();
comm.max(&ret[0], ret.size());
// return the maximum vector
return ret;
}
/// Turn a string into lowercase characters
/** \param in Input string
* \return Lowercase output string
*/
inline std::string to_lower (const std::string& in)
{
std::string out = in;
std::transform(in.begin(), in.end(), out.begin(),
[](unsigned char c){ return std::tolower(c); }
);
return out;
}
/// Base class for mappers for assembling local element and boundary index maps
/** This class provides common members and methods for retrieving the maps,
* and broadcasting data to all processes.
......@@ -403,12 +357,12 @@ public:
// load global mapping (on every process)
const auto file_path = _config.get<std::string>("grid.mappingFile", "");
if (to_lower(file_path) != "none" and not file_path.empty()){
H5File mapping_file(file_path);
element_index_map_global = read_medium_mapping(mapping_file);
if (is_none(file_path)) {
element_index_map_global = read_medium_global_index();
}
else {
element_index_map_global = read_medium_global_index();
H5File mapping_file(file_path);
element_index_map_global = read_medium_mapping(mapping_file);
}
if (this->_gv.comm().size() > 1)
......
#ifndef DUNE_DORIE_COMMON_UTILITY_HH
#define DUNE_DORIE_COMMON_UTILITY_HH
#include <string>
#include <algorithm>
#include <cctype>
#include <memory>
#include <dune/common/fvector.hh>
#include <dune/grid/common/gridview.hh>
namespace Dune {
namespace Dorie {
/// Retrieve the grid extensions as a position vector
/** Iterate over all vertices of the coarsest level grid view, determine the
* locally largest extensions and compute the maximum across all processes.
*
* \param grid Shared pointer to the grid
* \note This assumes the grid has rectangular shape and its origin
* is the origin of Euklidian space.
* \ingroup Grid
*/
template<typename Grid>
auto get_grid_extensions (const std::shared_ptr<Grid> grid)
-> Dune::FieldVector<typename Grid::ctype, Grid::dimension>
{
Dune::FieldVector<typename Grid::ctype, Grid::dimension> ret;
std::fill(ret.begin(), ret.end(), 0.0);
// iterate over all grid vertices
auto gv = grid->levelGridView(0);
for (auto&& vertex : vertices(gv)) {
const auto pos = vertex.geometry().center();
if (pos.two_norm() > ret.two_norm()) {
ret = pos;
}
}
// compute the maximum of all processes
auto& comm = grid->comm();
comm.max(&ret[0], ret.size());
// return the maximum vector
return ret;
}
/// Turn a string into lowercase characters
/** \param in Input string
* \return Lowercase output string
*/
inline std::string to_lower (const std::string& in)
{
std::string out = in;
std::transform(in.begin(), in.end(), out.begin(),
[](unsigned char c){ return std::tolower(c); }
);
return out;
}
/// Check if a string read as value from a file is none
/** Check if the input string is empty or its lowercase version is equal to
* `none`.
* \param in Input string to check
* \return True if the string is `none` or empty
*/
inline bool is_none (const std::string& in)
{
if (in.empty() or to_lower(in) == "none")
return true;
return false;
}
} // namespace Dorie
} // namespace Dune
#endif // DUNE_DORIE_COMMON_UTILITY_HH
......@@ -9,6 +9,7 @@
#include <yaml-cpp/yaml.h>
#include <dune/dorie/common/utility.hh>
#include <dune/dorie/common/logging.hh>
#include <dune/dorie/common/interpolator.hh>
#include <dune/dorie/common/h5file.hh>
......@@ -251,19 +252,6 @@ struct ScalingAdapterFactory
private:
using RF = typename Traits::RF;
/// Turn a string into lowercase characters
/** \param in Input string
* \return Lowercase output string
*/
static std::string to_lower (const std::string& in)
{
std::string out = in;
std::transform(in.begin(), in.end(), out.begin(),
[](unsigned char c){ return std::tolower(c); }
);
return out;
}
public:
/// Create a scaling adapter using a YAML config node
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment