Source code for apmapflow.openfoam.openfoam_export

"""
================================================================================
OpenFoam Export
================================================================================
| A class build to handle mass exportation of OpenFoam files.

| Written By: Matthew Stadelman
| Date Written: 2016/08/08
| Last Modifed: 2016/08/08

"""
from collections import OrderedDict
from .openfoam import OpenFoamFile
from .block_mesh_dict import BlockMeshDict


[docs]class OpenFoamExport(dict): r""" A class to handle generation and exporting of OpenFoam files """
[docs] def __init__(self, field=None, avg_fact=1.0, mesh_params=None): r""" Handles generation and exporting of OpenFoam files """ # super().__init__() self.foam_files = {} self.block_mesh_dict = None if field is not None: self.generate_block_mesh_dict(field, avg_fact, mesh_params)
[docs] def generate_block_mesh_dict(self, field, avg_fact=1.0, mesh_params=None): r""" Passes arguments off to BlockMeshDict init method. """ self.block_mesh_dict = BlockMeshDict(field, avg_fact, mesh_params)
[docs] def generate_foam_files(self, *args): r""" Generates, reads and adds OpenFoam files to the export to provide a centralized location to perform modifications and write files. Any number of 'file' arguments may be passed into this function but they are required to have one of the following three forms: 1. An already existing OpenFoamFile instances 2. A string representing the path to a valid OpenFoamFile 3. An iterable acceptable to the dictionary constructor with at minimum 2 keys: location and object; 'class' is an optional key. Those 2/3 keys are removed and the rest of the iterable is passed along to the OpenFoamFile __init__ method as the 'values' keyword. Files are stored on the export object in a dictionary attribute called 'foam_files'. Keys in this dictionary have the format of 'location.name'. Where 'name' is 'name' attribute on the OpenFoamFile. """ # # looping through args for file_params in args: if isinstance(file_params, OpenFoamFile): foam_file = file_params location = foam_file.head_dict['location'].replace('"', '') name = foam_file.name elif isinstance(file_params, str): foam_file = OpenFoamFile(file_params) location = foam_file.head_dict['location'].replace('"', '') name = foam_file.name else: # generating initial dict from iterable and getting args values = OrderedDict(file_params) location = values.pop('location') name = values.pop('object') class_name = values.pop('class_name', None) # foam_file = OpenFoamFile(location, name, class_name, values=values) # self.foam_files[location + '.' + name] = foam_file
[docs] def write_symmetry_plane(self, path='.', create_dirs=True, overwrite=False): r""" Passes arguments off to the BlockMeshDict method """ self.block_mesh_dict.write_symmetry_plane(path, create_dirs, overwrite)
[docs] def write_mesh_file(self, path='.', create_dirs=True, overwrite=False): r""" Passes arguments off to the BlockMeshDict method """ self.block_mesh_dict.write_mesh_file(path, create_dirs, overwrite)
[docs] def write_foam_files(self, path='.', overwrite=False): r""" Writes the files generated by 'generate_foam_files' to their associated directories on the supplied path. If a directory doesn't exist then it is created """ # # writing files for foam_file in self.foam_files.values(): foam_file.write_foam_file(path=path, create_dirs=True, overwrite=overwrite)