OOFEM 3.0
Loading...
Searching...
No Matches
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 - 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
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
47namespace oofem {
48
49ScalarFunction :: ScalarFunction()
50{
51 this->dvType = DV_Undefined;
52}
53
54ScalarFunction :: ScalarFunction(double val)
55{
56 this->dvType = DV_ValueType;
57 this->setValue(val);
58}
59
60
61ScalarFunction :: ScalarFunction(std :: string &val)
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
68ScalarFunction :: ScalarFunction(int val)
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
75ScalarFunction :: ~ScalarFunction() { }
76
77
78void
79ScalarFunction :: setValue(double val)
80{
81 this->dvType = DV_ValueType;
82 this->dValue = val;
83}
84
85
86void
87ScalarFunction :: setSimpleExpression(std :: string &val)
88{
90 this->eValue = val;
91}
92
93
94void
95ScalarFunction :: setReference(int val)
96{
98 this->fReference = val;
99}
100
101
102double
103ScalarFunction :: 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, (int)val.giveSize() );
141 }
142 return val.at(1);
143 }
144 return 0.;
145}
146
147
148double
149ScalarFunction :: 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
160bool
161ScalarFunction :: isDefined() const
162{
163 if ( this->dvType == DV_Undefined ){
164 return 0;
165 }
166 return 1;
167}
168
169std :: ostream &operator << ( std :: ostream & out, const ScalarFunction & s )
170{
171 if ( s.dvType == ScalarFunction :: DV_ValueType ) {
172 out << s.dValue;
173 } else if ( s.dvType == ScalarFunction :: DV_SimpleExpressionType ) {
174 out << '$' << s.eValue << '$';
175 } else {
176 out << '@' << s.fReference;
177 }
178 return out;
179}
180} // end namespace OOFEM
Function * giveFunction(int n)
Definition domain.C:271
double & at(Index i)
Definition floatarray.h:202
Index giveSize() const
Returns the size of receiver.
Definition floatarray.h:261
FunctionArgumentType type
Determines which of the types the instance points towards.
Definition function.h:72
virtual double evaluate(TimeStep *tStep, ValueModeType mode)
Definition function.C:57
int & at(std::size_t i)
Definition intarray.h:104
int giveSize() const
Definition intarray.h:211
double eval(const char *string, int &err)
Definition parser.C:419
enum oofem::ScalarFunction::@044127242061323210226146117116105310173173316037 dvType
Enum value determining the dataValue type.
double dValue
Constant, double value.
void setSimpleExpression(std ::string &val)
int fReference
Reference to external function.
double eval(const std ::map< std ::string, FunctionArgument >valDict, Domain *d, GaussPoint *gp=NULL, double param=0.) const
void setReference(int val)
void setValue(double val)
std::string eValue
Simple expression (evaluated by internal parser).
#define OOFEM_ERROR(...)
Definition error.h:79
std::ostream & operator<<(std ::ostream &out, const Dictionary &r)
Definition dictionary.C:247

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