v0.15.0
Loading...
Searching...
No Matches
MoFEM::TimeScale Struct Reference

Force scale operator for reading two columns. More...

#include "src/boundary_conditions/ScalingMethod.hpp"

Inheritance diagram for MoFEM::TimeScale:
[legend]
Collaboration diagram for MoFEM::TimeScale:
[legend]

Public Types

using ScalingFun = std::function<double(double)>
 

Public Member Functions

 TimeScale (std::string file_name="", bool error_if_file_not_given=false, ScalingFun def_scaling_fun=[](double time) { return time;})
 TimeScale constructor.
 
 TimeScale (std::string file_name, std::string delimiter, bool error_if_file_not_given=false, ScalingFun def_scaling_fun=[](double time) { return time;})
 TimeScale constructor.
 
double getScale (const double time)
 Get scaling at a given time.
 
- Public Member Functions inherited from MoFEM::ScalingMethod
 ScalingMethod ()=default
 
virtual ~ScalingMethod ()=default
 

Public Attributes

std::string fileName = ""
 
PetscBool argFileScale = PETSC_FALSE
 

Private Member Functions

MoFEMErrorCode timeData (std::string fileName, std::string delimiter)
 
double getScaleFromData (const double time)
 Get scaling at a given time when the scalar values have been provided. Uses linear interpolation on the nearest time range to calculate scaling if the provided time is not present in the data.
 
double getLinearScale (const double time)
 Returns the value of time.
 

Private Attributes

std::map< double, doubletSeries
 
std::string fileNameFlag = "-time_scalar_file"
 
bool errorIfFileNotGiven
 
ScalingFun defScalingMethod = [](double time) { return time; }
 
ScalingFun scalingMethod = defScalingMethod
 

Static Private Attributes

static const std::string defaultDelimiter
 comma or space
 

Detailed Description

Force scale operator for reading two columns.

Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 32 of file ScalingMethod.hpp.

Member Typedef Documentation

◆ ScalingFun

using MoFEM::TimeScale::ScalingFun = std::function<double(double)>

Definition at line 34 of file ScalingMethod.hpp.

Constructor & Destructor Documentation

◆ TimeScale() [1/2]

TimeScale::TimeScale ( std::string file_name = "",
bool error_if_file_not_given = false,
ScalingFun def_scaling_fun = [](double time) { return time; } )

TimeScale constructor.

Parameters
file_namePath to input CSV data file
error_if_file_not_givenIf file name is not provided, the constructor will throw an error if this flag is set to true or throw a warning and use linear scaling if this flag is set to false
Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 22 of file ScalingMethod.cpp.

24 : TimeScale(file_name, defaultDelimiter, error_if_file_not_given,
25 def_scaling_fun) {}
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

◆ TimeScale() [2/2]

TimeScale::TimeScale ( std::string file_name,
std::string delimiter,
bool error_if_file_not_given = false,
ScalingFun def_scaling_fun = [](double time) { return time; } )

TimeScale constructor.

Parameters
file_namePath to input CSV data file
delimiterCharacter which is used to separate the data in a csv row, by default it is ','
error_if_file_not_givenIf file name is not provided, the constructor will throw an error if this flag is set to true or throw a warning and use linear scaling if this flag is set to false

Definition at line 27 of file ScalingMethod.cpp.

29 : fileName(file_name), errorIfFileNotGiven(error_if_file_not_given),
30 defScalingMethod(def_scaling_fun) {
31 CHK_THROW_MESSAGE(timeData(file_name, delimiter),
32 "Error in reading time data");
33}
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
std::string fileName
ScalingFun defScalingMethod
MoFEMErrorCode timeData(std::string fileName, std::string delimiter)

Member Function Documentation

◆ getLinearScale()

double MoFEM::TimeScale::getLinearScale ( const double time)
private

Returns the value of time.

Returns
double

◆ getScale()

double TimeScale::getScale ( const double time)
virtual

Get scaling at a given time.

Parameters
time
Returns
double

Reimplemented from MoFEM::ScalingMethod.

Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 137 of file ScalingMethod.cpp.

137{ return scalingMethod(time); }
ScalingFun scalingMethod

◆ getScaleFromData()

double TimeScale::getScaleFromData ( const double time)
private

Get scaling at a given time when the scalar values have been provided. Uses linear interpolation on the nearest time range to calculate scaling if the provided time is not present in the data.

Returns
double

Definition at line 109 of file ScalingMethod.cpp.

109 {
110 if (tSeries.empty())
111 return 0.;
112
113 auto it = tSeries.find(time);
114 if (it != tSeries.end()) {
115 return it->second;
116 } else {
117 auto it = tSeries.lower_bound(time);
118 if (it == tSeries.end()) {
119 return (--it)->second;
120 }
121 if (it == tSeries.begin()) {
122 return it->second;
123 }
124 auto upper = *(it);
125 it--;
126 if (it == tSeries.end()) {
127 return upper.second;
128 }
129 auto lower = *it;
130 double t = (time - lower.first) / (upper.first - lower.first);
131 double scale1 = upper.second;
132 double scale0 = lower.second;
133 return scale0 + t * (scale1 - scale0);
134 }
135}
constexpr double t
plate stiffness
Definition plate.cpp:58
std::map< double, double > tSeries

◆ timeData()

MoFEMErrorCode TimeScale::timeData ( std::string fileName,
std::string delimiter )
private

Definition at line 35 of file ScalingMethod.cpp.

36 {
38 MOFEM_LOG_CHANNEL("WORLD");
39 // Set the argument found flag to false as default
40 char time_file_name[255] = {'\0'};
41 // Check to see if a filename has been provided
42 if (fileName.empty()) {
43 // If no filename, look for command line argument
44 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, fileNameFlag.c_str(),
45 time_file_name, 255, &argFileScale);
46 if (argFileScale) {
47 fileName = std::string(time_file_name);
48 }
49 } else {
50 // Set the command line flag to true for correct flow control using provided
51 // filename
52 argFileScale = PETSC_TRUE;
53 }
54 if (!argFileScale && fileName.empty() && errorIfFileNotGiven) {
55 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
56 "*** ERROR %s (time_data FILE NEEDED)", fileName.c_str());
57 } else if (!argFileScale && fileName.empty() && !errorIfFileNotGiven) {
58 MOFEM_LOG_C("WORLD", Sev::warning,
59 "The %s file not provided. Loading scaled with time.",
60 fileName.c_str());
61 scalingMethod = [this](double time) {
62 return this->defScalingMethod(time);
63 };
65 }
66
67 std::ifstream in_file_stream(fileName);
68 if (!in_file_stream.is_open() && errorIfFileNotGiven) {
69 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
70 "*** ERROR data file < %s > open unsuccessful", fileName.c_str());
71 } else if (!in_file_stream.is_open() && !errorIfFileNotGiven) {
72 MOFEM_LOG("WORLD", Sev::warning)
73 << "*** Warning data file " << fileName
74 << " open unsuccessful. Using default time scaling.";
75 scalingMethod = [this](double time) {
76 return this->defScalingMethod(time);
77 };
79 }
80
81 in_file_stream.seekg(0);
82 std::string line;
83 double time = 0.0, value = 0.0;
84 tSeries[time] = value;
85
86 std::regex rgx(delimiter.c_str());
87 std::sregex_token_iterator end;
88 while (std::getline(in_file_stream, line)) {
89 std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
90 auto time_str = iter;
91 auto value_str = ++iter;
92 if (time_str == end || value_str == end) {
93 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
94 "*** ERROR read data file error (check input time data file) ");
95 }
96 time = std::stod(time_str->str());
97 value = std::stod(value_str->str());
98 tSeries[time] = value;
99 }
100 in_file_stream.close();
101 scalingMethod = [this](double time) { return this->getScaleFromData(time); };
102 if (in_file_stream.is_open()) {
103 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
104 "*** ERROR file close unsuccessful");
105 }
107}
#define MOFEM_LOG_C(channel, severity, format,...)
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MOFEM_LOG(channel, severity)
Log.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
double getScaleFromData(const double time)
Get scaling at a given time when the scalar values have been provided. Uses linear interpolation on t...
std::string fileNameFlag

Member Data Documentation

◆ argFileScale

PetscBool MoFEM::TimeScale::argFileScale = PETSC_FALSE

Definition at line 72 of file ScalingMethod.hpp.

◆ defaultDelimiter

const std::string TimeScale::defaultDelimiter
staticprivate
Initial value:
=
"(\\s*,\\s*|\\s+)"

comma or space

Definition at line 95 of file ScalingMethod.hpp.

◆ defScalingMethod

ScalingFun MoFEM::TimeScale::defScalingMethod = [](double time) { return time; }
private

Definition at line 98 of file ScalingMethod.hpp.

98{ return time; };

◆ errorIfFileNotGiven

bool MoFEM::TimeScale::errorIfFileNotGiven
private

Definition at line 97 of file ScalingMethod.hpp.

◆ fileName

std::string MoFEM::TimeScale::fileName = ""

Definition at line 71 of file ScalingMethod.hpp.

◆ fileNameFlag

std::string MoFEM::TimeScale::fileNameFlag = "-time_scalar_file"
private

Definition at line 92 of file ScalingMethod.hpp.

◆ scalingMethod

ScalingFun MoFEM::TimeScale::scalingMethod = defScalingMethod
private

Definition at line 99 of file ScalingMethod.hpp.

◆ tSeries

std::map<double, double> MoFEM::TimeScale::tSeries
private

Definition at line 91 of file ScalingMethod.hpp.


The documentation for this struct was generated from the following files: