# Tutorial on 3D geometries

**URL:** <https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140>\
**Category:** dolfinx\
**Created:** [November 22, 2021, 11:00am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140 "2021-11-22T11:00:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [November 22, 2021, 11:00am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/1 "2021-11-22T11:00:11Z")

</div>

Dear all,

Does anybody know about any tutorial/examples in 3D problems?  
I am especially interested in learning how to impose the boundary conditions when the problem is 3D.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 22, 2021, 11:59am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/2 "2021-11-22T11:59:24Z")

</div>

I have made a tutorial for DOLFINx, which includes 3D problems such as: [Hyperelasticity — FEniCSx tutorial](https://jorgensd.github.io/dolfinx-tutorial/chapter2/hyperelasticity.html)  
and  
[Using the GMSH Python API to generate complex meshes | Jørgen S. Dokken](https://jsdokken.com/converted_files/tutorial_gmsh.html)  
If you are interested in old dolfin, the documentation features some 3D examples as well:  
[https://fenicsproject.org/olddocs/dolfin/latest/python/demos/hyperelasticity/demo\_hyperelasticity.py.html](https://fenicsproject.org/olddocs/dolfin/latest/python/demos/hyperelasticity/demo_hyperelasticity.py.html)

There is no fundamental difference between enforcing boundary conditions on a 2D or 3D geometry in DOLFIN/DOLFINx

---

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [November 22, 2021, 12:41pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/3 "2021-11-22T12:41:44Z")

</div>

That’s great. Thank you very much for the speedy response!

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 7, 2023, 10:36am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/4 "2023-01-07T10:36:33Z")

</div>

Hi there,

@dokken yet old dolfin can make geometries per the tutorial ? I’ve tried to make a simple 3D domain, though no success so far.

> import dolfinx  
> box = Box(0, 0, 0, 9, 9, 3)  
> g3d = box  
> info(g3d, True)  
> print(g3d, ‘3D geometry’)

it gets “name Box is not defined”, so no such key words are available for dolfinx that were valid for dolfin?

Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 7, 2023, 11:57am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/5 "2023-01-07T11:57:36Z")

</div>

First of all, by not using a wildcard import (`from somemodule import *`) you need to call `somemodule.Mesh` to access the Mesh class from that module.

Secondly, the api in DOLFINx is quite different than DOLFIN. See:  
[https://docs.fenicsproject.org/dolfinx/v0.5.1/python/demos.html#demos](https://docs.fenicsproject.org/dolfinx/v0.5.1/python/demos.html#demos)  
and  
[https://jsdokken.com/dolfinx-tutorial/](https://jsdokken.com/dolfinx-tutorial/)  
for an introduction to the new API

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 7, 2023, 12:18pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/6 "2023-01-07T12:18:53Z")

</div>

Many thanks.

> msh = mesh.create\_rectangle(  
> comm = MPI.COMM\_WORLD,  
> points = ((0,0), (2,1)),  
> n = (31, 16),  
> cell\_type = mesh.CellType.triangle,)

When I want to plot(msh), then it gets object is not callable.  
Is this because under WSL there is no apt to show it up?

Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 7, 2023, 12:58pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/7 "2023-01-07T12:58:10Z")

</div>

As you have not supplied what script you are running, (i.e. where you have gotten the `plot` command from), I cannot really help you. For WSL plotting issues see for instance: [Problem about pyvista - #2 by dokken](https://fenicsproject.discourse.group/t/problem-about-pyvista/8912/2)

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 7, 2023, 3:06pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/8 "2023-01-07T15:06:23Z")

</div>

> from dolfinx import fem, io, mesh, plot  
> from mpi4py import MPI
> 
> import numpy as np
> 
> msh = mesh.create\_rectangle(comm = MPI.COMM\_WORLD,  
> points = ((0,0), (2,1)),  
> n = (31, 16),  
> cell\_type = mesh.CellType.triangle,)  
> plot(msh)

I saw that some users get the pictures with no X-server help, even under WSL, which seems to be convenient, I’m recalling all things about to set up X-server under WSL, and it’s some sort of magic.

Do you mean there should be no plot command there above?

Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 7, 2023, 3:23pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/9 "2023-01-07T15:23:09Z")

</div>

> [@E\_P](#):
>
> plot(msh)

Please read the tutorials, as they show you how to plot meshes and functions.

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 7, 2023, 4:18pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/10 "2023-01-07T16:18:45Z")

</div>

Dr. @dokken many thanks, yes, reviewed the tutorial and came up with the following:

> msh = mesh.create\_box (comm = MPI.COMM\_WORLD,  
> points = ((0,0,0), (9,9,3),  
> n = (36,36,18),  
> cell\_type = mesh.CellType.tetrahedron,)

> with io.XDMFFile (msh.comm, ‘text.xdmf’, ‘w’) as file:  
> file.write\_mesh(msh)

this way I get the file in home directory of Ubuntu 22.04.1 LTS under WSL (Windows 10), while having ParaView on Windows 10, and there I can see the file.

Also one more question - what if I would do some manipulations with the box, for instance, to make some series of holes there, then how can I do so ?

Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 8, 2023, 9:47am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/11 "2023-01-08T09:47:48Z")

</div>

> [@E\_P](#):
>
> Also one more question - what if I would do some manipulations with the box, for instance, to make some series of holes there, then how can I do so ?

You should use external software for such, for instance the gmsh python api, as used in the tutorials  
[https://jsdokken.com/dolfinx-tutorial/chapter2/ns\_code2.html#mesh-generation](https://jsdokken.com/dolfinx-tutorial/chapter2/ns_code2.html#mesh-generation)  
or

> **[Using the GMSH Python API to generate complex meshes](http://jsdokken.com/src/tutorial_gmsh.html)**
>
> Personal webpage

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 8, 2023, 12:57pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/12 "2023-01-08T12:57:06Z")

</div>

@dokken many thanks. As I wrote above, I got the simplest domain - need to upload the picture to visualize my words hereto. For my task this would be a channel where compressible gas flows (one inlet boundary and four outlet boundaries); when I see my domain, my understanding – the entire volume is the domain where I want to simulate compressible gas, and the only thing - is to formulate inlet boundary and outlet boundaries, they might be circle form, for instance.

 ![2023-1-7-box-mesh-slice](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/80ea298ecf6aa8e01f46912083a95b4801ec184f.jpeg)  
 ![2023-1-7-box-mesh](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/8f4eab5ae5fb5c268922ce9edc21b7ab155be94e.jpeg)

Should be it done prior to in the channel geometry stage - for instance, by making a circle shaped hole on the surface and then applying sufficient boundary conditions afterwards?

or perhaps I can formulate such boundary conditions on this channel (already consisting of n amount of tetrahedrons)? and no need to make holes at the geometry stage?

Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 8, 2023, 1:23pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/13 "2023-01-08T13:23:44Z")

</div>

You should probably resolve the inlets and outlets in the mesh/geometry creation, as the boundaries between inlet/outlet and walls will not be smooth otherwise.

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 8, 2023, 3:28pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/14 "2023-01-08T15:28:59Z")

</div>

@dokken many thanks. I thought that outlets / inlets could be determined via nodes of those finite elements that are belong to corresponding boundary (outlets / inlets) ? And probably what you mentioned above - is to determine those regions at the geometry (to explicitly specify those regions) or at mesh stage (when again to determine required nodes)? Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 8, 2023, 3:46pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/15 "2023-01-08T15:46:09Z")

</div>

> [@E\_P](#):
>
> nodes of those finite elements that are belong to corresponding boundary (outlets / inlets) ?

You could do that, but it will not be a smooth representation of the inlet/outlet.

You can look at this in 2D by trying to mark a circle on a unit square mesh.  
See for instance [Plotting subdomains/internal boundaries - #2 by dokken](https://fenicsproject.discourse.group/t/plotting-subdomains-internal-boundaries/6011/2)  
or  
[https://github.com/FEniCS/dolfinx/blob/main/python/demo/demo\_pyvista.py#L95](https://github.com/FEniCS/dolfinx/blob/main/python/demo/demo_pyvista.py#L95)  
or  
[Defining subdomains for different materials — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html#subdomains-defined-from-external-mesh-data)

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 9, 2023, 6:51pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/16 "2023-01-09T18:51:51Z")

</div>

@dokken thanks a lot. One more thing needs your attention:

> import gmsh  
> gmsh.initialize()  
> channel = gmsh.model.occ.addBox(0,0,0,9,3,3)  
> inlet = gmsh.model.occ.addCircle(1.5,1.5,0,0.75)  
> domain = gmsh.model.occ.cut( [(3, channel)], [(2, inlet)] )  
> gmsh.model.occ.synchronize()  
> gmsh.write(‘domain.msh’)

It gets Unknown OpenCASCADE entity of dimension 2 with tag 13  
It looks like I formulated Circle in wrong way, though the idea was to create a channel, then to make inlet (Circle shaped only in plane meaning gdim = 2).

Please advise what’s wrong. Thanks,

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 9, 2023, 7:00pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/17 "2023-01-09T19:00:45Z")

</div>

You do not want to cut out the circle, you want it to be meshed as part of the boundary. Probably you need to use the fragment command. See for instance [Mesh, Submesh , Torus, Boundary - #5 by dokken](https://fenicsproject.discourse.group/t/mesh-submesh-torus-boundary/6378/5)  
or  
[https://bthierry.pages.math.cnrs.fr/tutorial/gmsh/occ/basics/#boolean-operations-cad-like-a-boss-\_](https://bthierry.pages.math.cnrs.fr/tutorial/gmsh/occ/basics/#boolean-operations-cad-like-a-boss-_)  
or the Gmsh documentation.

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [January 10, 2023, 3:57am UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/18 "2023-01-10T03:57:18Z")

</div>

I don’t want to cut out the circle, I want it to be meshed in a special way (as a boundary) 😉 though would you provide me more, let me say, explicit guide ? Thanks,

---

<div class="post-metadata">

**Author:** ![E\_P](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/e_p/32/1356_2.png) [@E\_P](https://fenicsproject.discourse.group/u/E_P)\
**Post date:** [November 3, 2024, 5:13pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/19 "2024-11-03T17:13:48Z")

</div>

Hello all,

It seems like many new things happened to Fenicsx vs Fenics.

Once we created domain by

domain = mesh.create\_unit\_square(MPI.COMM\_WORLD, 8, 8, mesh.CellType.quadrilateral)

is there any easy way to visualize the mesh by using pyvista ? right away after mesh create stage.

Before it was the xdmf file generated out of the script & heavy paraview tool.

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [November 3, 2024, 8:19pm UTC](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140/20 "2024-11-03T20:19:30Z")

</div>

There are plenty of tutorials that shows this, including:  
[https://docs.fenicsproject.org/dolfinx/v0.9.0/python/demos/demo\_pyvista.html](https://docs.fenicsproject.org/dolfinx/v0.9.0/python/demos/demo_pyvista.html)  
[https://jsdokken.com/FEniCS-workshop/src/mesh\_generation.html#visualizing-the-mesh](https://jsdokken.com/FEniCS-workshop/src/mesh_generation.html#visualizing-the-mesh)  
[https://jsdokken.com/dolfinx-tutorial/chapter3/em.html#meshing-a-complex-structure-with-subdomains](https://jsdokken.com/dolfinx-tutorial/chapter3/em.html#meshing-a-complex-structure-with-subdomains)

[Next page](https://fenicsproject.discourse.group/t/tutorial-on-3d-geometries/7140.md?page=2)
