Skip to content

Commit

Permalink
Merge pull request #485 from llaniewski/feature/runpython
Browse files Browse the repository at this point in the history
Some fixes for `RunPython`
  • Loading branch information
llaniewski authored Jan 10, 2024
2 parents 36c4007 + 614f37d commit f7f36f7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 99 deletions.
62 changes: 62 additions & 0 deletions example/python/karman_vtk.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0"?>
<CLBConfig version="2.0" output="output/" permissive="true">
<Geometry nx="1024" ny="100">
<MRT>
<Box/>
</MRT>
<WVelocity name="Inlet">
<Inlet/>
</WVelocity>
<EPressure name="Outlet">
<Outlet/>
</EPressure>
<Inlet nx="1" dx="5">
<Box/>
</Inlet>
<Outlet nx="1" dx="-5">
<Box/>
</Outlet>
<Wall mask="ALL">
<Channel/>
<Wedge dx="120" nx="20" dy="50" ny="20" direction="LowerRight"/>
<Wedge dx="120" nx="20" dy="30" ny="20" direction="UpperRight"/>
<Wedge dx="140" nx="20" dy="50" ny="20" direction="LowerLeft"/>
<Wedge dx="140" nx="20" dy="30" ny="20" direction="UpperLeft"/>
</Wall>
</Geometry>
<Model>
<Param name="VelocityX" value="0.01"/>
<Param name="Viscosity" value="0.02"/>
<Param name="Smag" value="0.16"/>
<Param name="PressDiffInObj" value="1"/>
<Param name="EOSScale" value="0.05"/>
<Param name="Tension" value="0.01"/>
<Param name="Coriolis" value="0.001"/>
<Param name="SolidAlfa" value="0.166"/>
<Param name="FluidAlfa" value="0.01"/>
<Param name="InitTemperature" value="0"/>
<Param name="InletTemperature" value="1"/>
</Model>
<RunPython>
import vtk;
from vtk.util import numpy_support
</RunPython>
<RunPython Iterations="1000">
img = vtk.vtkImageData()
tab = Solver.Geometry.X
img.SetDimensions(tab.shape[0]+1, tab.shape[1]+1, tab.shape[2]+1)

for n,tab in Solver.Quantities:
vtk_data = numpy_support.numpy_to_vtk(num_array=tab.reshape(-1,order='F'))
vtk_data.SetName(n)
if len(tab.shape) == 4:
vtk_data.SetNumberOfComponents(tab.shape[0])
img.GetCellData().AddArray(vtk_data)

writer = vtk.vtkXMLImageDataWriter()
writer.SetFileName("test.vti")
writer.SetInputData(img)
writer.Update()
</RunPython>
<Solve Iterations="10000"/>
</CLBConfig>
53 changes: 0 additions & 53 deletions example/python/pythonGeom.xml

This file was deleted.

10 changes: 0 additions & 10 deletions example/python/runexternal.py

This file was deleted.

34 changes: 0 additions & 34 deletions example/python/runexternal.xml

This file was deleted.

16 changes: 14 additions & 2 deletions src/Handlers/cbRunR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class rQuantities : public rWrapper {
Rcpp::NumericVector ret;
bool si = false;
std::string quant = name;
size_t last_index = name.find_last_not_of(".");
size_t last_index = name.find_last_of(".");
if (last_index != std::string::npos) {
std::string result = name.substr(last_index + 1);
if (result == "si") {
Expand Down Expand Up @@ -514,6 +514,7 @@ class rSolver : public rWrapper {
ret.push_back("Globals");
ret.push_back("Actions");
ret.push_back("Geometry");
ret.push_back("Info");
return ret;
}
};
Expand Down Expand Up @@ -696,13 +697,21 @@ namespace RunPython {
}

void initializePy() {
if (py_initialised) return;
RInside& R = RunR::GetR();
has_reticulate = R.parseEval("require(reticulate, quietly=TRUE)");
if (!has_reticulate) throw std::string("Tried to call Python, but no reticulate installed");
py_initialised = true;
R.parseEval(
"py_names = function(obj) names(obj) \n"
"py_element = function(obj, name) `[[`(obj,name) \n"
"py_element = function(obj, name) { \n"
" ret = `[[`(obj,name) \n"
" if (is.factor(ret)) { \n"
" as.integer(ret) - 1L \n"
" } else { \n"
" ret \n"
" } \n"
"} \n"
"py_element_assign = function(obj, name, value) `[[<-`(obj,name,value) \n"
"r_to_py.CLB = function(x, convert=FALSE) py$S3(reticulate:::py_capsule(x))\n"
);
Expand All @@ -714,6 +723,9 @@ namespace RunPython {
" return r.print(self.obj) \n"
" def __dir__(self): \n"
" return r.py_names(self.obj) \n"
" def __iter__(self): \n"
" for n in r.py_names(self.obj): \n"
" yield n, r.py_element(self.obj, n) \n"
" def __getattr__(self, index): \n"
" if index.startswith('_'): \n"
" return None \n"
Expand Down

0 comments on commit f7f36f7

Please sign in to comment.