Skip to content

Refactor Python elliptic1D example - #522

Open
oscariglesias21 wants to merge 3 commits into
csrc-sdsu:mainfrom
oscariglesias21:python-elliptic-validation
Open

oscariglesias21 wants to merge 3 commits into
csrc-sdsu:mainfrom
oscariglesias21:python-elliptic-validation

Conversation

@oscariglesias21

Copy link
Copy Markdown

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Example
  • Documentation

Description

This pull request is an initial improvement to the Python example python/examples/elliptic1D.py.

The code has been reorganized into separate functions for solving the one-dimensional elliptic problem and plotting the results. I removed unused imports, added a check for the minimum number of cells required by the selected accuracy order, and replaced the explicit matrix inverse with np.linalg.solve().

The example still solves the original Poisson problem with Robin boundary conditions and compares the numerical solution with the analytical solution (u(x)=e^x). The numerical and analytical solutions overlap closely in the resulting plot.

This is an initial work-in-progress contribution. In a later update, I plan to add maximum and discrete (L^2) error calculations, grid refinement, observed convergence rates, and additional validation tests.

Related Issues & Documents

QA Instructions, Screenshots, Recordings

The numerical solution points closely overlap the analytical solution curve attached.
Figure_1
From the root directory of the MOLE repository, install the local Python package if necessary and run:

python python/examples/elliptic1D.py

@valeriabarra
valeriabarra requested review from aboada and cpaolini and removed request for cpaolini November 18, 2026 17:26

@aboada aboada left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great, thanks for the refactor. All my comments are just suggestions.

You may want to use a linspace for plotting the analytical solution so the curve is smooth:

xf = np.linspace(WEST, EAST, 200)
plt.plot(xf, exact_solution(xf), label="Analytical")

© 2008-2024 San Diego State University Research Foundation (SDSURF).
See LICENSE file or https://www.gnu.org/licenses/gpl-3.0.html for details.

One-dimensional elliptic problem with Robin boundary conditions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: it would be great to state the problem definition in this context (BC, rhs, etc.)

Comment thread python/examples/elliptic1D.py Outdated
right_hand_side,
)

analytical_solution = exact_solution(x)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is minor, but the exact solution is computed twice. Compute it once and copy it for the RHS (https://en.wikipedia.org/wiki/Don%27t_repeat_yourself).
Copying the numpy object (https://numpy.org/doc/stable/reference/generated/numpy.copy.html) is faster than recomputing the analytical solution.

analytical_solution = exact_solution(x)
right_hand_side = np.copy(analytical_solution) # need a copy since rhs is modified afterwards
# now you can use/modify right_hand_side

Comment thread python/examples/elliptic1D.py Outdated
boundary_condition = RobinBoundaryCondition(
grid,
accuracy_order=accuracy_order,
dirichlet_coefficient=1.0,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: for consistency, define the Robin coefficients next to the other constants at the top. Something like:

WEST = 0.0
EAST = 1.0
ACCURACY_ORDER = 6
NUM_CELLS = 13
DIRICHLET_COEFFICIENT = 1.0
NEUMANN_COEFFICIENT = 1.0

@cpaolini cpaolini left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree with @aboada ’s np.linspace suggestion to give an improvement for producing a smoother analytical curve. All existing Python tests pass. I approve this PR.

@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@valeriabarra valeriabarra added student project python Pull requests that update python code labels Nov 23, 2026
@oscariglesias21

Copy link
Copy Markdown
Author

Hello, I addressed the suggestions by using np.linspace() for a smoother analytical curve, defining the Robin coefficients as constants, avoiding duplicate computation of the analytical solution, and documenting the problem definition. These changes can be seen in the last commit.

I also completed the validation work described in the issue. The example now calculates the maximum error and discrete $L^2$ error, performs a grid-refinement study, calculates observed convergence rates, and generates error and convergence plots.

I am also attaching images for reference.

The convergence results are:

Grid-refinement convergence study

Cells Spacing Max error Max rate L2 error L2 rate

  13  7.692308e-02      1.225692e-09        6.8914      9.792431e-10        6.8595
  26  3.846154e-02      1.032419e-11        5.0922      8.432921e-12        4.9228
  52  1.923077e-02      3.026468e-13             -      2.780209e-13             -
Figure_2 Figure_1

@cpaolini cpaolini left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I recommend approving PR #522: the latest changes refactor and validate the example and pass execution checks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests that update python code student project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants