Repository navigation
Refactor Python elliptic1D example - #522
oscariglesias21 wants to merge 1 commit into
Conversation
aboada
left a comment
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
nit: it would be great to state the problem definition in this context (BC, rhs, etc.)
| right_hand_side, | ||
| ) | ||
|
|
||
| analytical_solution = exact_solution(x) |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
What type of PR is this? (check all applicable)
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.

From the root directory of the MOLE repository, install the local Python package if necessary and run: