v0.15.0
Loading...
Searching...
No Matches
ScalingMethod.hpp
Go to the documentation of this file.
1/**
2 * @file ScalingMethod.hpp
3 * @brief
4 * @version 0.13.1
5 * @date 2022-08-12
6 *
7 * @copyright Copyright (c) 2022
8 *
9 */
10
11#ifndef _TIME_SCALING_HPP_
12#define _TIME_SCALING_HPP_
13
14namespace MoFEM {
15
17
18 /**
19 * @brief Get scaling at given time
20 *
21 * @param time
22 * @return double
23 */
24 virtual double getScale(const double time);
25
26 ScalingMethod() = default;
27 virtual ~ScalingMethod() = default;
28};
29
30/** \brief Force scale operator for reading two columns
31 */
32struct TimeScale : public ScalingMethod {
33
34 using ScalingFun = std::function<double(double)>;
35
36 /**
37 * @brief TimeScale constructor
38 *
39 * @param file_name Path to input CSV data file
40 * @param error_if_file_not_given If file name is not provided, the
41 * constructor will throw an error if this flag is set to true or throw a
42 * warning and use linear scaling if this flag is set to false
43 */
45 std::string file_name = "", bool error_if_file_not_given = false,
46 ScalingFun def_scaling_fun = [](double time) { return time; });
47
48 /**
49 * @brief TimeScale constructor
50 *
51 * @param file_name Path to input CSV data file
52 * @param delimiter Character which is used to separate the data in a csv row,
53 * by default it is ','
54 * @param error_if_file_not_given If file name is not provided, the
55 * constructor will throw an error if this flag is set to true or throw a
56 * warning and use linear scaling if this flag is set to false
57 */
59 std::string file_name, std::string delimiter,
60 bool error_if_file_not_given = false,
61 ScalingFun def_scaling_fun = [](double time) { return time; });
62
63 /**
64 * @brief Get scaling at a given time
65 *
66 * @param time
67 * @return double
68 */
69 double getScale(const double time);
70
71 std::string fileName = ""; //< file CSV data file
72 PetscBool argFileScale = PETSC_FALSE; //< get file name from command line
73
74private:
75 MoFEMErrorCode timeData(std::string fileName, std::string delimiter);
76
77 /**
78 * @brief Get scaling at a given time when the scalar values have been
79 * provided. Uses linear interpolation on the nearest time range to calculate
80 * scaling if the provided time is not present in the data.
81 * @return double
82 */
83 double getScaleFromData(const double time);
84
85 /**
86 * @brief Returns the value of time.
87 * @return double
88 */
89 double getLinearScale(const double time);
90
91 std::map<double, double> tSeries;
92 std::string fileNameFlag = "-time_scalar_file";
93
94 static const std::string
95 defaultDelimiter; // "(\\s*,\\s*|\\s+)";
96
98 ScalingFun defScalingMethod = [](double time) { return time; };
100};
101
102/**
103 * @brief get time scaling method
104 *
105 * @tparam TS
106 */
107template <typename TS> struct GetBlockScalingMethod {
108
110
111 template <typename... Args>
112 static boost::shared_ptr<ScalingMethod>
113 get(boost::shared_ptr<ScalingMethod> ts, std::string file_prefix,
114 std::string file_suffix, std::string block_name, Args &&...args) {
115
116 auto get_file_name = [&]() {
117 return file_prefix + "_" + block_name + file_suffix;
118 };
119
120 if (check_file_exists(get_file_name())) {
121 MOFEM_LOG_CHANNEL("WORLD");
122 MOFEM_LOG_TAG("WORLD","GetBlockScalingMethod");
123 MOFEM_LOG("WORLD", Sev::inform)
124 << "Block history file found: " << get_file_name();
125 MOFEM_LOG_CHANNEL("WORLD");
126 return boost::make_shared<TS>(get_file_name(), args...);
127 } else {
128 return ts;
129 }
130 }
131
132private:
133 static auto check_file_exists(std::string &&file_name) {
134 namespace fs = std::filesystem;
135 if (!fs::exists(file_name) || !fs::is_regular_file(file_name)) {
136 return false;
137 }
138 return true;
139 }
140};
141
142/** \brief Force scale operator for reading four columns (time and vector)
143 */
144template <int SPACE_DIM> struct TimeScaleVector : public ScalingMethod {
145
146 TimeScaleVector(std::string name = "-time_vector_file",
147 bool error_if_file_not_given = false);
148
149 TimeScaleVector(std::string name, int ms_id,
150 bool error_if_file_not_given = false);
151
152 virtual FTensor::Tensor1<double, SPACE_DIM> getVector(const double time);
154 getVectorFromData(const double time);
155
156private:
158
159 std::map<double, FTensor::Tensor1<double, 3>> tSeries;
161 string nAme;
163
164 PetscBool fLg;
165 std::function<FTensor::Tensor1<double, SPACE_DIM>(double)> scalingMethod =
166 [this](double time) {
169 s(i) = time;
170 return s;
171 };
172};
175
176} // namespace MoFEM
177
178#endif //_TIME_SCALING_HPP_
constexpr int SPACE_DIM
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
FTensor::Index< 'i', SPACE_DIM > i
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
get time scaling method
static auto check_file_exists(std::string &&file_name)
static boost::shared_ptr< ScalingMethod > get(boost::shared_ptr< ScalingMethod > ts, std::string file_prefix, std::string file_suffix, std::string block_name, Args &&...args)
virtual double getScale(const double time)
Get scaling at given time.
virtual ~ScalingMethod()=default
Force scale operator for reading four columns (time and vector)
std::map< double, FTensor::Tensor1< double, 3 > > tSeries
TimeScaleVector(std::string name="-time_vector_file", bool error_if_file_not_given=false)
std::function< FTensor::Tensor1< double, SPACE_DIM >(double)> scalingMethod
MoFEMErrorCode timeData()
virtual FTensor::Tensor1< double, SPACE_DIM > getVector(const double time)
virtual FTensor::Tensor1< double, SPACE_DIM > getVectorFromData(const double time)
Force scale operator for reading two columns.
std::string fileName
ScalingFun defScalingMethod
double getLinearScale(const double time)
Returns the value of time.
std::function< double(double)> ScalingFun
ScalingFun scalingMethod
double getScaleFromData(const double time)
Get scaling at a given time when the scalar values have been provided. Uses linear interpolation on t...
MoFEMErrorCode timeData(std::string fileName, std::string delimiter)
std::string fileNameFlag
double getScale(const double time)
Get scaling at a given time.
TimeScale(std::string file_name="", bool error_if_file_not_given=false, ScalingFun def_scaling_fun=[](double time) { return time;})
TimeScale constructor.
static const std::string defaultDelimiter
comma or space
std::map< double, double > tSeries