OOFEM  2.4
OOFEM.org - Object Oriented Finite Element Solver
userdefdirichletbc.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 <Python.h>
36 
37 #include "userdefdirichletbc.h"
38 
39 #include "boundarycondition.h"
40 #include "timestep.h"
41 #include "function.h"
42 #include "verbose.h"
43 #include "dynamicinputrecord.h"
44 #include "classfactory.h"
45 #include "dofmanager.h"
46 #include "dof.h"
47 #include "error.h"
48 
49 namespace oofem {
50 REGISTER_BoundaryCondition(UserDefDirichletBC);
51 
53 { }
54 
55 
57 {
58  if ( mpModule ) Py_DECREF(mpModule);
59 }
60 
61 
62 double
64 {
65  double factor = 0.;
66  if ( mode == VM_Total ) {
67  factor = this->giveTimeFunction()->evaluateAtTime(time);
68  } else if ( mode == VM_Velocity ) {
69  factor = this->giveTimeFunction()->evaluateVelocityAtTime(time);
70  } else if ( mode == VM_Acceleration ) {
71  factor = this->giveTimeFunction()->evaluateAccelerationAtTime(time);
72  } else {
73  OOFEM_ERROR("Should not be called for value mode type then total, velocity, or acceleration.");
74  }
75  DofManager *dMan = dof->giveDofManager();
76 
77 
78  /*
79  * The Python function takes three input arguments:
80  * 1) An array with node coordinates
81  * 2) The dof id
82  * 3) The current time
83  */
84  int numArgs = 3;
85 
86  // Create array with node coordinates
87  int dim = dMan->giveCoordinates()->giveSize();
88  PyObject *pArgArray = PyList_New(dim);
89 
90  PyObject *pArgs = PyTuple_New(numArgs);
91 
92  for ( int i = 0; i < dim; i++ ) {
93  PyList_SET_ITEM( pArgArray, i, PyFloat_FromDouble( dMan->giveCoordinate(i + 1) ) );
94  }
95 
96  // PyTuple_SetItem takes over responsibility for objects passed
97  // to it -> no DECREF
98  PyTuple_SetItem(pArgs, 0, pArgArray);
99 
100  // Dof number
101  PyTuple_SetItem(pArgs, 1, PyLong_FromLong( dof->giveDofID() ));
102 
103  // Time
104  PyTuple_SetItem(pArgs, 2, PyFloat_FromDouble( time ));
105 
106  // Value returned from the Python function
107  PyObject *pRetVal = NULL;
108 
109  if ( PyCallable_Check(mpFunc) ) {
110  pRetVal = PyObject_CallObject(mpFunc, pArgs);
111  } else {
112  OOFEM_ERROR("Python function is not callable.");
113  }
114 
115  // Get return value
116  double retVal = 0.0;
117  if ( pRetVal != NULL ) {
118  retVal = PyFloat_AsDouble(pRetVal);
119  } else {
120  OOFEM_ERROR("Failed to fetch Python return value.");
121  }
122 
123  // Decrement reference count on pointers
124  Py_DECREF(pArgs);
125  Py_DECREF(pRetVal);
126 
127  return retVal * factor;
128 }
129 
130 
133 // Sets up the dictionary where the receiver stores the conditions it
134 // imposes.
135 {
136  IRResultType result; // Required by IR_GIVE_FIELD macro
137 
139  if ( result != IRRT_OK ) {
140  return result;
141  }
142 
144 
145  // Import Python file
146  PyObject *mpName = PyString_FromString( this->mFileName.c_str() );
147  mpModule = PyImport_Import(mpName);
148  Py_DECREF(mpName);
149 
150  if ( mpModule != NULL ) {
151  // Load and call Python function
152  mpFunc = PyObject_GetAttrString(mpModule, "giveUserDefBC");
153  if ( !mpFunc ) {
154  OOFEM_WARNING("Cannot find function 'giveUserDefBC' in file: %s", this->mFileName.c_str());
155  return IRRT_BAD_FORMAT;
156  }
157  } else {
158  OOFEM_WARNING("Cannot find module in file: %s", this->mFileName.c_str());
159  return IRRT_BAD_FORMAT;
160  }
161 
162  return IRRT_OK;
163 }
164 
165 
166 void
168 {
171 }
172 
173 
174 void
176 {
177  values.times(s);
178 }
179 } // end namespace oofem
void setField(int item, InputFieldType id)
REGISTER_BoundaryCondition(BoundaryCondition)
virtual double evaluateAccelerationAtTime(double t)=0
Returns the second time derivative of the function at given time.
Class and object Domain.
Definition: domain.h:115
_object PyObject
#define _IFT_UserDefDirichletBC_filename
ValueModeType
Type representing the mode of UnknownType or CharType, or similar types.
Definition: valuemodetype.h:78
virtual FloatArray * giveCoordinates()
Definition: dofmanager.h:382
virtual void giveInputRecord(DynamicInputRecord &input)
Setups the input record string of receiver.
Base class for dof managers.
Definition: dofmanager.h:113
virtual void scale(double s)
Scales the receiver according to given value.
FloatArray values
Prescribed values for each resp. dof.
virtual ~UserDefDirichletBC()
Destructor.
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
Class implementing Dirichlet boundary condition on DOF (primary boundary condition).
virtual double give(Dof *dof, ValueModeType mode, double time)
#define OOFEM_ERROR(...)
Definition: error.h:61
DofIDItem giveDofID() const
Returns DofID value of receiver, which determines type of of unknown connected to receiver (e...
Definition: dof.h:276
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
virtual double evaluateVelocityAtTime(double t)=0
Returns the first time derivative of the function at given time.
Initializes the variable VERBOSE, in order to get a few intermediate messages on screen: beginning an...
IRResultType
Type defining the return values of InputRecord reading operations.
Definition: irresulttype.h:47
Class representing the general Input Record.
Definition: inputrecord.h:101
Class representing the a dynamic Input Record.
void times(double s)
Multiplies receiver with scalar.
Definition: floatarray.C:818
virtual void giveInputRecord(DynamicInputRecord &input)
Setups the input record string of receiver.
DofManager * giveDofManager() const
Definition: dof.h:123
int giveSize() const
Returns the size of receiver.
Definition: floatarray.h:218
virtual double giveCoordinate(int i)
Definition: dofmanager.h:380
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
Abstract class Dof represents Degree Of Freedom in finite element mesh.
Definition: dof.h:93
virtual double evaluateAtTime(double t)
Returns the value of the function at given time.
Definition: function.C:76
#define OOFEM_WARNING(...)
Definition: error.h:62
UserDefDirichletBC(int i, Domain *d)
Constructor.

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