# Using mesh\_builder\_xyz

**URL:** <https://simpeg.discourse.group/t/using-mesh-builder-xyz/66>\
**Category:** Discretization\
**Created:** [February 10, 2020, 11:09pm UTC](https://simpeg.discourse.group/t/using-mesh-builder-xyz/66 "2020-02-10T23:09:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RadhikaF](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@RadhikaF](https://simpeg.discourse.group/u/RadhikaF)\
**Post date:** [February 10, 2020, 11:09pm UTC](https://simpeg.discourse.group/t/using-mesh-builder-xyz/66/1 "2020-02-10T23:09:31Z")

</div>

Hello everyone,

I have just started familiarising myself with SimPEG and looking at its FDEM forward modelling capabilities. I have been exploring examples that use a cylindrical mesh for such problems (thank you for those!). I am now looking at using tree meshes because it seems to provide greater flexibility in mesh structure. I came across the function ‘mesh\_builder\_xyz’ in the discretize utils package, which builds a tensor mesh based on the input points. When I change the mesh\_type to ‘tree’, it throws an error. I realised this error occurred because the resulting mesh has no cell centres or cell faces (when printing `mesh.nC` and `mesh.nF`). Would anyone be able to shed some light on what the reason could be?

Thank you!

### Code:

```auto
import discretize
import matplotlib.pyplot as plt
import numpy as np

xyLoc = np.random.randn(8,2)

mesh = discretize.utils.meshutils.mesh_builder_xyz(
    xyLoc, [0.1, 0.1], depth_core=0.5,
    padding_distance=[[1,2], [1,0]],
    mesh_type='tree',
)
print(mesh.nC,mesh.nF)

axs = plt.subplot()
mesh.plotImage(mesh.vol, grid=True, ax=axs)
axs.scatter(xyLoc[:,0], xyLoc[:,1], 15, c='w', zorder=3)
axs.set_aspect('equal')
plt.show()

```

Kind regards,

Radhika

---

<div class="post-metadata">

**Author:** ![thibaut.astic](https://yyz2.discourse-cdn.com/free1/user_avatar/simpeg.discourse.group/thibaut.astic/32/109_2.png) [@thibaut.astic](https://simpeg.discourse.group/u/thibaut.astic)\
**Post date:** [February 18, 2020, 6:23pm UTC](https://simpeg.discourse.group/t/using-mesh-builder-xyz/66/2 "2020-02-18T18:23:17Z")

</div>

I do not know about the bahavior of the `mesh_builder_xyz` with `TreeMesh`. But if you have not already, you can check the tutorials on Mesh Generation available in the documentation for help: [http://discretize.simpeg.xyz/en/master/tutorials/mesh\_generation/index.html](http://discretize.simpeg.xyz/en/master/tutorials/mesh_generation/index.html)

---

<div class="post-metadata">

**Author:** ![domfournier](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@domfournier](https://simpeg.discourse.group/u/domfournier)\
**Post date:** [February 19, 2020, 7:08pm UTC](https://simpeg.discourse.group/t/using-mesh-builder-xyz/66/3 "2020-02-19T19:08:19Z")

</div>

Hi @RadhikaF,

Sorry for the late reply.  
Tree meshes require to be refined in order to have cells.  
You either refine globally using the `mesh.refine(int(value))`, or with more advanced options using: [http://discretize.simpeg.xyz/en/master/api/generated/discretize.utils.refine\_tree\_xyz.html?highlight=refine\_tree\_xyz#discretize-utils-refine-tree-xyz](http://discretize.simpeg.xyz/en/master/api/generated/discretize.utils.refine_tree_xyz.html?highlight=refine_tree_xyz#discretize-utils-refine-tree-xyz)

Here is your example with the refine call added:

```
xyLoc = np.random.randn(8,2)

mesh = discretize.utils.meshutils.mesh_builder_xyz(
    xyLoc, [0.1, 0.1], depth_core=0.5,
    padding_distance=[[1,2], [1,0]],
    mesh_type='tree',
)

mesh = discretize.utils.refine_tree_xyz(mesh, xyLoc, finalize=True)

print(mesh.nC,mesh.nF)

axs = plt.subplot()
mesh.plotImage(mesh.vol, grid=True, ax=axs)
axs.scatter(xyLoc[:,0], xyLoc[:,1], 15, c='w', zorder=3)
axs.set_aspect('equal')
plt.show() 

```

![Figure_1](https://global.discourse-cdn.com/free1/uploads/simpeg/original/1X/8dd98294c4718d1b135d0db441050c655ec71535.png)

!! IMPORTANT !!  
You can refine the mesh mulitple times, but the `finalize` call must only be called ONCE at the end.

Best.

---

<div class="post-metadata">

**Author:** ![RadhikaF](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@RadhikaF](https://simpeg.discourse.group/u/RadhikaF)\
**Post date:** [February 27, 2020, 11:21pm UTC](https://simpeg.discourse.group/t/using-mesh-builder-xyz/66/4 "2020-02-27T23:21:25Z")

</div>

Thank you very much @domfournier and @thibaut.astic!

I wasn’t aware of that requirement so thank you for the clarification. Also, apologies for the late reply; I have been away for the past couple of weeks.

Best regards,

Radhika
