OOFEM 3.0
Loading...
Searching...
No Matches
pythonfield.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#ifdef _USE_NANOBIND
36 #include<nanobind/nanobind.h>
37 // must include converters here so that compiler can pickup template specialization for arrays
38 #include"../../bindings/python/oofemarray-nanobind.h"
39 namespace nb = nanobind;
40#else
41 #include <pybind11/embed.h>
42 namespace py = pybind11;
43#endif
44
45
46#include "field.h"
47#include "pythonfield.h"
48#include "floatarray.h"
49#include "timestep.h"
50
51
52
53namespace oofem {
54// REGISTER_Material(PythonField);
55
56
58PythonField :: PythonField(void) : Field(FieldType::FT_Unknown)
59{
60 //pybind11::print("Hello, World!"); // use the Python API
61}
62
63void PythonField :: setFunctionName(std::string functionName){
64 this->functionName = functionName;
65}
66
67
68void PythonField :: setModuleName(std::string moduleName){
69 this->moduleName = moduleName;
70 //this->moduleName.resize(this->moduleName.size()); //remove trailing quotes
71}
72
73int PythonField :: evaluateAt(FloatArray &answer, const FloatArray &coords, ValueModeType mode, TimeStep *tStep)
74{
75 // Do not use the raw CPython API functions Py_Initialize and Py_Finalize as these do not properly handle the lifetime of pybind11’s internal data.
76 // https://pybind11.readthedocs.io/en/stable/advanced/embedding.html
77
78 // py::scoped_interpreter guard{}; // start the interpreter and keep it alive
79
80// py::initialize_interpreter();
81 #ifdef _USE_NANOBIND
82 nb::module_ calc = nb::module_::import_(moduleName.c_str());
83 nb::object result = calc.attr(functionName.c_str())(nb::cast(coords), nb::cast(mode), nb::cast(tStep));
84 answer = nb::cast<FloatArray>(result);
85 #else
86 py::module calc = py::module::import(moduleName.c_str());
87 py::object result = calc.attr(functionName.c_str())(coords, mode, tStep);
88 answer = result.cast<FloatArray>();
89 #endif
90
91
92// py::finalize_interpreter();
93
94 return 0;
95}
96
97int PythonField :: evaluateAt(FloatArray &answer, DofManager *dman, ValueModeType mode, TimeStep *tStep)
98{
99 OOFEM_ERROR("Not implemented");
100 return 1;
101}
102
103
104
105} // end namespace oofem
Field(FieldType b=FieldType::FT_Unknown)
Definition field.h:95
std::string functionName
Definition pythonfield.h:69
std::string moduleName
Name of python module containing evaluating function function.
Definition pythonfield.h:68
#define OOFEM_ERROR(...)
Definition error.h:79
FieldType
Physical type of field.
Definition field.h:64

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