Skip to content

[WIP] Add Python code for couple your code section #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kss682
Copy link
Collaborator

@kss682 kss682 commented Apr 25, 2025

This PR implements the discussions of #211.

Add Python code examples for the couple your code section using nav tabs.

@kss682 kss682 force-pushed the extend-codes-with-tabs branch from f5e0cac to 80aabc0 Compare April 27, 2025 22:07
@kss682
Copy link
Collaborator Author

kss682 commented Apr 27, 2025

@uekerman
Could you please check if the python doc until meshes are as expected ?

Copy link
Member

@uekerman uekerman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in principle. No need to also cover inline code. The code blocks are enough. And not need to bring the docstrings.

Be careful with details. The code should really run. Maybe test with a dummy on the side to be completely sure.

Where do the css changes come from? I am not saying that they don't make sense, I only want to understand better. Could you maybe add screenshots before and after?

I did not try to build the website.

You also pushed to #211. No need to update both. You could close #211 in favor of this one.

Comment on lines +29 to +53
```python
"""
Parameters:
participant_name: string
Name of the solver
configuration_file_name: string
Name of preCICE config file
rank: int
Rank of the process
size: int
Size of the process
"""
participant = Participant(participant_name, configuration_file_name, rank, size)

participant.initialize()

"""
Parameters:
computed_timestep_size: double
Length of timestep used by solver
"""
participant.advance(computed_timestep_size)

participant.finalize()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the docstring here. This should not replace or copy the API documentation. The meaning of the functions is explained in the text. And we don't need a full code example. This one we have below anyway.

Comment on lines +104 to +125
```python
import precice

turn_on_solver() # e.g. setup and partition mesh

precice = precice.Interface(
"FluidSolver", "precice-config.xml", rank, size
)
precice_dt = precice.initialize()

u = initialize_solution()

while t < t_end: # time loop
dt = compute_adaptive_dt()
dt = min(precice_dt, dt) # more about this in Step 5
u = solve_time_step(dt, u) # returns new solution
precice_dt = precice.advance(dt)
t = t + dt

precice.finalize() # frees data structures and closes communication channels

```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still preCICE v2 (from when the original PR #211 was opened)

Comment on lines +134 to +135
* `set_mesh_vertex` defines the coordinates of a single mesh vertex and returns a vertex ID, which you can use to refer to this vertex.
* `set_mesh_vertices` defines multiple vertices at once. So, you can use this function instead of calling `set_mesh_vertex` multiple times. This is also good practice for performance reasons.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's better not change these inline function names. At least for now. This will get too complicated. Only changing the code blocks is enough. Meaning tabs for each code block here.

Comment on lines +102 to +115
"""
Parameters
----------
mesh_name : str
Name of the mesh to add the vertex to.
position : array_like
The coordinates of the vertex.

Returns
-------
vertex_id : int
ID of the vertex which is set.
"""
set_mesh_vertex(mesh_name, position)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the docstring here, but please add the return value to the call.

Suggested change
"""
Parameters
----------
mesh_name : str
Name of the mesh to add the vertex to.
position : array_like
The coordinates of the vertex.
Returns
-------
vertex_id : int
ID of the vertex which is set.
"""
set_mesh_vertex(mesh_name, position)
vertex_id = set_mesh_vertex(mesh_name, position)

Comment on lines +117 to +131
"""
Parameters
----------
mesh_name : str
Name of the mesh to add the vertices to.
positions : array_like
The coordinates of the vertices in a numpy array [N x D] where
N = number of vertices and D = dimensions of geometry.

Returns
-------
vertex_ids : numpy.ndarray
IDs of the created vertices.
"""
set_mesh_vertices(mesh_name, positions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Comment on lines +140 to +152
"""
Parameters
----------
mesh_name : str
name of the mesh to write to.
data_name : str
Data name to write to.
vertex_ids : array_like
Indices of the vertices.
values : array_like
Values of data
"""
write_data(self, mesh_name, data_name, vertex_ids, values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
Parameters
----------
mesh_name : str
name of the mesh to write to.
data_name : str
Data name to write to.
vertex_ids : array_like
Indices of the vertices.
values : array_like
Values of data
"""
write_data(self, mesh_name, data_name, vertex_ids, values)
write_data(mesh_name, data_name, vertex_ids, values)


```python
turn_on_solver() # e.g. setup and partition mesh
num_vertices = 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the example really consistent with the CPP code. There this "3" is not used, for example.


mesh_dim = precice.get_mesh_dimensions("FluidMesh")

vertices = np.zeros((num_vertices, participant.get_mesh_dimensions(mesh_name))) # coords of vertices at wet surface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you double-check that the order of dimensions is correct in such calls? I am not saying that it is wrong, but I did not test it 😄

displacements_dim = precice.get_data_dimensions("FluidMesh", "Displacements")
displacements = np.zeros((num_vertices, displacements_dim))


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Let's avoid unnecessary empty lines.

precice_dt = precice.get_max_time_step_size()
solver_dt = begin_time_step() # e.g. compute adaptive dt
dt = min(precice_dt, solver_dt)
precice.read_data("FluidMesh", "Displacements", vertex_ids, dt, displacements)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
precice.read_data("FluidMesh", "Displacements", vertex_ids, dt, displacements)
displacements = precice.read_data("FluidMesh", "Displacements", vertex_ids, dt)

no?

@kss682
Copy link
Collaborator Author

kss682 commented Apr 29, 2025

The css changes are from one of the commits from the original PR.
The css adds a box like feel and a background color for the nav tabs.
Curr
image

New
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants