OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
interpolatingfunction.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 "interpolatingfunction.h"
36 #include "floatarray.h"
37 #include "classfactory.h"
38 #include "error.h"
39 
40 #include <iostream>
41 #include <fstream>
42 
43 namespace oofem {
44 REGISTER_Function(InterpolatingFuction);
45 
47 { }
48 
50 { }
51 
52 
53 void
54 InterpolatingFuction :: evaluate(FloatArray &answer, const std :: map< std :: string, FunctionArgument > &valDict, GaussPoint *gp, double param)
55 {
56  auto it = valDict.find("x");
57  if ( it == valDict.end() ) {
58  OOFEM_ERROR("Coordinate needed for evaluating function");
59  }
60  const FloatArray &globalCoordinates = it->second.val1;
61  // Map the corresponding random value from the field
62  int countXDown = 0, countXUp = 0, countYDown = 0, countYUp = 0;
63  double randomVariable = 0.;
64  //Determine x count
65  double helpX = 0., helpY = 0.;
66  int i = 0, exitFlag = 0;
67 
68  //Check if gp lies in random field
69  double randomXStart = field(0);
70  double randomXEnd = field( 3 * ( numberReal(0) - 1 ) * numberReal(1) );
71  double randomYStart = field(1);
72  double randomYEnd = field(3 * ( numberReal(0) - 1 ) + 1);
73 
74  if ( !( globalCoordinates.at(1) > randomXStart && globalCoordinates.at(1) < randomXEnd &&
75  globalCoordinates.at(2) > randomYStart && globalCoordinates.at(2) < randomYEnd ) ) {
76  randomVariable = 1;
77  } else {
78  //Determine the corner points of the square over which we are going to interpolate
79  while ( exitFlag == 0 ) {
80  if ( field( 3 * i * numberReal(1) ) > globalCoordinates.at(1) ) {
81  if ( i == 0 ) { //Check soundness
82  OOFEM_ERROR("i is zero");
83  } else if ( i == numberReal(0) ) {
84  OOFEM_ERROR("i is equal to realNumber");
85  }
86 
87  exitFlag = 1;
88  countXDown = i - 1;
89  countXUp = i;
90  helpX = ( globalCoordinates.at(1) - field( 3 * countXDown * numberReal(1) ) )
91  / ( field( 3 * countXUp * numberReal(1) ) - field( 3 * countXDown * numberReal(1) ) );
92  }
93 
94  i++;
95  }
96 
97  //Determine y count
98  i = 0, exitFlag = 0;
99  while ( exitFlag == 0 ) {
100  if ( field(3 * i + 1) > globalCoordinates.at(2) ) {
101  if ( i == 0 ) {
102  OOFEM_ERROR("i is zero");
103  } else if ( i == numberReal(0) ) {
104  OOFEM_ERROR("i is equal to realNumber");
105  }
106 
107  exitFlag = 1;
108  countYDown = i - 1;
109  countYUp = i;
110  helpY = ( globalCoordinates.at(2) - field(3 * countYDown + 1) )
111  / ( field(3 * countYUp + 1) - field(3 * countYDown + 1) );
112  }
113 
114  i++;
115  }
116 
117  //Do the interpolation
118  if ( randomVariable == 0. ) {
119  randomVariable =
120  ( 1. - helpX ) * ( 1. - helpY ) * field(3 * ( countXDown * numberReal(1) + countYDown ) + 2) +
121  helpX * ( 1. - helpY ) * field(3 * ( countXUp * numberReal(1) + countYDown ) + 2) +
122  helpX *helpY *field(3 * ( countXUp *numberReal ( 1 ) + countYUp ) + 2) +
123  ( 1. - helpX ) * helpY * field(3 * ( countXDown * numberReal(1) + countYUp ) + 2);
124  }
125  }
126 
127  if ( randomVariable <= 0. ) {
128  randomVariable = 1.e-8;
129  }
130 
131  answer = FloatArray{randomVariable};
132 }
133 
134 double
136 {
137  OOFEM_ERROR("InterpolatingFunction needs coordinates to evaluate.");
138  return 0.;
139 }
140 
141 
142 
145 {
146  std :: string name;
147  IRResultType result; // Required by IR_GIVE_FIELD macro
148 
150 
151  std :: ifstream inputField( name.c_str() );
152 
153  if ( !inputField.is_open() ) {
154  OOFEM_WARNING("Unable to open file %s", name.c_str());
155  return IRRT_BAD_FORMAT;
156  }
157 
158  double deltaX, deltaY;
159  numberReal.resize(2);
160  numberReal.zero();
161  inputField >> numberReal(0) >> numberReal(1) >> deltaX >> deltaY;
162 
163  field.resize( 3 * numberReal(0) * numberReal(1) );
164  field.zero();
165 
166  //Read in coordinates and field values
167  for ( int i = 0; i < numberReal(0) * numberReal(1); i++ ) {
168  inputField >> field(3 * i) >> field(3 * i + 1) >> field(3 * i + 2);
169  }
170 
171  return IRRT_OK;
172 }
173 } // end namespace oofem
virtual ~InterpolatingFuction()
Destructor.
Class and object Domain.
Definition: domain.h:115
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
void zero()
Sets all component to zero.
Definition: intarray.C:52
double & at(int i)
Coefficient access function.
Definition: floatarray.h:131
REGISTER_Function(CalculatorFunction)
virtual void evaluate(FloatArray &answer, const std::map< std::string, FunctionArgument > &valDict, GaussPoint *gp=NULL, double param=0.)
Returns the value of the function for given input.
#define OOFEM_ERROR(...)
Definition: error.h:61
void resize(int n)
Checks size of receiver towards requested bounds.
Definition: intarray.C:124
InterpolatingFuction(int n, Domain *d)
Constructor.
Class representing vector of real numbers.
Definition: floatarray.h:82
virtual double evaluateAtTime(double t)
Returns the value of the function at given time.
Abstract base class representing a function with vector input and output.
Definition: function.h:88
IRResultType
Type defining the return values of InputRecord reading operations.
Definition: irresulttype.h:47
Class representing the general Input Record.
Definition: inputrecord.h:101
void zero()
Zeroes all coefficients of receiver.
Definition: floatarray.C:658
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
#define _IFT_InterpolatingFuction_filename
Class representing integration point in finite element program.
Definition: gausspoint.h:93
#define OOFEM_WARNING(...)
Definition: error.h:62
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:29 for OOFEM by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2011