def update_p_file(job_dir, inlet_side=None, outlet_side=None, **kwargs):
    r"""
    Updates the 0/p file
    """
    # loading files
    p_file = OpenFoamFile(os.path.join(job_dir, '0', 'p'))
    u_file = OpenFoamFile(os.path.join(job_dir, '0', 'U'))

    # setting pressure BCs and modifying velocity BCs
    if kwargs['inlet_p']:
        p_val = convert_value(float(kwargs['inlet_p'][0]), kwargs['inlet_p'][1])
        p_val = p_val/kwargs['density'][0]
        p_val = 'uniform {}'.format(p_val)
        #
        p_file['boundaryField'][inlet_side]['type'] = 'fixedValue'
        p_file['boundaryField'][inlet_side]['value'] = p_val
        #
        u_file['boundaryField'][inlet_side]['type'] = 'zeroGradient'
        u_file['boundaryField'][inlet_side].pop('value', None)

    #
    if kwargs['outlet_p']:
        p_val = convert_value(float(kwargs['outlet_p'][0]), kwargs['outlet_p'][1])
        p_val = p_val/kwargs['density'][0]
        p_val = 'uniform {}'.format(p_val)
        #
        p_file['boundaryField'][outlet_side]['type'] = 'fixedValue'
        p_file['boundaryField'][outlet_side]['value'] = p_val
        #
        u_file['boundaryField'][outlet_side]['type'] = 'zeroGradient'
        u_file['boundaryField'][outlet_side].pop('value', None)

    #
    p_file.write_foam_file(path=job_dir, overwrite=True)
    u_file.write_foam_file(path=job_dir, overwrite=True)
def update_u_file(job_dir, **kwargs):
    r"""
    Updates the 0/U file
    """
    aper_map = DataField(kwargs['aper_map'])
    p_file = OpenFoamFile(os.path.join(job_dir, '0', 'p'))
    u_file = OpenFoamFile(os.path.join(job_dir, '0', 'U'))
    inlet_side = kwargs['inlet_side']
    outlet_side = kwargs['outlet_side']
    vox = kwargs['voxel_size']
    avg = kwargs['avg_fact']
    #
    area_dict = {
        'left': sum(aper_map.data_map[:, 0] * vox**2 * avg),
        'right': sum(aper_map.data_map[:, -1] * vox**2 * avg),
        'top': sum(aper_map.data_map[-1, :] * vox**2 * avg),
        'bottom': sum(aper_map.data_map[0, :] * vox**2 * avg)
    }

    # calculating SI velocities
    if kwargs['inlet_q']:
        vel = kwargs['inlet_q'][0:3]
        vel = [convert_value(float(v), kwargs['inlet_q'][3]) for v in vel]
        vel = vel/area_dict[inlet_side]
        vel = 'uniform ({} {} {})'.format(*vel)
        #
        u_file['boundaryField'][inlet_side]['type'] = 'fixedValue'
        u_file['boundaryField'][inlet_side]['value'] = vel
        #
        p_file['boundaryField'][inlet_side]['type'] = 'zeroGradient'
        p_file['boundaryField'][inlet_side].pop('value', None)
    #
    if kwargs['outlet_q']:
        vel = kwargs['outlet_q'][0:3]
        vel = [convert_value(float(v), kwargs['outlet_q'][3]) for v in vel]
        vel = vel/area_dict[outlet_side]
        vel = 'uniform ({} {} {})'.format(*vel)
        #
        u_file['boundaryField'][outlet_side]['type'] = 'fixedValue'
        u_file['boundaryField'][outlet_side]['value'] = vel
        p_file['boundaryField'][outlet_side]['type'] = 'zeroGradient'
        p_file['boundaryField'][outlet_side].pop('value', None)
    #
    p_file.write_foam_file(path=job_dir, overwrite=True)
    u_file.write_foam_file(path=job_dir, overwrite=True)
    def test_register_voxel_unit(self):
        r"""
        Checks that voxel can be registered and converted with
        """
        unit_conversion.register_voxel_unit(10.0, 'microns')
        self.ureg.define('voxel = 10.0 * microns = vox')

        value = unit_conversion.convert_value(1000, 'vox', 'SI')
        test_value = 1000 * self.ureg('vox').to_base_units()
        #
        assert value == test_value.magnitude
 def test_convert_value(self):
     r"""
     Testing values returned by a general test of many conversions
     """
     for val_in, unit_in, unit_out, val_out in self.convert_value_test:
         val = unit_conversion.convert_value(val_in, unit_in, unit_out)
         #
         # applying a tolerance since were working with floating point values
         val = round(val * 1.0E9)
         val_out = round(val_out.magnitude * 1.0E9)
         msg = 'Mutlitple unit conversion from {} to {} failed.'
         msg = msg.format(unit_in, unit_out)
         #
         assert val == val_out, msg
def update_transport_props(job_dir, **kwargs):
    r"""
    Updates the constant/transportProperties file. Updates the args object
    to have SI values for density and viscosity.
    """
    # loading file
    foam_file = os.path.join(job_dir, 'constant', 'transportProperties')
    foam_file = OpenFoamFile(foam_file)

    # converting values to SI
    density = kwargs['density']
    density = [convert_value(float(density[0]), density[1]), 'kg/m^3']
    #
    viscosity = kwargs['viscosity']
    viscosity = [convert_value(float(viscosity[0]), viscosity[1]), 'pa*s']

    # ouputting SI values to global args object
    args.density = density
    args.viscosity = viscosity
    viscosity = viscosity[0]/density[0]

    # setting kinematic viscosity values
    fmt = 'nu  [ 0  2 -1 0 0 0 0 ] {:0.6e};'
    foam_file['nu'] = fmt.format(viscosity)
    #
    fmt = 'nu0 [ 0 2 -1 0 0 0 0 ] {:0.6e}'
    foam_file['CrossPowerLawCoeffs']['nu0'] = fmt.format(viscosity)
    foam_file['BirdCarreauCoeffs']['nu0'] = fmt.format(viscosity)
    #
    fmt = 'nuInf [ 0 2 -1 0 0 0 0 ] {:0.6e}'
    foam_file['CrossPowerLawCoeffs']['nuInf'] = fmt.format(viscosity)
    foam_file['BirdCarreauCoeffs']['nuInf'] = fmt.format(viscosity)

    # setting density value
    fmt = 'rho  [ 1  -3 0 0 0 0 0 ] {:0.6e}'
    foam_file['rho'] = fmt.format(density[0])
    foam_file.write_foam_file(path=job_dir, overwrite=True)