Code Monkey home page Code Monkey logo

vertices_to_h5m's Introduction

N|Python

CI with install

CI with examples

Upload Python Package anaconda-publish

conda-publish PyPI

This is a minimal Python package that provides a Python API interfaces for converting mesh vertices into a DAGMC h5m file ready for use in simulation.

Convert a set of vertices with their connectivity in to a DAGMC h5m file complete with material tags and ready for use neutronics simulations.

warning this approach does not imprint and merge the geometry and therefore requires that the mesh is well formed and does not overlap. Overlaps could lead to particles being lost during transport. If imprinting and merging is required consider using Paramak export_dagmc_h5m() method or cad-to-h5m to make the DAGMC geometry.

It is strongly advised to used the DAGMC overlap checker to check the resulting h5m file (see checking for overlaps section below).

Installation - Conda

This single line command should install the package and dependencies (including moab).

conda install -c fusion-energy -c conda-forge vertices_to_h5m

Installation - Pip + Conda

These two commands should install the package and dependencies. Moab requires a separate install as it is not available on pip

conda install -c conda-forge moab
pip install vertices_to_h5m

Examples

These examples with volumes made from just four triangles to keep the examples minimal. The package can also convert larger meshes as shown in the picture below.

Usage - single volume

To convert a single volume mesh into a h5m file. This also tags the volume with the material tag mat1.

from vertices_to_h5m import vertices_to_h5m
import numpy as np

# these are the x,y,z coordinates of each vertex. Entries should be floats 
vertices = np.array(
    [
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ]
)


# These are the triangle that connect individual vertices together to form a continious surface and also a closed volume. Entries should be ints
triangles = [
    np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
]


# This will produce a h5m file called one_volume.h5m ready for use with DAGMC enabled codes.
vertices_to_h5m(
    vertices=vertices,
    triangles=triangles,
    material_tags=["mat1"],
    h5m_filename="one_volume.h5m",
)

single_volume

Usage - multiple volumes

To convert multiple mesh volumes files into a h5m file. This also tags the relevant volumes with material tags called mat1 and mat2. This example also uses numpy arrays instead of lists, both are acceptable.

from vertices_to_h5m import vertices_to_h5m
import numpy as np

# These are the x,y,z coordinates of each vertex. Numpy array is set to type float to enforce floats
vertices = np.array(
    [[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1], [1, 1, 0]], dtype="float64"
)

# These are the two sets triangle that connect individual vertices together to form a continious surfaces and also two closed volume.
triangles = [
    np.array([[0, 1, 2], [3, 1, 2], [0, 2, 3], [0, 1, 3]]),
    np.array([[4, 5, 1], [4, 5, 2], [4, 1, 2], [5, 1, 2]]),
]

# This will produce a h5m file called two_volume_touching_edge.h5m ready for use with DAGMC enabled codes
vertices_to_h5m(
    vertices=vertices,
    triangles=triangles,
    material_tags=["mat1", "mat2"],
    h5m_filename="two_volume_touching_edge.h5m",
)

two_volumes

Checking for overlaps

To check for overlaps in the resulting h5m file one can use the DAGMC overlap checker. -p is the number of points to check on each line

conda install -c conda-forge
overlap_check dagmc.h5m -p 1000

vertices_to_h5m's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

nschloe ebknudsen

vertices_to_h5m's Issues

making h5m files with h5py

The package currently uses pymoab

However it might be possible to use h5py

This would have the advantage of being pip installable and faster.

The data structure needed for a DAGMC compatible h5m file is shown here https://sigma.mcs.anl.gov/moab/h5m-file-format/

meshio is able to make h5m files, but these have never quite worked in the simulation code we make the h5m files for (dagmc/openmc).

It would be great to add to meshio so that the h5m files produced work in neutronics simulations.
The h5m files produced by this package work in neutronics simulations so could be used to provide a simple comparison as we can make a simple set of vertices and triangles into a dagmc/openmc compatible h5m file and compare it to the output from meshio and see what is missing

Removing duplicate triangles

I originally wrote this issue up over on another repo
fusion-energy/brep_to_h5m#27

But I think it should live here instead

Basically, we have surfaces from different volumes that touch, in this case we have two sets of triangles that are duplicates. We should be able to remove one set and still have dagmc compatible geometry by flipping setting the surface sense

This might mean refactoring the input arguments from triangles and vertices to triangles broken down into faces and vertices

Then duplicate faces could be found.

Brep-to-h5m probably needs to have the option of outputting the mesh as triangles in each solid broken down into faces

adding unstrucutred mesh support

working on this but here is a script that nearly works

# this script uses cadquery to make a nice twisting CAD geometry
# the geometry is saved as a STEP file
# The step file is read into Gmesh and volume meshed (unstructured mesh)
# The mesh is saved as a .msh file
# The mesh file is converted to a h5 file (unstructured moab mesh format)
# The h5 file is read into an OpenMC unstructured mesh filter
# The simulation is run and a vtk file is produced for the mesh tally

from math import cos, floor, pi, sin

import cadquery as cq
from cadquery import exporters

import gmsh

# makes the CAD
def hypocycloid(t, r1, r2):
    return ((r1-r2)*cos(t)+r2*cos(r1/r2*t-t), (r1-r2)*sin(t)+r2*sin(-(r1/r2*t-t)))

def epicycloid(t, r1, r2):
    return ((r1+r2)*cos(t)-r2*cos(r1/r2*t+t), (r1+r2)*sin(t)-r2*sin(r1/r2*t+t))

def gear(t, r1=4, r2=1):
    if (-1)**(1+floor(t/2/pi*(r1/r2))) < 0:
        return epicycloid(t, r1, r2)
    else:
        return hypocycloid(t, r1, r2)

result = (cq.Workplane('XY').parametricCurve(lambda t: gear(t*2*pi, 6, 1))
    .twistExtrude(15, 90).faces('>Z').workplane().circle(2).cutThruAll())

# saves the step file
exporters.export(result, 'twist.step')


# meshes the step file
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)

volumes = gmsh.model.occ.importShapes('twist.step')
gmsh.model.occ.synchronize()

gmsh.model.mesh.generate(3)

gmsh.write("twist.vtk")
gmsh.finalize()

import meshio
# vtk to h5m appears to be one of the few conversions that does not print out errors
mesh = meshio.read("twist.vtk")
mesh.write("twist.h5m")

# neutronics simulation 
import openmc

air = openmc.Material(name='air')
air.set_density('g/cc', 0.001205)
air.add_element('N', 0.784431)

materials = openmc.Materials([air])
materials.export_to_xml()

src_pnt = openmc.stats.Point(xyz=(0.0, 0.0, 0.0))

source = openmc.Source(space=src_pnt)

settings = openmc.Settings()
settings.source = source

settings.run_mode = "fixed source"
settings.batches = 10
settings.particles = 100
settings.export_to_xml()

# we are using a csg cell but here is the code for a dagmc cell
# dagmc_univ = openmc.DAGMCUniverse(filename='twist.h5m').bounded_universe()
# geometry = openmc.Geometry(root=dagmc_univ)
# geometry.export_to_xml()

# UM geometry is 10 cm heigh so a 50cm sphere should be fine
surf1=openmc.Sphere(r=50, boundary_type='vacuum')
region1=-surf1
cell1=openmc.Cell(region=region1, fill=air)
geometry = openmc.Geometry([cell1])
geometry.export_to_xml()

unstructured_mesh = openmc.UnstructuredMesh("twist.h5m", library='moab')
mesh_filter = openmc.MeshFilter(unstructured_mesh)

tally = openmc.Tally()
tally.filters = [mesh_filter]
tally.scores = ['flux']
tally.estimator = 'tracklength'
tally.id = 1

tallies = openmc.Tallies([tally])
tallies.export_to_xml()

openmc.run()

# prints warning
#  WARNING: Output for a MOAB mesh (mesh 1) was requested but will not be written.
#           Please use the Python API to generated the desired VTK tetrahedral
#           mesh.
# therefore attempting to get mesh from statepoint file


with openmc.StatePoint('statepoint.10.h5') as sp:
    tally = sp.tallies[1]
    umesh = sp.meshes[1]

    centroids = umesh.centroids  # why is this needed, why can't we use 
    mesh_vols = umesh.volumes  # why is this needed

    flux_scores = tally.get_values(scores=['flux']).squeeze()

print(flux_scores[0:10])
data_dict = {'Flux': flux_scores}

umesh.write_data_to_vtk(filename="flux.vtk", datasets=data_dict)

# then open paraview to see the vtk file produced

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.