CFD-GO / TCLB

TCLB - Templated MPI+CUDA/CPU Lattice Boltzmann code
https://tclb.io
GNU General Public License v3.0
177 stars 70 forks source link

RunPython #468

Closed llaniewski closed 8 months ago

llaniewski commented 8 months ago

This PR extends the RunR capabilities with Python execution through the reticulate. You can now execute Python code with <RunPython/> element, where Solver is exposed in the same way as in R.

Example

<?xml version="1.0"?>
<CLBConfig version="2.0" output="output/">
    <Geometry nx="1024" ny="100">
        <MRT><Box/></MRT>
        <WVelocity><Box nx="1"/></WVelocity>
        <EPressure><Box dx="-1"/></EPressure>
    </Geometry>
    <Model>
        <Param name="VelocityX" value="0.05"/>
        <Param name="Viscosity" value="0.01"/>
    </Model>
    <RunR Iterations="100">
        print(sum(Solver$Quantities$Rho))
    </RunR>
    <RunPython Iterations="100">
print(Solver.Quantities.Rho.sum())
    </RunPython>
    <Solve Iterations="1000"/>
</CLBConfig>

FYI: @TravisMitchell @shkodm @mdzik I think with this present in the code we can get rid of the native Python support.

TravisMitchell commented 8 months ago

I was already okay with removing native Python support but this also looks nice :).

codecov-commenter commented 8 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (4e28d4e) 44.30% compared to head (e096d7d) 44.30%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #468 +/- ## ======================================= Coverage 44.30% 44.30% ======================================= Files 169 169 Lines 7896 7896 ======================================= Hits 3498 3498 Misses 4398 4398 ``` | [Flag](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | Coverage Δ | | |---|---|---| | [d2q9](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | `30.39% <100.00%> (ø)` | | | [d2q9_bc](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | `26.68% <100.00%> (ø)` | | | [d2q9_kuper](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | `27.46% <100.00%> (ø)` | | | [d3q27_PSM_NEBB](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | `36.00% <100.00%> (ø)` | | | [d3q27_pf_velocity](https://app.codecov.io/gh/CFD-GO/TCLB/pull/468/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO) | `30.46% <100.00%> (ø)` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CFD-GO#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

shkodm commented 8 months ago

Very exciting, thanks @llaniewski ! Do you want me to test it using the two-phase fracture setup I was working on?

TravisMitchell commented 8 months ago

@shkodm I think I would like you to test on the fracture setup, and maybe add to the multiphase tools repo as a demo for using it ;)

llaniewski commented 8 months ago

@shkodm That would be great. Hope this will simplify your setup a bit. edit: Of course, after you come back to the office.

@TravisMitchell @shkodm Some notes:

  1. Seems that the read-only part works quite well, but setting stuff back in the Solver object doesn't really work well (eg. Solver.Geometry.BOUNDARY[0,0] = "None"). I think this would mean much more effort.
  2. R objects are auto converted to python. Matrices become numpy arrays, so that's good. But the factor arrays seem to be converted to vectors of strings.
shkodm commented 8 months ago

@llaniewski I tested, and it seems to work for my purposes now (plotting phase field on the aperture plot). We can probably merge this one directly now and then create follow up PRs if I find some issues.

Regarding notes:

  1. I don't think I need it right now, but setting stuff back would be cool. Maybe it is possible to do it in the follow up PR?
  2. Factor array converted to strings is actually good, because of https://github.com/CFD-GO/TCLB/issues/470