Skip to content

Commit

Permalink
fix Rust code
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo authored Oct 21, 2024
1 parent d0b6f78 commit 3fdcc3b
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions docs/markdown/pympdata_landing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
```
</details>

Expand Down Expand Up @@ -253,34 +253,38 @@ advector = VectorField(pyargs(...
<details>
<summary>Rust code (click to expand)</summary>
```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))?;
```
</details>

Expand Down Expand Up @@ -359,9 +363,8 @@ stepper = Stepper(pyargs(...
<summary>Rust code (click to expand)</summary>

```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))?;
```
</details>

Expand Down Expand Up @@ -396,7 +399,7 @@ stepper = Stepper(pyargs(...
<summary>Rust code (click to expand)</summary>

```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)
```
</details>

Expand Down Expand Up @@ -479,12 +482,14 @@ state = solver.advectee.get();
<summary>Rust code (click to expand)</summary>

```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(())
}
}
```
</details>

Expand Down

0 comments on commit 3fdcc3b

Please sign in to comment.