Skip to content

Bug: Accessing TreeMesh.cell_centers before finalization produces invalid mesh #293

Open
@santisoler

Description

@santisoler

When creating a TreeMesh, if we access the cell_centers attribute before we finalize the mesh we end up getting an invalid mesh with an empty array for the cell_centers after we do finalize it.

The following example works as expected and a valid mesh is created:

import numpy as np
import discretize

# Minimum cell widths
dx, dy = 5, 5

# Mesh lengths along each direction
x_length, y_length = 300.0, 300.0

# Compute number of base mesh cells required in x and y
nbcx = 2 ** int(np.round(np.log2(x_length / dx)))
nbcy = 2 ** int(np.round(np.log2(y_length / dy)))

# Define the base mesh
hx = [(dx, nbcx)]
hy = [(dy, nbcy)]
mesh = discretize.TreeMesh([hx, hy], x0="CC")

# Refine 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_[discretize.utils.mkvc(xx), discretize.utils.mkvc(yy)]
mesh = discretize.utils.refine_tree_xyz(
    mesh, pts, octree_levels=[2, 2], method="radial", finalize=False
)

mesh.finalize()

print(mesh)
print("\nSize of cell_centers:", mesh.cell_centers.size)
QuadTreeMesh: 4.64% filled

Level : Number of cells               Mesh Extent               Cell Widths    
-----------------------           min     ,     max            min   ,   max   
  2   :        4             ---------------------------   --------------------
  3   :       34          x:    -160.0    ,    160.0          5.0    ,    80.0   
  4   :       36          y:    -160.0    ,    160.0          5.0    ,    80.0   
  5   :       68       
  6   :       48       
-----------------------
Total :       190      

Size of cell_centers: 380

But if we access mesh.cell_centers before running mesh.finalize(), the mesh.cell_centers array is empty, making impossible to use this mesh in any further step (inversion, plotting, etc).

import numpy as np
import discretize

# Minimum cell widths
dx, dy = 5, 5

# Mesh lengths along each direction
x_length, y_length = 300.0, 300.0

# Compute number of base mesh cells required in x and y
nbcx = 2 ** int(np.round(np.log2(x_length / dx)))
nbcy = 2 ** int(np.round(np.log2(y_length / dy)))

# Define the base mesh
hx = [(dx, nbcx)]
hy = [(dy, nbcy)]
mesh = discretize.TreeMesh([hx, hy], x0="CC")

# Refine 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_[discretize.utils.mkvc(xx), discretize.utils.mkvc(yy)]
mesh = discretize.utils.refine_tree_xyz(
    mesh, pts, octree_levels=[2, 2], method="radial", finalize=False
)

print(mesh.cell_centers)
mesh.finalize()

print(mesh)
print("\nSize of cell_centers:", mesh.cell_centers.size)
[]

QuadTreeMesh: 4.64% filled

Level : Number of cells               Mesh Extent               Cell Widths    
-----------------------           min     ,     max            min   ,   max   
  2   :        4             ---------------------------   --------------------
  3   :       34          x:    -160.0    ,    160.0          5.0    ,    80.0   
  4   :       36          y:    -160.0    ,    160.0          5.0    ,    80.0   
  5   :       68       
  6   :       48       
-----------------------
Total :       190      

Size of cell_centers: 0

cc @lheagy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions