OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
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 - 2013 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 
35 #include "piecewiselinfunction.h"
36 #include "mathfem.h"
37 #include "classfactory.h"
38 #include "dynamicinputrecord.h"
39 
40 #include <fstream>
41 #include <sstream>
42 
43 namespace oofem {
44 #define PiecewiseLinFunction_PRECISION 1.e-12
45 
46 REGISTER_Function(PiecewiseLinFunction);
47 
48 PiecewiseLinFunction :: PiecewiseLinFunction(int i, Domain *d) : Function(i, d), dates(), values()
49 { }
50 
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 
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 
123 {
124  IRResultType result; // Required by IR_GIVE_FIELD macro
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  return Function :: initializeFrom(ir);
169 }
170 
171 
173 {
177 }
178 
179 
180 
183 {
184  if ( mode & CM_Definition ) {
185  dates.storeYourself(stream);
186  values.storeYourself(stream);
187  }
188 
189  return CIO_OK;
190 }
191 
192 
195 {
196  if ( mode & CM_Definition ) {
197  dates.restoreYourself(stream);
198  values.restoreYourself(stream);
199  }
200 
201  return CIO_OK;
202 }
203 } // end namespace oofem
void setField(int item, InputFieldType id)
Class and object Domain.
Definition: domain.h:115
#define PiecewiseLinFunction_PRECISION
The purpose of DataStream abstract class is to allow to store/restore context to different streams...
Definition: datastream.h:54
contextIOResultType storeYourself(DataStream &stream) const
Definition: floatarray.C:872
double & at(int i)
Coefficient access function.
Definition: floatarray.h:131
virtual bool hasField(InputFieldType id)=0
Returns true if record contains field identified by idString keyword.
PiecewiseLinFunction(int i, Domain *d)
virtual void giveInputRecord(DynamicInputRecord &input)
Setups the input record string of receiver.
#define _IFT_PiecewiseLinFunction_ft
REGISTER_Function(CalculatorFunction)
virtual void giveInputRecord(DynamicInputRecord &input)
Setups the input record string of receiver.
Definition: femcmpnn.C:77
#define _IFT_PiecewiseLinFunction_t
virtual contextIOResultType restoreContext(DataStream &stream, ContextMode mode, void *obj=NULL)
Restores the receiver state previously written in stream.
#define OOFEM_ERROR(...)
Definition: error.h:61
contextIOResultType restoreYourself(DataStream &stream)
Definition: floatarray.C:895
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
Definition: femcmpnn.C:89
Abstract base class representing a function with vector input and output.
Definition: function.h:88
virtual double evaluateVelocityAtTime(double t)
Returns the first time derivative of the function at given time.
IRResultType
Type defining the return values of InputRecord reading operations.
Definition: irresulttype.h:47
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
Class representing the general Input Record.
Definition: inputrecord.h:101
Class representing the a dynamic Input Record.
long ContextMode
Context mode (mask), defining the type of information written/read to/from context.
Definition: contextmode.h:43
#define CM_Definition
Definition: contextmode.h:47
#define IR_GIVE_OPTIONAL_FIELD(__ir, __value, __id)
Macro facilitating the use of input record reading methods.
Definition: inputrecord.h:78
int giveSize() const
Returns the size of receiver.
Definition: floatarray.h:218
virtual double evaluateAtTime(double t)
Returns the value of the function at given time.
the oofem namespace is to define a context or scope in which all oofem names are defined.
#define IR_GIVE_FIELD(__ir, __value, __id)
Macro facilitating the use of input record reading methods.
Definition: inputrecord.h:69
int parameterType
Definition: function.h:92
#define _IFT_PiecewiseLinFunction_dataFile
#define OOFEM_WARNING(...)
Definition: error.h:62
virtual contextIOResultType saveContext(DataStream &stream, ContextMode mode, void *obj=NULL)
Stores receiver state to output stream.
void resize(int s)
Resizes receiver towards requested size.
Definition: floatarray.C:631

This page is part of the OOFEM documentation. Copyright (c) 2011 Borek Patzak
Project e-mail: info@oofem.org
Generated at Tue Jan 2 2018 20:07:30 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011