Source code for bolt.lib.linear.file_io.dump

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import arrayfire as af
import numpy as np
from petsc4py import PETSc
from bolt.lib.linear.utils.fft_funcs import ifft2

def dump_moments(self, file_name):
    """
    This function is used to dump variables to a file for later usage.

    Parameters
    ----------

    file_name : str
                The variables will be dumped to this provided file name.

    Returns
    -------

    This function returns None. However it creates a file 'file_name.h5',
    containing all the moments that were defined under moments_defs in
    physical_system.

    Examples
    --------

    >> solver.dump_variables('boltzmann_moments_dump')

    The above set of statements will create a HDF5 file which contains the
    all the moments which have been defined in the physical_system object.
    The data is always stored with the key 'moments' inside the HDF5 file.
    Suppose 'density' and 'energy' are two these moments, and are declared
    the first and second in the moment_exponents object:

    These variables can then be accessed from the file using
    
    >> import h5py
    
    >> h5f    = h5py.File('boltzmann_moments_dump.h5', 'r')
    
    >> rho    = h5f['moments'][:][:, :, 0]
    
    >> energy = h5f['moments'][:][:, :, 1]
    
    >> h5f.close()

    However, in the case of multiple species, the following holds:

    >> rho_species_1 = h5f['moments'][:][:, :, 0]
    
    >> rho_species_2 = h5f['moments'][:][:, :, 1]
    
    >> energy_species_1 = h5f['moments'][:][:, :, 2]

    >> energy_species_2 = h5f['moments'][:][:, :, 3]

    """
    attributes = [a for a in dir(self.physical_system.moments) if not a.startswith('_')]
    
    for i in range(len(attributes)):
        
        if(i == 0):
            array_to_dump = self.compute_moments(attributes[i])
        else:
            array_to_dump = af.join(1, array_to_dump,
                                    self.compute_moments(attributes[i])
                                   )
    
    af.flat(array_to_dump).to_ndarray(self._glob_moments_array)
    viewer = PETSc.Viewer().createHDF5(file_name + '.h5', 'w')
    viewer(self._glob_moments)

def dump_distribution_function(self, file_name):
    """
    This function is used to dump distribution function to a file for
    later usage.This dumps the complete 5D distribution function which
    can be used for post-processing

    Parameters
    ----------

    file_name : The distribution_function array will be dumped to this
                provided file name.

    Returns
    -------

    This function returns None. However it creates a file 'file_name.h5',
    containing the data of the distribution function

    Examples
    --------
    
    >> solver.dump_distribution_function('distribution_function')

    The above statement will create a HDF5 file which contains the
    distribution function. The data is always stored with the key 
    'distribution_function'

    This can later be accessed using

    >> import h5py
    
    >> h5f = h5py.File('distribution_function', 'r')
    
    >> f   = h5f['distribution_function'][:]
    
    >> h5f.close()

    Alternatively, it can also be used with the load function to resume
    a long-running calculation.

    >> solver.load_distribution_function('distribution_function')
    """
    array_to_dump = 0.5 * self.N_q2 * self.N_q1  * af.real(ifft2(self.f_hat))
    array_to_dump.to_ndarray(self._glob_f_array)
    
    viewer = PETSc.Viewer().createHDF5(file_name + '.h5', 'w')
    viewer(self._glob_f)

def dump_EM_fields(self, file_name):
    """
    This function is used to EM fields to a file for later usage.
    This dumps all the EM fields quantities E1, E2, E3, B1, B2, B3 
    which can then be used later for post-processing

    Parameters
    ----------

    file_name : The EM_fields array will be dumped to this
                provided file name.

    Returns
    -------

    This function returns None. However it creates a file 'file_name.h5',
    containing the data of the EM fields.

    Examples
    --------
    
    >> solver.dump_EM_fields('data_EM_fields')

    The above statement will create a HDF5 file which contains the
    EM fields data. The data is always stored with the key 
    'EM_fields'

    This can later be accessed using

    >> import h5py
    
    >> h5f = h5py.File('data_EM_fields.h5', 'r')
    
    >> EM_fields = h5f['EM_fields'][:]

    >> E1 = EM_fields[:, :, 0]
    
    >> E2 = EM_fields[:, :, 1]
    
    >> E3 = EM_fields[:, :, 2]
    
    >> B1 = EM_fields[:, :, 3]
    
    >> B2 = EM_fields[:, :, 4]
    
    >> B3 = EM_fields[:, :, 5]

    >> h5f.close()

    Alternatively, it can also be used with the load function to resume
    a long-running calculation.

    >> solver.load_EM_fields('data_EM_fields')
    """
    array_to_dump = 0.5 * self.N_q2 * self.N_q1  * af.real(ifft2(self.fields_solver.fields_hat))
    array_to_dump.to_ndarray(self.fields_solver._glob_fields_array)
    
    viewer = PETSc.Viewer().createHDF5(file_name + '.h5', 'w')
    viewer(self.fields_solver._glob_fields)

    return