romtools.vector_space.utils.outputter
1# 2# ************************************************************************ 3# 4# ROM Tools and Workflows 5# Copyright 2019 National Technology & Engineering Solutions of Sandia,LLC 6# (NTESS) 7# 8# Under the terms of Contract DE-NA0003525 with NTESS, the 9# U.S. Government retains certain rights in this software. 10# 11# ROM Tools and Workflows is licensed under BSD-3-Clause terms of use: 12# 13# Redistribution and use in source and binary forms, with or without 14# modification, are permitted provided that the following conditions 15# are met: 16# 17# 1. Redistributions of source code must retain the above copyright 18# notice, this list of conditions and the following disclaimer. 19# 20# 2. Redistributions in binary form must reproduce the above copyright 21# notice, this list of conditions and the following disclaimer in the 22# documentation and/or other materials provided with the distribution. 23# 24# 3. Neither the name of the copyright holder nor the names of its 25# contributors may be used to endorse or promote products derived 26# from this software without specific prior written permission. 27# 28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 31# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 32# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 33# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 38# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39# POSSIBILITY OF SUCH DAMAGE. 40# 41# Questions? Contact Eric Parish (ejparis@sandia.gov) 42# 43# ************************************************************************ 44# 45 46import os 47import math 48import numpy as np 49from romtools.vector_space import VectorSpace 50 51try: 52 import exodus 53 exodus_available = True 54except ImportError: 55 exodus_available = False 56 57try: 58 import h5py 59 hdf5_available = True 60except ImportError: 61 hdf5_available = False 62 63 64def npz_output(output_filename: str, vector_space: VectorSpace, compress=True) -> None: 65 ''' 66 Save vector space information to a compressed or uncompressed NumPy .npz file. 67 68 Args: 69 filename (str): The name of the output file. 70 vector_space (VectorSpace): The vector space containing shift and basis information. 71 compress (bool, optional): Whether to compress the output file (default is True). 72 73 Example: 74 npz_output("vector_space.npz", my_vector_space) 75 ''' 76 if compress: 77 np.savez_compressed(output_filename, 78 shift=vector_space.get_shift_vector(), 79 basis=vector_space.get_basis()) 80 else: 81 np.savez(output_filename, 82 shift=vector_space.get_shift_vector(), 83 basis=vector_space.get_basis()) 84 85 86def hdf5_output(output_filename: str, vector_space: VectorSpace) -> None: 87 ''' 88 Save vector space information to an HDF5 file. 89 90 Args: 91 output_filename (str): The name of the output HDF5 file. 92 vector_space (VectorSpace): The vector space containing shift and basis information. 93 94 Example: 95 hdf5_output("vector_space.h5", my_vector_space) 96 ''' 97 with h5py.File(output_filename, 'w') as f: 98 f.create_dataset('shift', data=vector_space.get_shift_vector()) 99 f.create_dataset('basis', data=vector_space.get_basis()) 100 101 102def exodus_ouput(output_filename: str, mesh_filename: str, vector_space: VectorSpace, var_names: list = None) -> None: 103 ''' 104 Save vector space information to an Exodus file. 105 106 Args: 107 output_filename (str): The name of the output Exodus file. 108 mesh_filename (str): The name of the mesh file. 109 vector_space (VectorSpace): The vector space containing shift and basis information. 110 var_names (list, optional): A list of variable names (default is None). 111 112 Example: 113 exodus_output("vector_space.e", "mesh.exo", my_vector_space, var_names=["var1", "var2"]) 114 ''' 115 if os.path.isfile(output_filename): 116 os.system(f'rm {output_filename}') 117 118 e = exodus.copy_mesh(mesh_filename, output_filename) 119 e.close() 120 e = exodus.exodus(output_filename, mode='a') 121 122 num_vars = vector_space.get_shift_vector().shape[0] 123 num_modes = vector_space.get_basis().shape[2] 124 num_modes_str_len = int(math.log10(num_modes))+1 125 126 if var_names is None: 127 var_names = [f"{i}" for i in range(num_vars)] 128 129 assert (len(var_names) == num_vars), ( 130 f"len(variable_names), {len(var_names)} " 131 f"!= number of variables in basis, {num_vars}" 132 ) 133 134 field_names = [] 135 for var_name in var_names: 136 field_names.append(f"u0_{var_name}") 137 for j in range(num_modes): 138 mode_str = str.zfill(str(j+1), num_modes_str_len) 139 field_names.append(f"phi_{var_name}_{mode_str}") 140 exodus.add_variables(e, nodal_vars=field_names) 141 142 for i in range(num_vars): 143 shift = vector_space.get_shift_vector()[i, :] 144 field_name = field_names[i*(num_modes+1)] 145 e.put_node_variable_values(field_name, 1, shift) 146 147 basis = vector_space.get_basis() 148 for j in range(num_modes): 149 field_name = field_names[i*(num_modes+1) + j + 1] 150 e.put_node_variable_values(field_name, 1, basis[i, :, j]) 151 152 e.close()
def
npz_output( output_filename: str, vector_space: romtools.vector_space.VectorSpace, compress=True) -> None:
65def npz_output(output_filename: str, vector_space: VectorSpace, compress=True) -> None: 66 ''' 67 Save vector space information to a compressed or uncompressed NumPy .npz file. 68 69 Args: 70 filename (str): The name of the output file. 71 vector_space (VectorSpace): The vector space containing shift and basis information. 72 compress (bool, optional): Whether to compress the output file (default is True). 73 74 Example: 75 npz_output("vector_space.npz", my_vector_space) 76 ''' 77 if compress: 78 np.savez_compressed(output_filename, 79 shift=vector_space.get_shift_vector(), 80 basis=vector_space.get_basis()) 81 else: 82 np.savez(output_filename, 83 shift=vector_space.get_shift_vector(), 84 basis=vector_space.get_basis())
Save vector space information to a compressed or uncompressed NumPy .npz file.
Arguments:
- filename (str): The name of the output file.
- vector_space (VectorSpace): The vector space containing shift and basis information.
- compress (bool, optional): Whether to compress the output file (default is True).
Example:
npz_output("vector_space.npz", my_vector_space)
87def hdf5_output(output_filename: str, vector_space: VectorSpace) -> None: 88 ''' 89 Save vector space information to an HDF5 file. 90 91 Args: 92 output_filename (str): The name of the output HDF5 file. 93 vector_space (VectorSpace): The vector space containing shift and basis information. 94 95 Example: 96 hdf5_output("vector_space.h5", my_vector_space) 97 ''' 98 with h5py.File(output_filename, 'w') as f: 99 f.create_dataset('shift', data=vector_space.get_shift_vector()) 100 f.create_dataset('basis', data=vector_space.get_basis())
Save vector space information to an HDF5 file.
Arguments:
- output_filename (str): The name of the output HDF5 file.
- vector_space (VectorSpace): The vector space containing shift and basis information.
Example:
hdf5_output("vector_space.h5", my_vector_space)
def
exodus_ouput( output_filename: str, mesh_filename: str, vector_space: romtools.vector_space.VectorSpace, var_names: list = None) -> None:
103def exodus_ouput(output_filename: str, mesh_filename: str, vector_space: VectorSpace, var_names: list = None) -> None: 104 ''' 105 Save vector space information to an Exodus file. 106 107 Args: 108 output_filename (str): The name of the output Exodus file. 109 mesh_filename (str): The name of the mesh file. 110 vector_space (VectorSpace): The vector space containing shift and basis information. 111 var_names (list, optional): A list of variable names (default is None). 112 113 Example: 114 exodus_output("vector_space.e", "mesh.exo", my_vector_space, var_names=["var1", "var2"]) 115 ''' 116 if os.path.isfile(output_filename): 117 os.system(f'rm {output_filename}') 118 119 e = exodus.copy_mesh(mesh_filename, output_filename) 120 e.close() 121 e = exodus.exodus(output_filename, mode='a') 122 123 num_vars = vector_space.get_shift_vector().shape[0] 124 num_modes = vector_space.get_basis().shape[2] 125 num_modes_str_len = int(math.log10(num_modes))+1 126 127 if var_names is None: 128 var_names = [f"{i}" for i in range(num_vars)] 129 130 assert (len(var_names) == num_vars), ( 131 f"len(variable_names), {len(var_names)} " 132 f"!= number of variables in basis, {num_vars}" 133 ) 134 135 field_names = [] 136 for var_name in var_names: 137 field_names.append(f"u0_{var_name}") 138 for j in range(num_modes): 139 mode_str = str.zfill(str(j+1), num_modes_str_len) 140 field_names.append(f"phi_{var_name}_{mode_str}") 141 exodus.add_variables(e, nodal_vars=field_names) 142 143 for i in range(num_vars): 144 shift = vector_space.get_shift_vector()[i, :] 145 field_name = field_names[i*(num_modes+1)] 146 e.put_node_variable_values(field_name, 1, shift) 147 148 basis = vector_space.get_basis() 149 for j in range(num_modes): 150 field_name = field_names[i*(num_modes+1) + j + 1] 151 e.put_node_variable_values(field_name, 1, basis[i, :, j]) 152 153 e.close()
Save vector space information to an Exodus file.
Arguments:
- output_filename (str): The name of the output Exodus file.
- mesh_filename (str): The name of the mesh file.
- vector_space (VectorSpace): The vector space containing shift and basis information.
- var_names (list, optional): A list of variable names (default is None).
Example:
exodus_output("vector_space.e", "mesh.exo", my_vector_space, var_names=["var1", "var2"])