v0.14.0
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
 ScalingMethod ()=default
 
virtual ~ScalingMethod ()=default
 

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. 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() [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(file_name, delimiter),
29  "Error in reading time data");
30 }

Member Function Documentation

◆ getLinearScale()

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

Returns the value of time.

Returns
double

Definition at line 103 of file ScalingMethod.cpp.

103 { 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::DynamicFirstOrderConsConstantTimeScale, Example::DynamicFirstOrderConsSinusTimeScale, LinMomTimeScale, Example::ScaledTimeScale, Example::ScaledTimeScale, and Contact::ScaledTimeScale.

Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 133 of file ScalingMethod.cpp.

133 { return scalingMethod(time); }

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

105  {
106  if (tSeries.empty())
107  return 0.;
108 
109  auto it = tSeries.find(time);
110  if (it != tSeries.end()) {
111  return it->second;
112  } else {
113  auto it = tSeries.lower_bound(time);
114  if (it == tSeries.end()) {
115  return (--it)->second;
116  }
117  if (it == tSeries.begin()) {
118  return it->second;
119  }
120  auto upper = *(it);
121  it--;
122  if (it == tSeries.end()) {
123  return upper.second;
124  }
125  auto lower = *it;
126  double t = (time - lower.first) / (upper.first - lower.first);
127  double scale1 = upper.second;
128  double scale0 = lower.second;
129  return scale0 + t * (scale1 - scale0);
130  }
131 }

◆ timeData()

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

Definition at line 32 of file ScalingMethod.cpp.

33  {
35  MOFEM_LOG_CHANNEL("WORLD");
36  // Set the argument found flag to false as default
37  PetscBool arg_found = PETSC_FALSE;
38  char time_file_name[255] = {'\0'};
39  // Check to see if a filename has been provided
40  if (fileName.empty()) {
41  // If no filename, look for command line argument
42  CHKERR PetscOptionsGetString(PETSC_NULL, PETSC_NULL, fileNameFlag.c_str(),
43  time_file_name, 255, &arg_found);
44  if (arg_found) {
45  fileName = std::string(time_file_name);
46  }
47  } else {
48  // Set the command line flag to true for correct flow control using provided
49  // filename
50  arg_found = PETSC_TRUE;
51  }
52  if (!arg_found && fileName.empty() && errorIfFileNotGiven) {
53  SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
54  "*** ERROR %s (time_data FILE NEEDED)", fileName.c_str());
55  } else if (!arg_found && fileName.empty() && !errorIfFileNotGiven) {
56  MOFEM_LOG_C("WORLD", Sev::warning,
57  "The %s file not provided. Loading scaled with time.",
58  fileName.c_str());
59  scalingMethod = [this](double time) { return this->getLinearScale(time); };
61  }
62 
63  std::ifstream in_file_stream(fileName);
64  if (!in_file_stream.is_open() && errorIfFileNotGiven) {
65  SETERRQ1(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
66  "*** ERROR data file < %s > open unsuccessful", fileName.c_str());
67  } else if (!in_file_stream.is_open() && !errorIfFileNotGiven) {
68  MOFEM_LOG("WORLD", Sev::warning)
69  << "*** Warning data file " << fileName
70  << " open unsuccessful. Using linear time scaling.";
71  scalingMethod = [this](double time) { return this->getLinearScale(time); };
73  }
74 
75  in_file_stream.seekg(0);
76  std::string line;
77  double time = 0.0, value = 0.0;
78  tSeries[time] = value;
79 
80  std::regex rgx(delimiter.c_str());
81  std::sregex_token_iterator end;
82  while (std::getline(in_file_stream, line)) {
83  std::sregex_token_iterator iter(line.begin(), line.end(), rgx, -1);
84  auto time_str = iter;
85  auto value_str = ++iter;
86  if (time_str == end || value_str == end) {
87  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
88  "*** ERROR read data file error (check input time data file) ");
89  }
90  time = std::stod(time_str->str());
91  value = std::stod(value_str->str());
92  tSeries[time] = value;
93  }
94  in_file_stream.close();
95  scalingMethod = [this](double time) { return this->getScaleFromData(time); };
96  if (in_file_stream.is_open()) {
97  SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
98  "*** ERROR file close unsuccessful");
99  }
101 }

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:
MoFEMFunctionReturnHot
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:447
MOFEM_LOG_CHANNEL
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
Definition: LogManager.hpp:284
CHK_THROW_MESSAGE
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
Definition: definitions.h:596
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:535
MoFEM::TimeScale::TimeScale
TimeScale(std::string file_name="", bool error_if_file_not_given=false)
TimeScale constructor.
Definition: ScalingMethod.cpp:22
MOFEM_LOG_C
#define MOFEM_LOG_C(channel, severity, format,...)
Definition: LogManager.hpp:311
MoFEM::TimeScale::tSeries
std::map< double, double > tSeries
Definition: ScalingMethod.hpp:82
MoFEM::TimeScale::fileName
std::string fileName
Definition: ScalingMethod.hpp:83
MoFEM::TimeScale::timeData
MoFEMErrorCode timeData(std::string fileName, std::string delimiter)
Definition: ScalingMethod.cpp:32
t
constexpr double t
plate stiffness
Definition: plate.cpp:59
MoFEM::TimeScale::errorIfFileNotGiven
bool errorIfFileNotGiven
Definition: ScalingMethod.hpp:89
MoFEM::TimeScale::defaultDelimiter
static const std::string defaultDelimiter
comma or space
Definition: ScalingMethod.hpp:87
MOFEM_LOG
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
MoFEM::TimeScale::fileNameFlag
std::string fileNameFlag
Definition: ScalingMethod.hpp:84
MOFEM_DATA_INCONSISTENCY
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
MoFEM::PetscOptionsGetString
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
Definition: DeprecatedPetsc.hpp:172
MoFEM::TimeScale::scalingMethod
std::function< double(double)> scalingMethod
Definition: ScalingMethod.hpp:90
MoFEM::TimeScale::getScaleFromData
double getScaleFromData(const double time)
Get scaling at a given time when the scalar values have been provided. Uses linear interpolation on t...
Definition: ScalingMethod.cpp:105
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
MoFEM::TimeScale::getLinearScale
double getLinearScale(const double time)
Returns the value of time.
Definition: ScalingMethod.cpp:103
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346