Skip to content
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

Adding some CI #5

Merged
merged 19 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/ci_with_install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

# This CI will launch a Docker image that contains all the dependencies required
# within that image the pytest test suite is run

name: CI with install

on:
pull_request:
branches:
- develop
- main
paths-ignore:
- 'docs/**'
- '.gitignore'
- '*.md'
- 'CITATION.cff'
- 'LICENSE.txt'
- 'readthedocs.yml'

jobs:
testing:
runs-on: ubuntu-latest
container:
image: continuumio/miniconda3:4.12.0
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: install non pypi dependencies for cad creation
run: |
apt-get --allow-releaseinfo-change update
apt-get update -y
apt-get upgrade -y
apt-get install -y libgl1-mesa-glx libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev libosmesa6 libosmesa6-dev libgles2-mesa-dev
conda install -c conda-forge mamba
mamba install -c conda-forge moab
mamba install -c cadquery -c conda-forge cadquery=master

- name: install package
run: |
pip install .
python -c "import cad_to_dagmc"

- name: Run cad creation tests
run: |
pip install .[tests]
pytest tests/test_h5m_creation.py -v
# pytest tests/test_h5m_creation.py -v --cov=cad_to_dagmc --cov-append --cov-report term --cov-report xml

- name: install non pypi dependencies for neutronics
run: |
mamba uninstall moab
mamba uninstall cadquery
mamba install -c conda-forge -y "openmc=0.13.0=dagmc*nompi*"

- name: Run simulation tests
run: |
pip install .[tests]
pytest tests/test_h5m_in_simulation.py -v
40 changes: 40 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This yml file will trigger a Github Actions event that builds and upload the
# Python package to PiPy. This makes use of Twine and is triggered when a push
# to the main branch occures. For more information see:
# https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
# and for details on the Autobump version section see:
# https://github.com/grst/python-ci-versioneer

name: Upload Python Package

on:
# allows us to run workflows manually
workflow_dispatch:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel build twine

- name: Build and publish
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
python -m build
twine check dist/*
twine upload dist/*
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
# cad_to_dagmc
A minimal package that uses CadQuery functionality to convert Cad geometry to DAGMC h5m files

This particular method of producing DAGMC compatible h5m files from CAD geometry
is intended to convert STP files or [CadQuery](https://cadquery.readthedocs.io) objects to h5m file.

The use of CadQuery based surface tesselation and then conversion of the
vertices and triangle sets into h5m files directly (in memory) is relatively
fast with minimal file IO and the resulting meshed volumes have low triangle
count while maintaining a good representation of the volume as described in
[this](https://www.sciencedirect.com/science/article/abs/pii/S0920379615301484)
publication.

While this package concentrates on loading the CAD and meshing the surface it
then hands off the vertices and triangles sets to
[vertices_to_h5m](https://github.com/fusion-energy/vertices_to_h5m) which
converts these into a h5m geometry file.
Due to the modularity of this workflow if you have a preferred meshing
algorithm then it is entirely possible to pipe your own vertices and triangles
directly into vertices_to_h5m.

# Install

```bash
pip install cad_to_dagmc
```

# Usage

```python
Expand Down
31 changes: 10 additions & 21 deletions cad_to_dagmc/core.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
from vertices_to_h5m import vertices_to_h5m
from pathlib import Path
import dagmc_h5m_file_inspector as di
import openmc
import openmc_data_downloader as odd
import math

"""
Tests that check that:
- h5m files are created
- h5m files contain the correct number of volumes
- h5m files contain the correct material tags
- h5m files can be used a transport geometry in DAGMC with OpenMC
"""


from cadquery import importers
from OCP.GCPnts import GCPnts_QuasiUniformDeflection
Expand Down Expand Up @@ -61,7 +50,7 @@ def merge_surfaces(geometry):
return solids[0]

for solid in solids:
print(type(solid))
# print(type(solid))
# checks if solid is a compound as .val() is not needed for compounds
if isinstance(solid, (cq.occ_impl.shapes.Compound, cq.occ_impl.shapes.Solid)):
bldr.AddArgument(solid.wrapped)
Expand Down Expand Up @@ -144,7 +133,7 @@ def tessellate_parts(merged_solid, tolerance: float, angularTolerance: float = 0
loop_counter = 0

for s in merged_solid.Solids():
print(s.hashCode())
# print(s.hashCode())
# all_vertices[s.hashCode()] = {}
triangles_on_solids_faces[s.hashCode()] = {}
for f in s.Faces():
Expand Down Expand Up @@ -224,7 +213,7 @@ def tessellate_touching_parts(
loop_counter = 0

for s in merged_solid.Solids():
print(s.hashCode())
# print(s.hashCode())
# all_vertices[s.hashCode()] = {}
triangles_on_solids_faces[s.hashCode()] = {}
for f in s.Faces():
Expand Down Expand Up @@ -272,11 +261,11 @@ def tessellate_touching_parts(
offset += poly.NbNodes()

else:
print("found face in existing faces, reusing triangles")
# print("found face in existing faces, reusing triangles")
for key_s, value in triangles_on_solids_faces.items():
for key_f, face in value.items():
if key_f == f.hashCode():
print(f"found face {f.hashCode()}")
# print(f"found face {f.hashCode()}")
face_triangles = triangles_on_solids_faces[key_s][key_f]
# triangles.append(face_triangles)
# triangles_on_solids_faces[s.hashCode()]
Expand All @@ -289,14 +278,14 @@ def tessellate_touching_parts(

list_of_triangles_per_solid = []
for key, value in triangles_on_solids_faces.items():
print(key)
# print(key)
triangles_on_solid = []
for key, face in value.items():
print(" ", key, face)
# print(" ", key, face)
triangles_on_solid = triangles_on_solid + face
list_of_triangles_per_solid.append(triangles_on_solid)
for vertice in vertices:
print(vertice)
print(len(vertices))
# for vertice in vertices:
# print(vertice)
# print(len(vertices))

return vertices, list_of_triangles_per_solid
84 changes: 84 additions & 0 deletions tests/test_h5m_creation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# import dagmc_h5m_file_inspector as di
import cad_to_dagmc
from vertices_to_h5m import vertices_to_h5m
import dagmc_h5m_file_inspector as di

"""
Tests that check that:
- h5m files are created
- h5m files contain the correct number of volumes
- h5m files contain the correct material tags

"""


from cadquery import importers
from OCP.GCPnts import GCPnts_QuasiUniformDeflection

# from cadquery.occ_impl import shapes
import OCP
import cadquery as cq
from OCP.TopLoc import TopLoc_Location
from OCP.BRep import BRep_Tool
from OCP.TopAbs import TopAbs_Orientation


def test_h5m_production_with_single_volume_list():
# """The simplest geometry, a single 4 sided shape with lists instead of np arrays"""

stp_files = ["tests/extrude_rectangle.stp", "tests/single_cube.stp"]
h5m_files = ["tests/extrude_rectangle.h5m", "tests/single_cube.h5m"]

for stp_file, h5m_file in zip(stp_files, h5m_files):

stp_file = cad_to_dagmc.load_stp_file(stp_file)

merged_stp_file = cad_to_dagmc.merge_surfaces(stp_file)
vertices, triangles = cad_to_dagmc.tessellate(merged_stp_file, tolerance=2)

vertices_to_h5m(
vertices=vertices,
triangles=[triangles],
material_tags=["mat1"],
h5m_filename=h5m_file,
)

assert di.get_volumes_and_materials_from_h5m(h5m_file) == {1: "mat1"}


def test_h5m_production_with_multi_volume_list():

stp_files = [
"tests/multi_volume_cylinders.stp",
"tests/two_disconnected_cubes.stp",
"tests/two_connected_cubes.stp",
]
material_tags = [
["mat1", "mat2", "mat3", "mat4", "mat5", "mat6"],
["mat1", "mat2"],
["mat1", "mat2"],
]
h5m_files = [
"tests/multi_volume_cylinders.h5m",
"tests/two_disconnected_cubes.h5m",
"tests/two_connected_cubes.h5m",
]
for stp_file, mat_tags, h5m_file in zip(stp_files, material_tags, h5m_files):

stp_file_object = cad_to_dagmc.load_stp_file(stp_file)
merged_stp_file = cad_to_dagmc.merge_surfaces(stp_file_object)
vertices, triangles = cad_to_dagmc.tessellate_touching_parts(
merged_stp_file, tolerance=2
)

vertices_to_h5m(
vertices=vertices,
triangles=triangles,
material_tags=mat_tags,
h5m_filename=h5m_file,
)

tags_dict = {}
for counter, loop_mat_tag in enumerate(mat_tags, 1):
tags_dict[counter] = loop_mat_tag
assert di.get_volumes_and_materials_from_h5m(h5m_file) == tags_dict
Loading