Skip to content

Commit c2d73e6

Browse files
committed
working version with possibility to initialize the flow from python
1 parent 227e375 commit c2d73e6

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

src/pydefix.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ PYBIND11_EMBEDDED_MODULE(pydefix, m) {
4646
.def_readwrite("Ex3", &DataBlockHost::Ex3)
4747
#endif
4848
.def_readwrite("InvDt", &DataBlockHost::InvDt);
49+
50+
m.attr("RHO") = RHO;
51+
m.attr("VX1") = VX1;
52+
m.attr("VX2") = VX2;
53+
m.attr("VX3") = VX3;
54+
m.attr("PRS") = PRS;
55+
#if MHD == YES
56+
m.attr("BX1") = BX1;
57+
m.attr("BX2") = BX2;
58+
m.attr("BX3") = BX3;
59+
#endif
60+
m.attr("IDIR") = IDIR;
61+
m.attr("JDIR") = JDIR;
62+
m.attr("KDIR") = KDIR;
4963
}
5064

5165

@@ -87,7 +101,7 @@ void Pydefix::Output(DataBlock &data) {
87101
}
88102
DataBlockHost dataHost(data);
89103
dataHost.SyncFromDevice();
90-
this->CallScript(&dataHost,this->scriptFilename,this->outputFunctionName);
104+
this->CallScript(dataHost,this->scriptFilename,this->outputFunctionName);
91105
idfx::popRegion();
92106
}
93107

@@ -102,7 +116,7 @@ void Pydefix::InitFlow(DataBlock &data) {
102116
}
103117
DataBlockHost dataHost(data);
104118
dataHost.SyncFromDevice();
105-
this->CallScript(&dataHost,this->scriptFilename,this->initflowFunctionName);
119+
this->CallScript(dataHost,this->scriptFilename,this->initflowFunctionName);
106120
dataHost.SyncToDevice();
107121
idfx::popRegion();
108122
}
@@ -136,14 +150,15 @@ Pydefix::~Pydefix() {
136150
}
137151
}
138152

139-
void Pydefix::CallScript(DataBlockHost *data, std::string scriptName, std::string funcName) {
153+
void Pydefix::CallScript(DataBlockHost &data, std::string scriptName, std::string funcName) {
140154
idfx::pushRegion("Pydefix::CallScript");
141155
try {
142156
//auto Vc = pydefix.toNumpyArray(d.Vc);
143157
py::module_ script = py::module_::import(scriptName.c_str());
144158

145159
//py::module_ embeded = py::module_::import("embeded");
146160
//py::object myV = py::cast(Vc);
161+
//py::object result = script.attr(funcName.c_str())(data);
147162
py::object result = script.attr(funcName.c_str())(data);
148163
} catch(std::exception &e) {
149164
std::stringstream message;

src/pydefix.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Pydefix {
3434
bool haveOutput{false};
3535
bool haveInitflow{false};
3636
private:
37-
void CallScript(DataBlockHost *, std::string, std::string);
37+
void CallScript(DataBlockHost &, std::string, std::string);
3838
static int ninstance;
3939
std::string scriptFilename;
4040
std::string outputFunctionName;
@@ -78,12 +78,13 @@ template <typename T> struct type_caster<IdefixHostArray4D<T>> {
7878
static py::handle cast(const IdefixHostArray4D<T>& src,
7979
py::return_value_policy policy,
8080
py::handle parent) {
81+
py::none dummyDataOwner;
8182
py::array_t<real, py::array::c_style> a({src.extent(0),
8283
src.extent(1),
8384
src.extent(2),
8485
src.extent(3)},
85-
src.data());
86-
idfx::cout << "Coucou @ cast" << std::endl;
86+
src.data(), dummyDataOwner);
87+
8788
return a.release();
8889
}
8990
};
@@ -122,10 +123,11 @@ template <typename T> struct type_caster<IdefixHostArray3D<T>> {
122123
static py::handle cast(const IdefixHostArray3D<T>& src,
123124
py::return_value_policy policy,
124125
py::handle parent) {
126+
py::none dummyDataOwner;
125127
py::array_t<real, py::array::c_style> a({src.extent(0),
126128
src.extent(1),
127129
src.extent(2)},
128-
src.data());
130+
src.data(),dummyDataOwner);
129131
return a.release();
130132
}
131133
};
@@ -164,7 +166,8 @@ template <typename T> struct type_caster<IdefixHostArray2D<T>> {
164166
static py::handle cast(const IdefixHostArray2D<T>& src,
165167
py::return_value_policy policy,
166168
py::handle parent) {
167-
py::array_t<real, py::array::c_style> a({src.extent(0),src.extent(1)},src.data());
169+
py::none dummyOwner;
170+
py::array_t<real, py::array::c_style> a({src.extent(0),src.extent(1)},src.data(),dummyOwner);
168171
return a.release();
169172
}
170173
};
@@ -203,7 +206,8 @@ template <typename T> struct type_caster<IdefixHostArray1D<T>> {
203206
static py::handle cast(const IdefixHostArray1D<T>& src,
204207
py::return_value_policy policy,
205208
py::handle parent) {
206-
py::array_t<real, py::array::c_style> a(src.extent(0),src.data());
209+
py::none dummyDataOwner;
210+
py::array_t<real, py::array::c_style> a(src.extent(0),src.data(),dummyDataOwner);
207211
return a.release();
208212
}
209213
};

test/HD/sod/idefix_py.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ initflow_function initflow
2929
[Output]
3030
vtk 0.1
3131
dmp 0.2
32-
python 0.1 output myfunc
32+
python 0.01

test/HD/sod/pydefix_example.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
import pydefix as pdfx
2+
import numpy as np
3+
import matplotlib.pyplot as plt
14

25
def output(data):
3-
print("data=")
4-
print(data.Vc[0,0,0,:])
5-
data.Vc[0,0,0,10] = 2.0
6+
7+
plt.figure()
8+
plt.plot(data.x[pdfx.IDIR],data.Vc[pdfx.VX1,0,0,:],label='VX1')
9+
plt.plot(data.x[pdfx.IDIR],data.Vc[pdfx.RHO,0,0,:],label='RHO')
10+
plt.legend()
11+
plt.show()
12+
#data.Vc[0,0,0,10] = 2.0
613

714
def initflow(data):
15+
816
# Initialize the flow
9-
print(data.Vc.shape)
10-
data.Vc[0,0,0,:] = 1+0*data.x[0][:]
11-
data.Vc[0,0,0,1] = 1.0
12-
data.Vc[1,0,0,:] = data.x[0][:]
13-
print(data.Vc[0,0,0,:])
14-
print(data.Vc[1,0,0,:])
17+
data.Vc[pdfx.RHO,0,0,:] = 1.0
18+
data.Vc[pdfx.PRS,0,0,:] = 2.0
19+
data.Vc[pdfx.VX1,0,0,:] = np.sin(2.0*np.pi*data.x[0][:])

0 commit comments

Comments
 (0)