-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #324 from fusion-energy/improving-cad-examples
Improving cad examples
- Loading branch information
Showing
8 changed files
with
14,769 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
tasks/task_16_converting_CAD_geometry_to_DAGMC/2_converting_cad_in_memory.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# CAD conversion in memory\n", | ||
"\n", | ||
"It is possible to convert from a CadQuery object to a DAGMC surface or volume mesh.\n", | ||
"\n", | ||
"This is quicker and more concise than writing to file and reading it in again.\n", | ||
"\n", | ||
"First import the packages needed for the cad creation ([CadQuery](https://cadquery.readthedocs.io)) and conversion ([cad_to_dagmc](https://github.com/fusion-energy/cad_to_dagmc))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from cad_to_dagmc import CadToDagmc\n", | ||
"import cadquery as cq" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Make some CAD geometry, we have lots of options here but to keep the example concise I've made some text.\n", | ||
"\n", | ||
"The important thing to note here is that we have 10 separate solids in the CAD model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"text = cq.Workplane().text(txt=\"cad to dagmc\", fontsize=10, distance=1)\n", | ||
"text" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Next we make an instance of CadToDagmc with this geometry to prepare for conversion" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_model = CadToDagmc()\n", | ||
"my_model.add_cadquery_object(\n", | ||
" cadquery_object=text,\n", | ||
" material_tags=[\"mat1\"]* 10 # ten volumes each with with material tag \"mat1\"\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The ```export_dagmc_h5m_file()``` function will make a surface mesh geometry of the CAD ready for use in neutronics simulations.\n", | ||
"\n", | ||
"With openmc this DAGMC h5m file can be loading in using the ```openmc.DAGMCUniverse``` class.\n", | ||
"\n", | ||
"This makes a 2D mesh / surface mesh / faceted geometry / triangles of the geometry faces.\n", | ||
"\n", | ||
"With this type of geometry you can do simulations and score material tallies, cell tallies directly on the mesh volumes.\n", | ||
"\n", | ||
"You can also overlay OpenMC meshes such as regular mesh or cylindrical mesh on top of the geometry.\n", | ||
"\n", | ||
"The ```export_dagmc_h5m_file()``` function supports different arguments to control the mesh parameters.\n", | ||
"\n", | ||
"For this example we will use simple settings for ```max_mesh_size``` and ```min_mesh_size```.\n", | ||
"\n", | ||
"To see more options take a look at the [cad_to_dagmc package](https://github.com/fusion-energy/cad_to_dagmc)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_model.export_dagmc_h5m_file(\n", | ||
" filename=\"dagmc.h5m\",\n", | ||
" max_mesh_size=10,\n", | ||
" min_mesh_size=2,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"DAGMC and OpenMC support another form of mesh often called an unstructured mesh.\n", | ||
"\n", | ||
"An unstructured mesh can be overlaid over the geometry much like a regular mesh or cylindrical mesh and used to score tallies spatially.\n", | ||
"\n", | ||
"The ```openmc.UnstructuredMesh``` class supports MOAB and LibMesh type unstructured meshes.\n", | ||
"\n", | ||
"In this example we are making a MOAB / DAGMC unstructured mesh which is entirely made of tetrahedrals.\n", | ||
"\n", | ||
"This is a volume mesh where the outer surface is also the same as the outer surface of the 2D mesh as we have used the same mesh parameters.\n", | ||
"\n", | ||
"This means we have a conformal volume mesh to score on which aligns to the material / cell boundaries in the DAGMC h5m 2D mesh.\n", | ||
"\n", | ||
"This conformal mesh is one of the advantages of UnstructuredMesh over regular meshes and cylindrical meshes\n", | ||
"\n", | ||
"The ```export_unstructured_mesh_file()``` function will produce a vtk file than can be loaded into OpenMC using ```openmc.UnstructuredMesh```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"my_model.export_unstructured_mesh_file(\n", | ||
" filename=\"dagmc.vtk\",\n", | ||
" max_mesh_size=10,\n", | ||
" min_mesh_size=2,\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now you have two mesh files that can be used for neutronics simulations in OpenMC or other DAGMC compatible codes.\n", | ||
"\n", | ||
"Learning objectives for this task\n", | ||
"- Know how to produce DAGMC 2d surface mesh geometry\n", | ||
"- Know how to produce DAGMC 3d volume mesh\n", | ||
"- Understand the differences between the types of meshes produced by cad-to-dagmc\n", | ||
"- know some of the arguments for customising the mesh" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "openmc_dev_2", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from cad_to_dagmc import CadToDagmc | ||
import cadquery as cq | ||
|
||
text = cq.Workplane().text(txt="OpenMC", fontsize=10, distance=1) | ||
|
||
text.export("step_cad_file_for_conversion.step") |
Oops, something went wrong.