-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathPyVTKOutput.cpp
More file actions
231 lines (180 loc) · 5.48 KB
/
PyVTKOutput.cpp
File metadata and controls
231 lines (180 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Source includes
#include "fileIO/Outputs/VTKOutput.hpp"
#include "PyVTKOutputType.hpp"
#include "dataRepository/python/PyGroupType.hpp"
#define VERIFY_NON_NULL_SELF( self ) \
PYTHON_ERROR_IF( self == nullptr, PyExc_RuntimeError, "Passed a nullptr as self.", nullptr )
#define VERIFY_INITIALIZED( self ) \
PYTHON_ERROR_IF( self->group == nullptr, PyExc_RuntimeError, "The PyVTKOutput is not initialized.", nullptr )
namespace geos
{
namespace python
{
struct PyVTKOutput
{
PyObject_HEAD
static constexpr char const * docString =
"A Python interface to VTKOutput.";
geos::VTKOutput * group;
};
static PyObject * PyVTKOutput_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
{
GEOS_UNUSED_VAR( args, kwds );
PyVTKOutput *self;
self = (PyVTKOutput *)type->tp_alloc( type, 0 );
if( self != nullptr )
{
self->group = nullptr;
}
return (PyObject *)self;
}
static PyObject * PyVTKOutput_repr( PyObject * const obj ) noexcept
{
PyVTKOutput const * const pyVTKOutput = LvArray::python::convert< PyVTKOutput >( obj, getPyVTKOutputType() );
if( pyVTKOutput == nullptr )
{
return nullptr;
}
VERIFY_INITIALIZED( pyVTKOutput );
string repr;
string const path = pyVTKOutput->group->getPath();
string const type = LvArray::system::demangle( typeid( *(pyVTKOutput->group) ).name() );
repr = path + " ( " + type + " )";
return PyUnicode_FromString( repr.c_str() );
}
static PyObject * output( PyVTKOutput * self, PyObject * args )
{
VERIFY_NON_NULL_SELF( self );
VERIFY_INITIALIZED( self );
double time;
double dt;
int cycleNumber;
if( !PyArg_ParseTuple( args, "ddi", &time, &dt, &cycleNumber ) )
{
return nullptr;
}
geos::DomainPartition & domain = self->group->getGroupByPath< DomainPartition >( "/Problem/domain" );
try
{
self->group->execute( time, dt, cycleNumber, 0, 0, domain );
}
catch( std::out_of_range const & e )
{
std::cout << "Target not found. Impossible output."<< std::endl;
}
Py_RETURN_NONE;
}
static PyObject * setOutputDir( PyVTKOutput * self, PyObject * args )
{
VERIFY_NON_NULL_SELF( self );
VERIFY_INITIALIZED( self );
PyObject * unicodePath;
if( !PyArg_ParseTuple( args, "U", &unicodePath ) )
{
return nullptr;
}
LvArray::python::PyObjectRef<> asciiPath { PyUnicode_AsASCIIString( unicodePath ) };
if( asciiPath == nullptr )
{
return nullptr;
}
char const * const path = PyBytes_AsString( asciiPath );
if( path == nullptr )
{
return nullptr;
}
try
{
self->group->setOutputDirectory( path );
self->group->postInputInitialization();
}
catch( std::out_of_range const & e )
{
std::cout << "Target not found." <<std::endl;
}
Py_RETURN_NONE;
}
static PyObject * setOutputFileRootName( PyVTKOutput * self, PyObject * args )
{
VERIFY_NON_NULL_SELF( self );
VERIFY_INITIALIZED( self );
PyObject * fileNameRoot;
if( !PyArg_ParseTuple( args, "U", &fileNameRoot ) )
{
return nullptr;
}
LvArray::python::PyObjectRef<> asciiFileNameRoot { PyUnicode_AsASCIIString( fileNameRoot ) };
if( asciiFileNameRoot == nullptr )
{
return nullptr;
}
char const * const filename = PyBytes_AsString( asciiFileNameRoot );
if( filename == nullptr )
{
return nullptr;
}
try
{
self->group->setPlotFileRoot( filename );
self->group->postInputInitialization();
}
catch( std::out_of_range const & e )
{
std::cout << "Target not found." << std::endl;
}
Py_RETURN_NONE;
}
static PyObject * reinit( PyVTKOutput * self, PyObject *args )
{
VERIFY_NON_NULL_SELF( self );
VERIFY_INITIALIZED( self );
GEOS_UNUSED_VAR( args );
self->group->reinit();
Py_RETURN_NONE;
}
static PyMethodDef PyVTKOutput_methods[] = {
{ "output", (PyCFunction) output, METH_VARARGS, "wrapper to routine VTKOutput::execute"},
{ "setOutputDir", (PyCFunction) setOutputDir, METH_VARARGS, "wrapper to change directory output"},
{ "setOutputFileRootName", (PyCFunction) setOutputFileRootName, METH_VARARGS, "wrapper to change the root name of the output files and subfolders"},
{ "reinit", (PyCFunction) reinit, METH_VARARGS, "reinitialization function"},
{ nullptr, nullptr, 0, nullptr } /* Sentinel */
};
/**
* Initialize the module object for Python with the exported functions
*/
BEGIN_ALLOW_DESIGNATED_INITIALIZERS
static PyTypeObject PyVTKOutputType = {
PyVarObject_HEAD_INIT( nullptr, 0 )
.tp_name = "pygeosx.VTKOutput",
.tp_basicsize = sizeof( PyVTKOutput ),
.tp_itemsize = 0,
.tp_repr = PyVTKOutput_repr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = PyVTKOutput::docString,
.tp_methods = PyVTKOutput_methods,
.tp_base = getPyGroupType(),
.tp_new = PyVTKOutput_new,
};
END_ALLOW_DESIGNATED_INITIALIZERS
PyTypeObject * getPyVTKOutputType()
{
return &PyVTKOutputType;
}
}
}