OOFEM 3.0
Loading...
Searching...
No Matches
piecewiselinfunction.C
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
36#include "mathfem.h"
37#include "classfactory.h"
38#include "dynamicinputrecord.h"
39
40#include <fstream>
41#include <sstream>
42
43namespace oofem {
44#define PiecewiseLinFunction_PRECISION 1.e-12
45
47
48PiecewiseLinFunction :: PiecewiseLinFunction(int i, Domain *d) : Function(i, d), dates(), values()
49{ }
50
51double PiecewiseLinFunction :: evaluateAtTime(double time)
52// Returns the value of the receiver at time 'time'. 'time' should be
53// one of the dates of the receiver (currently there is no interpola-
54// tion between two points).
55{
56 const double precision = PiecewiseLinFunction_PRECISION;
57 double xa, xb, ya, yb;
58
59 if ( this->dates.giveSize() == 0 ) {
60 OOFEM_ERROR("Undefined dates and values");
61 }
62
63 for ( int i = 1; i <= this->dates.giveSize(); i++ ) {
64 if ( fabs(this->dates.at(i) - time) < precision ) {
65 return this->values.at(i);
66 } else if ( this->dates.at(i) > time ) {
67 if ( i == 1 ) {
68 OOFEM_WARNING("computational time %f is out of given time %f, using closest value", time, dates.at(i) );
69 return this->dates.at(i);
70 }
71
72 xa = this->dates.at(i - 1);
73 xb = this->dates.at(i);
74 ya = this->values.at(i - 1);
75 yb = this->values.at(i);
76
77 return ya + ( time - xa ) * ( yb - ya ) / ( xb - xa );
78 }
79 }
80
81 OOFEM_WARNING("computational time %f is out of given time, using closest value", time );
82 return this->values.at(this->values.giveSize());
83}
84
85double PiecewiseLinFunction :: evaluateVelocityAtTime(double time)
86// Returns the derivative of the receiver at time 'time'. 'time' should be
87// one of the dates of the receiver (currently there is no interpola-
88// tion between two points).
89{
90 const double precision = PiecewiseLinFunction_PRECISION;
91 double xa, xb, ya, yb;
92
93 if ( this->dates.giveSize() == 0 ) {
94 OOFEM_ERROR("Undefined dates and values");
95 }
96
97 for ( int i = 1; i <= this->dates.giveSize(); i++ ) {
98 if ( fabs(dates.at(i) - time) < precision ) {
99 if ( i < this->dates.giveSize() ) {
100 return ( this->values.at(i + 1) - this->values.at(i) ) / ( this->dates.at(i + 1) - this->dates.at(i) );
101 } else {
102 return ( this->values.at(i) - this->values.at(i - 1) ) / ( this->dates.at(i) - this->dates.at(i - 1) );
103 }
104 } else if ( dates.at(i) > time ) {
105 if ( i == 1 ) {
106 return 0.;
107 }
108
109 xa = this->dates.at(i - 1);
110 xb = this->dates.at(i);
111 ya = this->values.at(i - 1);
112 yb = this->values.at(i);
113
114 return ( yb - ya ) / ( xb - xa );
115 }
116 }
117
118 return 0.;
119}
120
121void
122PiecewiseLinFunction :: initializeFrom(InputRecord &ir)
123{
124 Function :: initializeFrom(ir);
125
126 // Optional means, read data from external file (useful for very large sets of data)
128 std :: list< double >t, ft;
129 // Open the file;
130 std :: string fname;
132 if (fname != ""){ //allows empty filename when the function will not be used intentionally
133 std :: ifstream file(fname.c_str(), std :: ios :: in);
134 if ( !file.is_open() ) {
135 OOFEM_ERROR("Failed to open data file: %s\n", fname.c_str());
136 }
137 // Data should be stored in two columns (or just interleaved)
138 double temp_t, temp_ft;
139 std :: string sLine = "";
140 while ( !file.eof() ) {
141 getline(file, sLine);
142 if ( sLine [ 0 ] == '#' ) {
143 continue;
144 }
145 std :: stringstream ss1(sLine);
146 ss1 >> temp_t >> temp_ft;
147 t.push_back(temp_t);
148 ft.push_back(temp_ft);
149 }
150 file.close();
151 // Copy data over the float arrays
152 dates.resize( t.size() );
153 values.resize( ft.size() );
154 std :: list< double > :: iterator it_t = t.begin(), it_ft = ft.begin();
155 for ( int i = 1; i <= ( int ) t.size(); ++i, ++it_t, ++it_ft ) {
156 dates.at(i) = * it_t;
157 values.at(i) = * it_ft;
158 }
159 }
160 } else {
161 int numberOfPoints;
162 IR_GIVE_OPTIONAL_FIELD(ir, numberOfPoints, "npoints");
165 IR_GIVE_OPTIONAL_FIELD(ir, this->parameterType, "paramtype");
166 }
167}
168
169
170void PiecewiseLinFunction :: giveInputRecord(DynamicInputRecord &input)
171{
172 Function :: giveInputRecord(input);
175}
176
177
178
179void
180PiecewiseLinFunction :: saveContext(DataStream &stream, ContextMode mode)
181{
182 if ( mode & CM_Definition ) {
183 dates.storeYourself(stream);
184 values.storeYourself(stream);
185 }
186}
187
188
189void
190PiecewiseLinFunction :: restoreContext(DataStream &stream, ContextMode mode)
191{
192 if ( mode & CM_Definition ) {
193 dates.restoreYourself(stream);
194 values.restoreYourself(stream);
195 }
196}
197} // end namespace oofem
#define REGISTER_Function(class)
void setField(int item, InputFieldType id)
Function(int n, Domain *d)
Definition function.C:44
virtual bool hasField(InputFieldType id)=0
Returns true if record contains field identified by idString keyword.
#define CM_Definition
Definition contextmode.h:47
#define OOFEM_WARNING(...)
Definition error.h:80
#define OOFEM_ERROR(...)
Definition error.h:79
#define IR_GIVE_OPTIONAL_FIELD(__ir, __value, __id)
Definition inputrecord.h:75
#define IR_GIVE_FIELD(__ir, __value, __id)
Definition inputrecord.h:67
long ContextMode
Definition contextmode.h:43
#define PiecewiseLinFunction_PRECISION
#define _IFT_PiecewiseLinFunction_dataFile
#define _IFT_PiecewiseLinFunction_t
#define _IFT_PiecewiseLinFunction_ft

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011