OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
scalarfunction.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 "scalarfunction.h"
36 #include "floatarray.h"
37 #include "domain.h"
38 #include "function.h"
39 #include "parser.h"
40 #include "error.h"
41 #include "gausspoint.h"
42 
43 #include <map>
44 #include <string>
45 #include <sstream>
46 
47 namespace oofem {
48 
50 {
51  this->dvType = DV_Undefined;
52 }
53 
55 {
56  this->dvType = DV_ValueType;
57  this->setValue(val);
58 }
59 
60 
62 {
63  // initialize type to DV_ValueType to prevent clear() method to clear unitialized memory. will be set correctly by set method.
64  this->dvType = DV_ValueType;
65  this->setSimpleExpression(val);
66 }
67 
69 {
70  // initialize type to DV_ValueType to prevent clear() method to clear unitialized memory. will be set correctly by set method.
71  this->dvType = DV_ValueType;
72  this->setReference(val);
73 }
74 
76 
77 
78 void
80 {
81  this->dvType = DV_ValueType;
82  this->dValue = val;
83 }
84 
85 
86 void
88 {
90  this->eValue = val;
91 }
92 
93 
94 void
96 {
98  this->fReference = val;
99 }
100 
101 
102 double
103 ScalarFunction :: eval(const std :: map< std :: string, FunctionArgument >valDict, Domain *d, GaussPoint *gp, double param) const
104 {
105  if ( this->dvType == DV_ValueType ) {
106  return this->dValue;
107  } else if ( this->dvType == DV_SimpleExpressionType ) {
108  std :: ostringstream buff;
109  Parser p;
110  int err;
111  // process valDict and call internal parser
112  for ( const auto &named_arg : valDict ) {
113  const FunctionArgument &arg = named_arg.second;
114  if ( arg.type == FunctionArgument :: FAT_double ) {
115  buff << named_arg.first << "=" << arg.val0 << ";";
116  } else if ( arg.type == FunctionArgument :: FAT_FloatArray ) {
117  for ( int i = 1; i <= arg.val1.giveSize(); ++i ) {
118  buff << named_arg.first << i << "=" << arg.val1.at(i) << ";";
119  }
120  } else if ( arg.type == FunctionArgument :: FAT_int ) {
121  buff << named_arg.first << "=" << arg.val2 << ";";
122  } else if ( arg.type == FunctionArgument :: FAT_IntArray ) {
123  for ( int i = 1; i <= arg.val3.giveSize(); ++i ) {
124  buff << named_arg.first << i << "=" << arg.val3.at(i) << ";";
125  }
126  }
127  }
128  buff << this->eValue;
129  //printf("string is %s\n", buff.str().c_str());
130  // evaluate the expression
131  double value = p.eval(buff.str().c_str(), err);
132  if ( err ) {
133  OOFEM_ERROR( "parser syntax error (expr=\"%s\")", buff.str().c_str() );
134  }
135  return value;
136  } else if ( this->dvType == DV_FunctionReferenceType ) {
137  FloatArray val;
138  d->giveFunction(this->fReference)->evaluate(val, valDict, gp, param);
139  if ( val.giveSize() != 1 ) {
140  OOFEM_ERROR( "Function @%d did not return a scalar (size = %d)", this->fReference, val.giveSize() );
141  }
142  return val.at(1);
143  }
144  return 0.;
145 }
146 
147 
148 double
149 ScalarFunction :: eval(double time, Domain *d) const
150 {
151  if ( this->dvType == DV_ValueType ) {
152  return this->dValue;
153  } else {
154  std :: map< std :: string, FunctionArgument >valDict;
155  valDict.insert( std :: make_pair("t", time) );
156  return this->eval(valDict, d);
157  }
158 }
159 
160 bool
162 {
163  if ( this->dvType == DV_Undefined ){
164  return 0;
165  }
166  return 1;
167 }
168 
169 std :: ostream &operator << ( std :: ostream & out, const ScalarFunction & s )
170 {
172  out << s.dValue;
174  out << '$' << s.eValue << '$';
175  } else {
176  out << '@' << s.fReference;
177  }
178  return out;
179 }
180 } // end namespace OOFEM
double dValue
Constant, double value.
double eval(const std::map< std::string, FunctionArgument >valDict, Domain *d, GaussPoint *gp=NULL, double param=0.) const
Evaluates the receiver.
Class and object Domain.
Definition: domain.h:115
double & at(int i)
Coefficient access function.
Definition: floatarray.h:131
std::string eValue
Simple expression (evaluated by internal parser)
bool isDefined() const
True if receiver is defined.
double evaluate(TimeStep *tStep, ValueModeType mode)
Returns the value of load time function at given time.
Definition: function.C:55
int & at(int i)
Coefficient access function.
Definition: intarray.h:103
void setValue(double val)
Sets receiver to be a constant scalar function defined by given value.
friend std::ostream & operator<<(std::ostream &out, const ScalarFunction &s)
Wrapper for values of varying types.
Definition: function.h:60
void setReference(int val)
Sets receiver to be a scalar function defined using external function.
ScalarFunction()
Creates empty constant scalar function.
#define OOFEM_ERROR(...)
Definition: error.h:61
void setSimpleExpression(std::string &val)
Sets receiver to be a scalar funtion defined by given simple expression.
Function * giveFunction(int n)
Service for accessing particular domain load time function.
Definition: domain.C:268
int fReference
Reference to external function.
FunctionArgumentType type
Determines which of the types the instance points towards.
Definition: function.h:71
Class representing vector of real numbers.
Definition: floatarray.h:82
Implementation of Scalar function.
enum oofem::ScalarFunction::@1 dvType
Enum value determining the dataValue type.
int giveSize() const
Definition: intarray.h:203
int giveSize() const
Returns the size of receiver.
Definition: floatarray.h:218
the oofem namespace is to define a context or scope in which all oofem names are defined.
Class representing integration point in finite element program.
Definition: gausspoint.h:93
Class for evaluating mathematical expressions in strings.
Definition: parser.h:54
double eval(const char *string, int &err)
Definition: parser.C:378

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:31 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011