v0.14.0
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
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 Member Functions

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

Private Member Functions

MoFEMErrorCode timeData (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. More...
 
double getLinearScale (const double time)
 Returns the value of time. More...
 

Private Attributes

std::map< double, doubletSeries
 
std::string fileName = ""
 
std::string fileNameFlag = "-time_scalar_file"
 
bool errorIfFileNotGiven
 
std::function< double(double)> scalingMethod
 

Static Private Attributes

static const std::string defaultDelimiter
 comma or space More...
 

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.

Constructor & Destructor Documentation

◆ TimeScale() [1/2]

MoFEM::TimeScale::TimeScale ( std::string  file_name = "",
bool  error_if_file_not_given = false 
)

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.

23 : TimeScale(file_name, defaultDelimiter, error_if_file_not_given) {}
TimeScale(std::string file_name="", bool error_if_file_not_given=false)
TimeScale constructor.
static const std::string defaultDelimiter
comma or space

◆ TimeScale() [2/2]

MoFEM::TimeScale::TimeScale ( std::string  file_name,
std::string  delimiter,
bool  error_if_file_not_given = false 
)

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 25 of file ScalingMethod.cpp.

27 : fileName(file_name), errorIfFileNotGiven(error_if_file_not_given) {
28 CHK_THROW_MESSAGE(timeData(delimiter), "Error in reading time data");
29}
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
Definition: definitions.h:595
std::string fileName
MoFEMErrorCode timeData(std::string delimiter)

Member Function Documentation

◆ getLinearScale()

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

Returns the value of time.

Returns
double

Definition at line 92 of file ScalingMethod.cpp.

92{ return time; }

◆ getScale()

double MoFEM::TimeScale::getScale ( const double  time)
virtual

Get scaling at a given time.

Parameters
time
Returns
double

Reimplemented from MoFEM::ScalingMethod.

Reimplemented in Example::ScaledTimeScale, Contact::ScaledTimeScale, LinMomTimeScale, Example::DynamicFirstOrderConsSinusTimeScale, and Example::DynamicFirstOrderConsConstantTimeScale.

Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 122 of file ScalingMethod.cpp.

122{ return scalingMethod(time); }
std::function< double(double)> scalingMethod

◆ getScaleFromData()

double MoFEM::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 94 of file ScalingMethod.cpp.

94 {
95 if (tSeries.empty())
96 return 0.;
97
98 auto it = tSeries.find(time);
99 if (it != tSeries.end()) {
100 return it->second;
101 } else {
102 auto it = tSeries.lower_bound(time);
103 if (it == tSeries.end()) {
104 return (--it)->second;
105 }
106 if (it == tSeries.begin()) {
107 return it->second;
108 }
109 auto upper = *(it);
110 it--;
111 if (it == tSeries.end()) {
112 return upper.second;
113 }
114 auto lower = *it;
115 double t = (time - lower.first) / (upper.first - lower.first);
116 double scale1 = upper.second;
117 double scale0 = lower.second;
118 return scale0 + t * (scale1 - scale0);
119 }
120}
constexpr double t
plate stiffness
Definition: plate.cpp:59
std::map< double, double > tSeries

◆ timeData()

MoFEMErrorCode MoFEM::TimeScale::timeData ( std::string  delimiter)
private

Definition at line 31 of file ScalingMethod.cpp.

31 {
33 MOFEM_LOG_CHANNEL("WORLD");
34 PetscBool arg_found = PETSC_FALSE;
35 char time_file_name[255] = {'\0'};
36 CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, fileNameFlag.c_str(),
37 time_file_name, 255, &arg_found);
38 if (arg_found) {
39 fileName = std::string(time_file_name);
40 }
41 if (!arg_found && fileName.empty() && errorIfFileNotGiven) {
42 SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
43 "*** ERROR %s (time_data FILE NEEDED)", fileName.c_str());
44 } else if (!arg_found && fileName.empty() && !errorIfFileNotGiven) {
45 MOFEM_LOG_C("WORLD", Sev::warning,
46 "The %s file not provided. Loading scaled with time.",
47 fileName.c_str());
48 scalingMethod = [this](double time) { return this->getLinearScale(time); };
50 }
51
52 std::ifstream in_file_stream(fileName);
53 if (!in_file_stream.is_open() && errorIfFileNotGiven) {
54 SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
55 "*** ERROR data file < %s > open unsuccessful", fileName.c_str());
56 } else if (!in_file_stream.is_open() && !errorIfFileNotGiven) {
57 MOFEM_LOG("WORLD", Sev::warning)
58 << "*** Warning data file " << fileName
59 << " open unsuccessful. Using linear time scaling.";
60 scalingMethod = [this](double time) { return this->getLinearScale(time); };
62 }
63
64 in_file_stream.seekg(0);
65 std::string line;
66 double time = 0.0, value = 0.0;
67 tSeries[time] = value;
68
69 std::regex rgx(delimiter.c_str());
70 std::sregex_token_iterator end;
71 while (std::getline(in_file_stream, line)) {
72 std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
73 auto time_str = iter;
74 auto value_str = ++iter;
75 if (time_str == end || value_str == end) {
76 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
77 "*** ERROR read data file error (check input time data file) ");
78 }
79 time = std::stod(time_str->str());
80 value = std::stod(value_str->str());
81 tSeries[time] = value;
82 }
83 in_file_stream.close();
84 scalingMethod = [this](double time) { return this->getScaleFromData(time); };
85 if (in_file_stream.is_open()) {
86 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
87 "*** ERROR file close unsuccessful");
88 }
90}
#define MOFEM_LOG_C(channel, severity, format,...)
Definition: LogManager.hpp:311
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:447
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
#define CHKERR
Inline error check.
Definition: definitions.h:535
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
Definition: LogManager.hpp:284
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
double getLinearScale(const double time)
Returns the value of time.
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

◆ defaultDelimiter

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

comma or space

Definition at line 87 of file ScalingMethod.hpp.

◆ errorIfFileNotGiven

bool MoFEM::TimeScale::errorIfFileNotGiven
private

Definition at line 89 of file ScalingMethod.hpp.

◆ fileName

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

Definition at line 83 of file ScalingMethod.hpp.

◆ fileNameFlag

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

Definition at line 84 of file ScalingMethod.hpp.

◆ scalingMethod

std::function<double(double)> MoFEM::TimeScale::scalingMethod
private
Initial value:
= [](double time) {
return time;
}

Definition at line 90 of file ScalingMethod.hpp.

◆ tSeries

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

Definition at line 82 of file ScalingMethod.hpp.


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