-
Notifications
You must be signed in to change notification settings - Fork 35
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
Interacting with TreeMesh
object before finalization causes __repr__
and _repr_html_
to later error
#392
Comments
Hi @williamjsdavis, thanks for opening this Issue. I couldn't reproduce the error with the scripts you provided, but I suspect what issue you are trying to report. Trying to print a TreeMesh before it gets finalized raises an error. For example: from discretize import TreeMesh
from discretize.utils import mkvc
import numpy as np
dx = 5 # minimum cell width (base mesh cell width) in x
dy = 5 # minimum cell width (base mesh cell width) in y
x_length = 300.0 # domain width in x
y_length = 300.0 # domain width in y
# Compute number of base mesh cells required in x and y
nbcx = 2 ** int(np.round(np.log(x_length / dx) / np.log(2.0)))
nbcy = 2 ** int(np.round(np.log(y_length / dy) / np.log(2.0)))
# Define the base mesh
hx = [(dx, nbcx)]
hy = [(dy, nbcy)]
mesh = TreeMesh([hx, hy], x0="CC")
# Print the TreeMesh before we finilize it
print(mesh)
Is this the behavior you intended to report? If so, I think the |
I just double checked running my code and I reproduced my original error and working case. I was running these code blocks in jupyter notebook cells; I don't know if that makes a difference? Is there any additional information I can provide to help you reproduce my error? I was able to reproduce your example error. It is related to the behavior I was intended to report. The difference is that, in my example, even just printing |
Thanks for the swift reply. I opened a #392 to fix the error I described: in that PR the I can reproduce the error when I run your script in a Jupyter Notebook. I think the problem is not coming from the from discretize import TreeMesh
from discretize.utils import mkvc
import numpy as np
dx = 5 # minimum cell width (base mesh cell width) in x
dy = 5 # minimum cell width (base mesh cell width) in y
x_length = 300.0 # domain width in x
y_length = 300.0 # domain width in y
# Compute number of base mesh cells required in x and y
nbcx = 2 ** int(np.round(np.log(x_length / dx) / np.log(2.0)))
nbcy = 2 ** int(np.round(np.log(y_length / dy) / np.log(2.0)))
# Define the base mesh
hx = [(dx, nbcx)]
hy = [(dy, nbcy)]
mesh = TreeMesh([hx, hy], x0="CC")
# Access h_gridded before finalizing
mesh.h_gridded
# Refine surface topography
xx = mesh.nodes_x
yy = -3 * np.exp((xx**2) / 100**2) + 50.0
pts = np.c_[mkvc(xx), mkvc(yy)]
padding = [[0, 2], [0, 2]]
mesh.refine_surface(pts, padding_cells_by_level=padding, finalize=False)
# Refine mesh near points
xx = np.array([0.0, 10.0, 0.0, -10.0])
yy = np.array([-20.0, -10.0, 0.0, -10])
pts = np.c_[mkvc(xx), mkvc(yy)]
mesh.refine_points(pts, padding_cells_by_level=[2, 2], finalize=False)
mesh.finalize()
# h_gridded is empty even after finalizing the mesh
print(f"{mesh.h_gridded=}") Even after finalizing the mesh, the
I think the discretize/discretize/_extensions/tree_ext.pyx Lines 2058 to 2086 in 60f3af1
Maybe we could return an empty array and not cache anything if the mesh is not finalized. cc @jcapriot |
Related to #293 We need to add guards on most |
@santisoler Yes this is the erroring behavior that I was experiencing, thanks for opening that PR! |
This is some odd behavior I encountered when interacting with a
TreeMesh
object before finalizing it.Working example
The code here is modified from the documentation: link
The final display of
mesh
gives the following output:Erroring example
Running the exact same code, with the print statement not commented out, i.e.,
Causes the following error:
Full stacktrace:
Additional comments
The same behavior happens with the
_repr_html_
method ofTreeMesh
.The text was updated successfully, but these errors were encountered: