v0.13.2
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
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
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 89 of file ScalingMethod.cpp.

89{ 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::PlasticityTimeScale.

Examples
plastic.cpp.

Definition at line 119 of file ScalingMethod.cpp.

119{ 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 91 of file ScalingMethod.cpp.

91 {
92 if (tSeries.empty())
93 return 0.;
94
95 auto it = tSeries.find(time);
96 if (it != tSeries.end()) {
97 return it->second;
98 } else {
99 auto it = tSeries.lower_bound(time);
100 if (it == tSeries.end()) {
101 return (--it)->second;
102 }
103 if (it == tSeries.begin()) {
104 return it->second;
105 }
106 auto upper = *(it);
107 it--;
108 if (it == tSeries.end()) {
109 return upper.second;
110 }
111 auto lower = *it;
112 double t = (time - lower.first) / (upper.first - lower.first);
113 double scale1 = upper.second;
114 double scale0 = lower.second;
115 return scale0 + t * (scale1 - scale0);
116 }
117}
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 PetscBool arg_found = PETSC_FALSE;
34 char time_file_name[255] = {'\0'};
35 CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, fileNameFlag.c_str(),
36 time_file_name, 255, &arg_found);
37 if (arg_found) {
38 fileName = std::string(time_file_name);
39 }
40 if (!arg_found && fileName.empty() && errorIfFileNotGiven) {
41 SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
42 "*** ERROR %s (time_data FILE NEEDED)", fileName.c_str());
43 } else if (!arg_found && fileName.empty() && !errorIfFileNotGiven) {
44 MOFEM_LOG_C("WORLD", Sev::warning,
45 "The %s file not provided. Loading scaled with time.",
46 fileName.c_str());
47 scalingMethod = [this](double time) { return this->getLinearScale(time); };
49 }
50
51 std::ifstream in_file_stream(fileName);
52 if (!in_file_stream.is_open() && errorIfFileNotGiven) {
53 SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
54 "*** ERROR data file < %s > open unsuccessful", fileName.c_str());
55 } else if (!in_file_stream.is_open() && !errorIfFileNotGiven) {
56 MOFEM_LOG("WORLD", Sev::warning)
57 << "*** Warning data file " << fileName
58 << " open unsuccessful. Using linear time scaling.";
59 scalingMethod = [this](double time) { return this->getLinearScale(time); };
60 }
61 in_file_stream.seekg(0);
62 std::string line;
63 double time = 0.0, value = 0.0;
64 tSeries[time] = value;
65
66 std::regex rgx(delimiter.c_str());
67 std::sregex_token_iterator end;
68 while (std::getline(in_file_stream, line)) {
69 std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
70 auto time_str = iter;
71 auto value_str = ++iter;
72 if (time_str == end || value_str == end) {
73 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
74 "*** ERROR read data file error (check input time data file) ");
75 }
76 time = std::stod(time_str->str());
77 value = std::stod(value_str->str());
78 tSeries[time] = value;
79 }
80 in_file_stream.close();
81 scalingMethod = [this](double time) { return this->getScaleFromData(time); };
82 if (in_file_stream.is_open()) {
83 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
84 "*** ERROR file close unsuccessful");
85 }
87}
#define MOFEM_LOG_C(channel, severity, format,...)
Definition: LogManager.hpp:304
#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:301
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: