Package: py2dm
Py2DMA Python module for reading and writingSMS 2DMmesh files.Support for all standard 2DM element typesOptional C++ parser extensionsDesigned with large meshes (>10 million cells) in mindPython version 3.6+*Fully type annotated*Python versions 3.6 and 3.7 require external packages. The theinstallationsection for details.The documentation for this project is hosted atRead the Docs.Basic usageThe following sections cover basic use cases to illustrate the Py2DM interface.For detailed instructions, performance considerations and advanced use cases, please refer to thedocumentation.Reading mesh filesThepy2dm.Readerclass provides multiple interfaces for extracting mesh data.For iterator-based access, the.iter_nodes(),.iter_elements()and.iter_node_strings()methods are available. These allow specifying the range of elements to retrieve. Alternatively, the.elements,.nodes, andnode_stringsproperties provide a shorthand for the corresponding iterator's default values.importpy2dmwithpy2dm.Reader('path/to/mesh.2dm')asmesh:fornodeinmesh.iter_nodes():ifnode.id%10==0:print(node)# The above will print the following:'<Node #10: (1200.0, 200.0, 20.0)>''<Node #20: (1120.0, 220.0, 10.0)>'...To access a given element or node by its unique ID, use the.element()and.node()method respectively:importpy2dmwithpy2dm.Reader('path/to/mesh.2dm')asmesh:forelementinmesh.elements:coords=[mesh.node(n).posforninelement.nodes]print(f'Element #{element.id}coordinates:\n'f'\t{coords}')# The above will print the following:'Element #1 coordinates:''    [(1.0, 2.0, 2.0), (2.0, 2.0, 1.5), (2.0, 1.0, 1.75)]''Element #2 coordinates:''    [(2.0, 2.0, 1.5), (2.0, 1.0, 1.75), (1.0, 1.0, 1.25)]'...Writing mesh filesThepy2dm.Writerclass provides thenode(),element(), andnode_string()factory methods to add new geometries to the mesh. The factories for nodes and elements will return the ID they were assigned.withpy2dm.Writer('path/to/mesh.2dm')asmesh:# Nodes can be instantiated first and added latermy_node=py2dm.Node(1,-5.0,-5.0,0.1)mesh.node(my_node)# Alternatively, you can use the Writer.node() method as a factorymesh.node(2,-5.0,5.0,0.2)# Specifying a negative ID will auto-select it based on the number# of existing nodes in the meshmesh.node(-1,5.0,-5.0,0.3)mesh.node(-1,5.0,5.0,0.2)# Similarly, elements can also be created separately or via the# factory methodmy_element=py2dm.Element3T(1,1,2,3)mesh.element(my_element)# Here too you can use a negative value to auto-select an IDmesh.element('E3T',-1,2,4,3)Format supportThe 2DM standard has been extended several times by different parties over the years. This led to the original2DM format specificationno longer matching SMS' own implementation, or those of other software packages such asTUFLOWorBASEMENT.Py2DM attempts to strike a balance of supporting these custom format variants without breaking compatibility with the original standard.Notable deviations from the 2DM standardThe maximum ID limit of 999'999 is not enforced.Floating point values may be used as material IDs by default.You can set theallow_float_matidflag to False to quietly discard floating point materials in the mesh:withpy2dm.Reader('mesh.2dm',allow_float_matid=False)asmesh:...Zero-based indices are supported if thezero_indexflag is set upon reader instantiation:withpy2dm.Reader('mesh.2dm',zero_index=True)asmesh:my_node=mesh.node(0)# This would normally cause an errorMore information on the various 2DM dialects can be found on theSubformatspage of the project documentation.InstallationPy2DM is available onPyPIand can be installed with pip:python-mpipinstall--upgradepy2dmRequirementsPy2DM is written for Python 3.8 and up and requires no additional packages on this version.For Python versions 3.6 and 3.7 (notably the ones used byQGIS 3as of writing this), two additional packages are required to provide functionality that was not yet available in the standard library at the time.typing_extensionscached-propertyThe above packages are only required for Python versions 3.6 and 3.7, with Python 3.8+, no third-party dependencies are needed.ContributingIf you have encountered any bugs or performance issues, please do get in touch via therepository issues.Similarly, any information on additional subformats or software-specific caveats is highly appreciated.