From 3fdcc3b8f02b65aef9812b331377be43400e6d17 Mon Sep 17 00:00:00 2001 From: Sylwester Arabas Date: Mon, 21 Oct 2024 20:43:35 +0200 Subject: [PATCH] fix Rust code --- docs/markdown/pympdata_landing.md | 77 ++++++++++++++++--------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/docs/markdown/pympdata_landing.md b/docs/markdown/pympdata_landing.md index 5cdbca22..996c08de 100644 --- a/docs/markdown/pympdata_landing.md +++ b/docs/markdown/pympdata_landing.md @@ -98,11 +98,11 @@ options = Options(pyargs('n_iters', 2)); ```Rust use pyo3::prelude::*; use pyo3::types::{IntoPyDict, PyDict, PyTuple}; -//main fn handle... -Python::with_gil(|py| { - let options_args = [("n_iters", 2)].into_py_dict_bound(py); - let options = py.import_bound("PyMPDATA")?.getattr("Options")?.call((), Some(&options_args))?; -//... + +fn main() -> PyResult<()> { + Python::with_gil(|py| { + let options_args = [("n_iters", 2)].into_py_dict_bound(py); + let options = py.import_bound("PyMPDATA")?.getattr("Options")?.call((), Some(&options_args))?; ``` @@ -253,34 +253,38 @@ advector = VectorField(pyargs(...
Rust code (click to expand) ```Rust -let vector_field = py.import_bound("PyMPDATA")?.getattr("VectorField")?; -let scalar_field = py.import_bound("PyMPDATA")?.getattr("ScalarField")?; -let periodic = py.import_bound("PyMPDATA.boundary_conditions")?.getattr("Periodic")?; + let vector_field = py.import_bound("PyMPDATA")?.getattr("VectorField")?; + let scalar_field = py.import_bound("PyMPDATA")?.getattr("ScalarField")?; + let periodic = py.import_bound("PyMPDATA.boundary_conditions")?.getattr("Periodic")?; -let nx_ny = [24, 24]; -let Cx_Cy = [-0.5, -0.25]; -let boundary_con = PyTuple::new_bound(py, [periodic.call0()?, periodic.call0()?]).into_any(); -let halo = options.getattr("n_halo")?; + let nx_ny = [24, 24]; + let Cx_Cy = [-0.5, -0.25]; + let boundary_con = PyTuple::new_bound(py, [periodic.call0()?, periodic.call0()?]).into_any(); + let halo = options.getattr("n_halo")?; -let indices = PyDict::new_bound(py); - Python::run_bound(py, &format!(r#"import numpy as np + let indices = PyDict::new_bound(py); + Python::run_bound(py, &format!(r#" +import numpy as np nx, ny = {}, {} xi, yi = np.indices((nx, ny), dtype=float) data=np.exp( --(xi+.5-nx/2)**2 / (2*(ny/10)**2) --(yi+.5-nx/2)**2 / (2*(ny/10)**2) -)"#, nx_ny[0], nx_ny[1]), None, Some(&indices)).unwrap(); - -let advectee_arg = vec![("data", indices.get_item("data")?), ("halo", Some(halo.clone())), ("boundary_conditions", Some(boundary_con))].into_py_dict_bound(py); -let advectee = scalar_field.call((), Some(&advectee_arg))?; -let full = PyDict::new_bound(py); -Python::run_bound(py, &format!(r#"import numpy as np + -(xi+.5-nx/2)**2 / (2*(ny/10)**2) + -(yi+.5-nx/2)**2 / (2*(ny/10)**2) +) + "#, nx_ny[0], nx_ny[1]), None, Some(&indices)).unwrap(); + + let advectee_arg = vec![("data", indices.get_item("data")?), ("halo", Some(halo.clone())), ("boundary_conditions", Some(boundary_con))].into_py_dict_bound(py); + let advectee = scalar_field.call((), Some(&advectee_arg))?; + let full = PyDict::new_bound(py); + Python::run_bound(py, &format!(r#" +import numpy as np nx, ny = {}, {} Cx, Cy = {}, {} -data = (np.full((nx + 1, ny), Cx), np.full((nx, ny + 1), Cy))"#, nx_ny[0], nx_ny[1], Cx_Cy[0], Cx_Cy[1]), None, Some(&full)).unwrap(); -let boundary_con = PyTuple::new_bound(py, [periodic.call0()?, periodic.call0()?]).into_any(); -let advector_arg = vec![("data", full.get_item("data")?), ("halo", Some(halo.clone())), ("boundary_conditions", Some(boundary_con))].into_py_dict_bound(py); -let advector = vector_field.call((), Some(&advector_arg))?; +data = (np.full((nx + 1, ny), Cx), np.full((nx, ny + 1), Cy)) + "#, nx_ny[0], nx_ny[1], Cx_Cy[0], Cx_Cy[1]), None, Some(&full)).unwrap(); + let boundary_con = PyTuple::new_bound(py, [periodic.call0()?, periodic.call0()?]).into_any(); + let advector_arg = vec![("data", full.get_item("data")?), ("halo", Some(halo.clone())), ("boundary_conditions", Some(boundary_con))].into_py_dict_bound(py); + let advector = vector_field.call((), Some(&advector_arg))?; ```
@@ -359,9 +363,8 @@ stepper = Stepper(pyargs(... Rust code (click to expand) ```Rust -let stepper_arg = vec![("options", options), ("grid", PyTuple::new_bound(py, nx_ny).into_any())].into_py_dict_bound(py); - -let stepper = stepper_.call((), Some(&stepper_arg))?; + let stepper_arg = vec![("options", options), ("grid", PyTuple::new_bound(py, nx_ny).into_any())].into_py_dict_bound(py); + let stepper = stepper_.call((), Some(&stepper_arg))?; ``` @@ -396,7 +399,7 @@ stepper = Stepper(pyargs(... Rust code (click to expand) ```Rust -let stepper_arg = vec![("options", options),("n_dims", 2)].into_py_dict_bound(py) + let stepper_arg = vec![("options", options),("n_dims", 2)].into_py_dict_bound(py) ``` @@ -479,12 +482,14 @@ state = solver.advectee.get(); Rust code (click to expand) ```Rust -let solver_ = py.import_bound("PyMPDATA")?.getattr("Solver")?; -let mut solver = solver_.call((), Some(&vec![("stepper", stepper), ("advectee", advectee), ("advector", advector)].into_py_dict_bound(py)))?; -let state_0 = solver.getattr("advectee")?.getattr("get")?.call0()?.getattr("copy")?.call0()?; -solver.getattr("advance")?.call((), Some(&vec![("n_steps", 75)].into_py_dict_bound(py)))?; -let state = solver.getattr("advectee")?.getattr("get")?.call0()?; -Ok(())}} + let solver_ = py.import_bound("PyMPDATA")?.getattr("Solver")?; + let mut solver = solver_.call((), Some(&vec![("stepper", stepper), ("advectee", advectee), ("advector", advector)].into_py_dict_bound(py)))?; + let state_0 = solver.getattr("advectee")?.getattr("get")?.call0()?.getattr("copy")?.call0()?; + solver.getattr("advance")?.call((), Some(&vec![("n_steps", 75)].into_py_dict_bound(py)))?; + let state = solver.getattr("advectee")?.getattr("get")?.call0()?; + Ok(()) + } +} ```