v0.14.0
Public Types | Public Member Functions | Public Attributes | 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 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. More...
 
 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. 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
 

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

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 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.

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]

MoFEM::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() [2/2]

MoFEM::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 }

Member Function Documentation

◆ getLinearScale()

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

Returns the value of time.

Returns
double

◆ 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, and Contact::ScaledTimeScale.

Examples
dynamic_first_order_con_law.cpp, and plastic.cpp.

Definition at line 137 of file ScalingMethod.cpp.

137 { 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 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 }

◆ timeData()

MoFEMErrorCode MoFEM::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_NULL, PETSC_NULL, 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  SETERRQ1(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  SETERRQ1(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 }

Member Data Documentation

◆ argFileScale

PetscBool MoFEM::TimeScale::argFileScale = PETSC_FALSE

Definition at line 72 of file ScalingMethod.hpp.

◆ defaultDelimiter

const std::string MoFEM::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.

◆ 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:
MoFEMFunctionReturnHot
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:460
MoFEM::TimeScale::TimeScale
TimeScale(std::string file_name="", bool error_if_file_not_given=false, ScalingFun def_scaling_fun=[](double time) { return time;})
TimeScale constructor.
Definition: ScalingMethod.cpp:22
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:609
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEM::TimeScale::scalingMethod
ScalingFun scalingMethod
Definition: ScalingMethod.hpp:99
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:91
MoFEM::TimeScale::fileName
std::string fileName
Definition: ScalingMethod.hpp:71
MoFEM::TimeScale::timeData
MoFEMErrorCode timeData(std::string fileName, std::string delimiter)
Definition: ScalingMethod.cpp:35
t
constexpr double t
plate stiffness
Definition: plate.cpp:58
MoFEM::TimeScale::errorIfFileNotGiven
bool errorIfFileNotGiven
Definition: ScalingMethod.hpp:97
MoFEM::TimeScale::defScalingMethod
ScalingFun defScalingMethod
Definition: ScalingMethod.hpp:98
MoFEM::TimeScale::defaultDelimiter
static const std::string defaultDelimiter
comma or space
Definition: ScalingMethod.hpp:95
MOFEM_LOG
#define MOFEM_LOG(channel, severity)
Log.
Definition: LogManager.hpp:308
MoFEM::TimeScale::fileNameFlag
std::string fileNameFlag
Definition: ScalingMethod.hpp:92
MoFEM::TimeScale::argFileScale
PetscBool argFileScale
Definition: ScalingMethod.hpp:72
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::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:109
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359