Skip to content

Refactor Python elliptic1D example - #522

Open
oscariglesias21 wants to merge 1 commit into
csrc-sdsu:mainfrom
oscariglesias21:python-elliptic-validation
Open

oscariglesias21 wants to merge 1 commit 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.)

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

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!

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants