|
| 1 | +try: |
| 2 | + import omf |
| 3 | +except ImportError: |
| 4 | + raise ImportError( |
| 5 | + "You need to install the omf package to use this feature. " |
| 6 | + "You can install it with: pip install --pre omf" |
| 7 | + ) |
| 8 | +import numpy as np |
| 9 | + |
| 10 | + |
| 11 | +def get_project(filename): |
| 12 | + try: |
| 13 | + project = omf.load(filename) |
| 14 | + except FileNotFoundError: |
| 15 | + project = omf.Project(name='LoopStructural Model') |
| 16 | + return project |
| 17 | + |
| 18 | + |
| 19 | +def get_cell_attributes(loopobject): |
| 20 | + attributes = [] |
| 21 | + if loopobject.cell_properties: |
| 22 | + attributes += [ |
| 23 | + omf.NumericAttribute(name=k, array=v, location="faces") |
| 24 | + for k, v in loopobject.cell_properties.items() |
| 25 | + ] |
| 26 | + return attributes |
| 27 | + |
| 28 | + |
| 29 | +def get_point_attributed(loopobject): |
| 30 | + attributes = [] |
| 31 | + if loopobject.properties: |
| 32 | + attributes += [ |
| 33 | + omf.NumericAttribute(name=k, array=v, location="vertices") |
| 34 | + for k, v in loopobject.properties.items() |
| 35 | + ] |
| 36 | + return attributes |
| 37 | + |
| 38 | + |
| 39 | +def add_surface_to_omf(surface, filename): |
| 40 | + |
| 41 | + attributes = [] |
| 42 | + attributes += get_cell_attributes(surface) |
| 43 | + attributes += get_point_attributed(surface) |
| 44 | + surface = omf.Surface( |
| 45 | + vertices=surface.vertices, |
| 46 | + triangles=surface.triangles, |
| 47 | + attributes=attributes, |
| 48 | + name=surface.name, |
| 49 | + ) |
| 50 | + project = get_project(filename) |
| 51 | + |
| 52 | + project.elements += [surface] |
| 53 | + omf.save(project, filename, mode='w') |
| 54 | + |
| 55 | + |
| 56 | +def add_pointset_to_omf(points, filename): |
| 57 | + |
| 58 | + attributes = [] |
| 59 | + attributes += get_point_attributed(points) |
| 60 | + |
| 61 | + points = omf.PointSet( |
| 62 | + vertices=points.locations, |
| 63 | + attributes=attributes, |
| 64 | + name=points.name, |
| 65 | + ) |
| 66 | + |
| 67 | + project = get_project(filename) |
| 68 | + project.elements += [points] |
| 69 | + omf.save(project, filename, mode='w') |
| 70 | + |
| 71 | + |
| 72 | +def add_structured_grid_to_omf(grid, filename): |
| 73 | + print('Open Mining Format cannot store structured grids') |
| 74 | + return |
| 75 | + # attributes = [] |
| 76 | + # attributes += get_cell_attributes(grid) |
| 77 | + # attributes += get_point_attributed(grid) |
| 78 | + |
| 79 | + # vol = omf.TensorGridBlockModel( |
| 80 | + # name=grid.name, |
| 81 | + # tensor_u=np.ones(grid.nsteps[0]).astype(float), |
| 82 | + # tensor_v=np.ones(grid.nsteps[0]).astype(float), |
| 83 | + # tensor_w=np.ones(grid.nsteps[0]).astype(float), |
| 84 | + # origin=grid.origin, |
| 85 | + # attributes=attributes, |
| 86 | + # ) |
| 87 | + # project = get_project(filename) |
| 88 | + # project.elements += [vol] |
| 89 | + # omf.save(project, filename, mode='w') |
0 commit comments