OOFEM 3.0
Loading...
Searching...
No Matches
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 - 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 <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
49namespace oofem {
51
52UserDefDirichletBC :: UserDefDirichletBC(int i, Domain *d) : BoundaryCondition(i, d), mpModule(NULL)
53{ }
54
55
56UserDefDirichletBC :: ~UserDefDirichletBC()
57{
58 if ( mpModule ) Py_DECREF(mpModule);
59}
60
61
62double
63UserDefDirichletBC :: give(Dof *dof, ValueModeType mode, double time)
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
131void
132UserDefDirichletBC :: initializeFrom(InputRecord &ir)
133// Sets up the dictionary where the receiver stores the conditions it
134// imposes.
135{
136 GeneralBoundaryCondition :: initializeFrom(ir);
137
139
140 // Import Python file
141 PyObject *mpName = PyUnicode_FromString( this->mFileName.c_str() );
142 mpModule = PyImport_Import(mpName);
143 Py_DECREF(mpName);
144
145 if ( mpModule != NULL ) {
146 // Load and call Python function
147 mpFunc = PyObject_GetAttrString(mpModule, "giveUserDefBC");
148 if ( !mpFunc ) {
149 throw ValueInputException(ir, _IFT_UserDefDirichletBC_filename, "Cannot find function 'giveUserDefBC' in file: " + mFileName);
150 }
151 } else {
152 throw ValueInputException(ir, _IFT_UserDefDirichletBC_filename, "Cannot find module in file: " + mFileName);
153 }
154}
155
156
157void
158UserDefDirichletBC :: giveInputRecord(DynamicInputRecord &input)
159{
160 GeneralBoundaryCondition :: giveInputRecord(input);
162}
163
164
165void
166UserDefDirichletBC :: scale(double s)
167{
168 values.times(s);
169}
170} // end namespace oofem
#define REGISTER_BoundaryCondition(class)
BoundaryCondition(int i, Domain *d)
double giveCoordinate(int i) const
Definition dofmanager.h:383
const FloatArray & giveCoordinates() const
Definition dofmanager.h:390
DofIDItem giveDofID() const
Definition dof.h:276
DofManager * giveDofManager() const
Definition dof.h:123
void setField(int item, InputFieldType id)
Index giveSize() const
Returns the size of receiver.
Definition floatarray.h:261
FloatArray values
Prescribed values for each resp. dof.
#define OOFEM_ERROR(...)
Definition error.h:79
#define IR_GIVE_FIELD(__ir, __value, __id)
Definition inputrecord.h:67
_object PyObject
#define _IFT_UserDefDirichletBC_filename

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