def extract_interface_length(dim2dParamPath,temperature):
    '''
    @description: extract the interface length from the 
                  parameters in dim2d_parameters and the
                  temperature for the initial conditions
    '''

    if(os.path.isfile(dim2dParamPath)):
        length_c  = float(get_parameter('length_c', dim2dParamPath))
        dim2d_a   = float(get_parameter( 'dim2d_a', dim2dParamPath))
        dim2d_b   = float(get_parameter( 'dim2d_b', dim2dParamPath))
        dim2d_M   = float(get_parameter( 'dim2d_M', dim2dParamPath))
        dim2d_K   = float(get_parameter( 'dim2d_K', dim2dParamPath))

    else:
        sys.exit('*** '+dim2dParamPath+' does not exist***')

    # compute the Weber number
    we = get_we(length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_K)

    # compute the interface length from the temperature
    interface_lgh = get_interface_length(we,temperature)

    return interface_lgh
def get_inputsToBeModified(steady_state_ac,
                           temperature,
                           micro_contact_angle,
                           phase_at_center,
                           gravity_ac,
                           gravity_amp,
                           nb_pts_in_interface,
                           ratio_bubble_interface,
                           ratio_eq_length_domain,
                           ratio_drop_length_domain,
                           CFL_constant,
                           total_nb_files,
                           spherical_cap=False):

    '''
    @description
    determine the inputs to be modified in the template.txt
    template input file to run the DIM2D simulation for a steady
    state of a drop/bubble on a wall
    '''

    # extract length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_cv, dim2d_R
    # and dim2d_K from the dim2d_parameters.f fortran file
    dim2dParamPath = os.path.join(os.getenv('augeanstables'),
                                  'src',
                                  'physical_models',
                                  'dim2d',
                                  'dim2d_parameters.f')

    if(os.path.isfile(dim2dParamPath)):
        length_c  = float(get_parameter('length_c', dim2dParamPath))
        dim2d_a   = float(get_parameter( 'dim2d_a', dim2dParamPath))
        dim2d_b   = float(get_parameter( 'dim2d_b', dim2dParamPath))
        dim2d_M   = float(get_parameter( 'dim2d_M', dim2dParamPath))
        dim2d_cv  = float(get_parameter('dim2d_cv', dim2dParamPath))
        dim2d_R   = float(get_parameter( 'dim2d_R', dim2dParamPath))
        dim2d_K   = float(get_parameter( 'dim2d_K', dim2dParamPath))

    else:
        sys.exit('*** '+dim2dParamPath+' does not exist***')


    # compute the Weber number
    we = get_we(length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_K)
    if(debug): print 'we: ', we

    # compute the interface length from the temperature
    interface_lgh = get_interface_length(we,temperature)
    if(debug): print 'interface_length: ', interface_lgh

    # compute the bubble diameter from the interface length
    bubble_diameter = 3.0*interface_lgh*sqrt(2.0) #get_bubble_diameter(interface_lgh,
                                        #  ratio_bubble_interface)
    if(debug): print 'bubble_diameter: ', bubble_diameter
    

    # compute the x_max of the domain
    eq_length = get_equilibrium_length(micro_contact_angle,bubble_diameter)
    x_max     = get_x_max(bubble_diameter,eq_length,ratio_eq_length_domain)
    if(debug): print 'x_max: ', x_max

    # compute the y_max of the domain
    y_max     = x_max #get_y_max(bubble_diameter,ratio_drop_length_domain)
    if(debug): print 'y_max: ', y_max

    # compute the maximum space step from the interface length
    dx_max = get_interface_space_step(interface_lgh,
                                      nb_pts_in_interface)
    if(debug): print 'dx_max: ', dx_max

    # compute the extent of the domain as a matrix
    # x_min : domain_extent[0][0]
    # x_max : domain_extent[1][0]
    # y_min : domain_extent[0][1]
    # y_max : domain_extent[1][1]
    domain_extent = get_wall_domain_extent(x_max,y_max,dx_max)
    if(debug): print 'domain_extent: ', domain_extent

    # compute the reduced heat capacity
    cv_r = get_cv_r(dim2d_M,dim2d_cv,dim2d_R)

    # compute the maximum speed of sound in the flow
    speed_of_sound = get_max_speed_of_sound(temperature,cv_r)

    # compute the maximum time step ensuring numerical stability
    flow_velocity = 0.0
    speed_max     = speed_of_sound
    if(debug): print 'speed_of_sound: ', speed_of_sound

    dt_max        = get_dt_max(dx_max,speed_max,CFL_constant,precision_c=6)
    if(debug): print 'dt_max: ', dt_max

    # determine the maximum simulation time
    simulation_time = 200.0

    # determine the detail print
    detail_print = get_detail_print(total_nb_files,
                                    simulation_time,
                                    dt_max)
    if(debug): print 'detail_print: ', detail_print

    # initial conditions:
    # either half sphere with 90 contact angle
    # or spherical cap constrained by contact angle
    if(spherical_cap):
        ic_choice = spherical_cap_ic_choice
    else:
        ic_choice = half_sphere_ic_choice


    # gather the inputs to be modified in a dictionnary
    inputsToBeModified = {
        'detail_print'                : detail_print,
        'dt'                          : dt_max,
        't_max'                       : simulation_time,
        'steady_state_ac'             : steady_state_ac,
        'dx'                          : dx_max,
        'x_min'                       : domain_extent[0][0],
        'x_max'                       : domain_extent[1][0],
        'dy'                          : dx_max,
        'y_min'                       : domain_extent[0][1],
        'y_max'                       : domain_extent[1][1],
        'ic_choice'                   : ic_choice,
        'flow_velocity'               : flow_velocity,
        'temperature'                 : temperature,
        'wall_micro_contact_angle'    : micro_contact_angle,
        'phase_at_center'             : phase_at_center,
        'gravity_ac'                  : gravity_ac,
        'gravity_amp'                 : gravity_amp}

    return inputsToBeModified
def compute_inputsToBeModified(temperature,
                               flow_velocity,
                               nb_pts_in_interface,
                               ratio_interface_separation,
                               ratio_bubble_interface,
                               ratio_interface_influence,
                               CFL_constant,
                               total_nb_files,
                               dct_distance,
                               md_threshold_ac,
                               md_threshold,
                               flow_direction='x'):

    """Determine the inputs to be modified in the input.txt
    file for the simulation with two bubbles transported by the
    mean flow

    Args:
        temperature (double) : mean flow temperature
        flow_velocity (double) : mean flow velocity
        nb_pts_in_interface (int) : number of grid-points needed
            to resolve the interface profile
        ratio_interface_separation (double) : length (expressed as
            a fraction of the width of the interface) separating the
           two bubbles
        ratio_bubble_interface (double) : ratio between bubble
           diameter and interface width
        ratio_interface_influence (double) : length threshold
           (expressed as a fraction of the width of the interface)
           above which the interface is not supposed to interact
           with the border
        CFL_constant (double) : CFL threshold (0-1)
        total_nb_files (int) : total nb of files written
        dct_distance (int) : distance between the detectors and
           the boundary expressed as a number of gridpoints
        md_threshold_ac (int) : activate the increase of the computational
           domain with a mass density threshold
        md_threshold (double) : mass density threshold to activate the
           increase of the computational domain (between 0 and 1)

    Returns:
        inputsToBeModified (dict) :
           - dict[key] : name of the input to be modified
           - dict[value] : value of the input to be modified
    """

    # extract length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_cv, dim2d_R
    # and dim2d_K from the dim2d_parameters.f fortran file
    dim2dParamPath = os.path.join(os.getenv('augeanstables'),
                                  'src',
                                  'physical_models',
                                  'dim2d',
                                  'dim2d_parameters.f')

    if(os.path.isfile(dim2dParamPath)):
        length_c  = float(get_parameter('length_c', dim2dParamPath))
        dim2d_a   = float(get_parameter( 'dim2d_a', dim2dParamPath))
        dim2d_b   = float(get_parameter( 'dim2d_b', dim2dParamPath))
        dim2d_M   = float(get_parameter( 'dim2d_M', dim2dParamPath))
        dim2d_cv  = float(get_parameter('dim2d_cv', dim2dParamPath))
        dim2d_R   = float(get_parameter( 'dim2d_R', dim2dParamPath))
        dim2d_K   = float(get_parameter( 'dim2d_K', dim2dParamPath))

    else:
        sys.exit('*** '+dim2dParamPath+' does not exist***')


    # compute the Weber number
    we = get_we(length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_K)


    # compute the interface length from the temperature
    interface_length = get_interface_length(we,temperature)

    
    # compute the bubble diameter
    bubble_diameter = interface_length*ratio_bubble_interface

    
    # compute the extent of the domain
    [Lx,Ly] = get_domain_sizes(bubble_diameter,
                               interface_length,
                               ratio_interface_influence,
                               ratio_interface_separation)


    # compute the maximum space step from the interface length
    dx_max = get_interface_space_step(interface_length,
                                      nb_pts_in_interface)


    # get the extent of the domain
    # x_min : domain_extent[0][0]
    # x_max : domain_extent[1][0]
    # y_min : domain_extent[0][1]
    # y_max : domain_extent[1][1]
    domain_extent = get_domain_extent(Lx,Ly,dx_max,dx_max)

    
    # compute the reduced heat capacity
    cv_r = get_cv_r(dim2d_M,dim2d_cv,dim2d_R)


    # compute the maximum speed of sound in the flow
    speed_of_sound = get_max_speed_of_sound(temperature,cv_r)


    # compute the maximum time step ensuring numerical stability
    speed_max = speed_of_sound + abs(flow_velocity)
    dt_max    = get_dt_max(dx_max,speed_max,CFL_constant)


    # determine the simulation time needed to let the two bubbles
    # leave the computational domain
    simulation_time = get_simulation_time(Lx,
                                          bubble_diameter,
                                          ratio_interface_separation*interface_length,
                                          ratio_interface_influence*interface_length,
                                          flow_velocity)


    # determine the detail print
    detail_print = get_detail_print(total_nb_files,
                                    simulation_time,
                                    dt_max)


    # gather the inputs to be modified in a dictionnary
    inputsToBeModified = {
        'detail_print'                : detail_print,
        'dt'                          : dt_max,
        't_max'                       : simulation_time,
        'dx'                          : dx_max,
        'dy'                          : dx_max,
        'flow_velocity'               : flow_velocity,
        'temperature'                 : temperature,
        'li_separation'               : ratio_interface_separation,
        'openbc_detector_distance'    : dct_distance,
        'openbc_md_threshold_ac'      : md_threshold_ac,
        'openbc_md_threshold'         : md_threshold}

    if(flow_direction=='x'):

        inputsToBeModified.update({'x_min'          : domain_extent[0][0],
                                   'x_max'          : domain_extent[1][0],
                                   'y_min'          : domain_extent[0][1],
                                   'y_max'          : domain_extent[1][1],
                                   'flow_direction' : 'E'})
    else:

        if(flow_direction!='y'):
            sys.exit('create_bb_tr_input: '+
                     'compute_inputsToBeModified: '+
                     'flow_direction not recognized')
            sys.exit(2)

        inputsToBeModified.update({'x_min'          : domain_extent[0][1],
                                   'x_max'          : domain_extent[1][1],
                                   'y_min'          : domain_extent[0][0],
                                   'y_max'          : domain_extent[1][0],
                                   'flow_direction' : 'N'})

    return inputsToBeModified
def get_mass_for_contour(coord1,coord2,gradMass,phase_check):
    '''
    @description: extraction of the mass at [coord1,coord2]
    and phase verification if enabled
    '''

    #=========================================================
    # extract the mass at the location of the maximum gradient
    #=========================================================
    visit.AddPlot("Pseudocolor","mass", 1, 1)
    visit.DrawPlots()

    if(add_sleep):
        time.sleep(time_sleep)

    midMass = visit.NodePick(coord=(coord1,coord2,0),
                             vars=("default","mass"))
    midMass = midMass['mass']

    visit.DeleteActivePlots()


    #====================================================
    # determine the temperature at the interface location
    #====================================================
    # definition of the kortweg energy
    visit.DefineScalarExpression("mass_grad_squared", "mass_grad_x^2+mass_grad_y^2")
    visit.DefineScalarExpression("we", str(we))
    visit.DefineScalarExpression("korteweg_energy", "0.5/we*mass_grad_squared")
    
    # definition of the kinetic energy
    visit.DefineScalarExpression("velocity_x", "momentum_x/mass")
    visit.DefineScalarExpression("velocity_y", "momentum_y/mass")
    visit.DefineScalarExpression("kinetic_energy", "0.5*mass*(velocity_x^2+velocity_y^2)")
    
    # definition of the temperature
    visit.DefineScalarExpression("cv_r", str(cv_r))
    visit.DefineScalarExpression("temperature", "3/(8*cv_r)*(1/mass*(energy-kinetic_energy-korteweg_energy)+3*mass)")
    
    # draw the temperature and extract the temperature at the maximum gradient point
    visit.AddPlot("Pseudocolor","temperature", 1, 1)
    visit.DrawPlots()

    midTemperature = visit.NodePick(coord=(coord1,coord2,0), vars=("default","temperature"))
    midTemperature = midTemperature['temperature']

    if(add_sleep):
        time.sleep(time_sleep)
        print midTemperature

    visit.DeleteActivePlots()


    #====================================================
    # determine the norm of the temperature gradient
    # at the interface location
    #====================================================
    # definition of the temperature gradient
    visit.DefineScalarExpression("dTdx", "gradient(temperature)[0]")
    visit.DefineScalarExpression("dTdy", "gradient(temperature)[1]")
    visit.DefineScalarExpression("temperature_grad_norm", "sqrt(dTdx^2+dTdy^2)")

    visit.AddPlot("Pseudocolor","temperature_grad_norm", 1, 1)
    visit.DrawPlots()

    midTemperatureGrad = visit.NodePick(coord=(coord1,coord2,0), vars=("default","temperature_grad_norm"))
    midTemperatureGrad = midTemperatureGrad['temperature_grad_norm']

    if(add_sleep):
        time.sleep(time_sleep)
        print midTemperatureGrad

    visit.DeleteActivePlots()


    #==========================================================
    # if the phase check is enabled, determine whether the mass
    # density extracted corresponds to a potential mass density
    # at the location of the interface
    #==========================================================
    if(phase_check):
    
        # deduce the mass densities of the liquid and vapor phases at this temperature
        mass_vap = get_mass_density_vapor(midTemperature)
        mass_liq = get_mass_density_liquid(midTemperature)
    
        # check whether the mass density extracted is close enough from the
        # mid mass density
        mid_mass_c = 0.5*(mass_vap+mass_liq)
        check = abs((midMass-mid_mass_c)/(mass_liq-mass_vap)) < 0.4

        if(add_sleep):
            print 'check_mass: ', check
    
        # check by comparing the maximum gradient of the mass density
        interface_lgh = get_interface_length(we,midTemperature)
        mid_gradMass = abs((mass_liq-mass_vap)/interface_lgh)
        check_grad = (abs(gradMass)>0.9*mid_gradMass) #0.9

        if(debug):
            print 'check_md_grad: ', (abs(gradMass)>0.9*mid_gradMass)
            print 'diff: ', abs(gradMass)/mid_gradMass

        check = check and check_grad

    else:
        check = True
    

    return [midMass,check,midTemperature,midTemperatureGrad]
def get_inputsToBeModified(temperature,
                           flow_velocity,
                           dct_distance,
                           md_threshold_ac,
                           md_threshold,
                           ic_perturbation_ac,
                           ic_perturbation_amp,
                           li_perturbation_ac,
                           li_perturbation_amp,
                           bc_perturbation_T0_ac,
                           bc_perturbation_T0_amp,
                           bc_perturbation_vx0_ac,
                           bc_perturbation_vx0_amp,
                           bc_perturbation_vy0_ac,
                           bc_perturbation_vy0_amp,
                           nb_pts_in_interface,
                           ratio_bubble_interface,
                           CFL_constant,
                           ratio_interface_influence,
                           total_nb_files):
    '''
    @description:
    compute the inputs that are modified in the template.txt
    template input file to run the DIM2D simulation on a small
    and on a large domains
    '''

    # extract length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_cv, dim2d_R
    # and dim2d_K from the dim2d_parameters.f fortran file
    dim2dParamPath = os.path.join(os.getenv('augeanstables'),
                                  'src',
                                  'physical_models',
                                  'dim2d',
                                  'dim2d_parameters.f')

    if(os.path.isfile(dim2dParamPath)):
        length_c  = float(get_parameter('length_c', dim2dParamPath))
        dim2d_a   = float(get_parameter( 'dim2d_a', dim2dParamPath))
        dim2d_b   = float(get_parameter( 'dim2d_b', dim2dParamPath))
        dim2d_M   = float(get_parameter( 'dim2d_M', dim2dParamPath))
        dim2d_cv  = float(get_parameter('dim2d_cv', dim2dParamPath))
        dim2d_R   = float(get_parameter( 'dim2d_R', dim2dParamPath))
        dim2d_K   = float(get_parameter( 'dim2d_K', dim2dParamPath))

    else:
        sys.exit('*** '+dim2dParamPath+' does not exist***')


    # compute the Weber number
    we = get_we(length_c, dim2d_a, dim2d_b, dim2d_M, dim2d_K)
    #print "we: "+str(we)


    # compute the interface length from the temperature
    interface_lgh = get_interface_length(we,temperature)
    #print "interface_lgh: "+str(interface_lgh)


    # compute the bubble diameter from the interface length
    bubble_diameter = get_bubble_diameter(interface_lgh,
                                          ratio_bubble_interface)

    # compute the domain length from the bubble diameter
    domain_length = get_domain_length(bubble_diameter,
                                      interface_lgh,
                                      ratio_interface_influence)


    # compute the maximum space step from the interface length
    dx_max = get_interface_space_step(interface_lgh,
                                      nb_pts_in_interface)


    # compute the extent of the small domain as a matrix:
    # x_min : small_domain_extent[0][0]
    # x_max : small_domain_extent[1][0]
    # y_min : small_domain_extent[0][1]
    # y_max : small_domain_extent[1][1]
    small_domain_extent = get_small_domain_extent(domain_length,
                                                  dx_max)


    # compute the reduced heat capacity
    cv_r = get_cv_r(dim2d_M,dim2d_cv,dim2d_R)


    # compute the maximum speed of sound in the flow
    speed_of_sound = get_max_speed_of_sound(temperature,cv_r)


    # compute the maximum time step ensuring numerical stability
    speed_max = speed_of_sound + abs(flow_velocity)
    dt_max    = get_dt_max(dx_max,speed_max,CFL_constant)


    # determine the simulation time needed to let the bubble
    # leave the computational domain
    simulation_time = get_simulation_time(domain_length,
                                          bubble_diameter,
                                          interface_lgh,
                                          ratio_interface_influence,
                                          flow_velocity)


    # determine the detail print
    detail_print = get_detail_print(total_nb_files,
                                    simulation_time,
                                    dt_max)


    # determine the large domain extent
    large_domain_extent = get_large_domain_extent(small_domain_extent,
                                                  dx_max,dx_max,
                                                  flow_velocity,
                                                  speed_of_sound,
                                                  simulation_time)


    # gather the inputs to be modified in dictionnaries
    inputsToBeModified_sm_domain = {
        'detail_print'                : detail_print,
        'dt'                          : dt_max,
        't_max'                       : simulation_time,
        'dx'                          : dx_max,
        'x_min'                       : small_domain_extent[0][0],
        'x_max'                       : small_domain_extent[1][0],
        'dy'                          : dx_max,
        'y_min'                       : small_domain_extent[0][1],
        'y_max'                       : small_domain_extent[1][1],
        'flow_velocity'               : flow_velocity,
        'temperature'                 : temperature,
        'openbc_detector_distance'    : dct_distance,
        'openbc_md_threshold_ac'      : md_threshold_ac,
        'openbc_md_threshold'         : md_threshold,
        'ic_perturbation_ac'          : ic_perturbation_ac,
        'ic_perturbation_amp'         : ic_perturbation_amp,
        'li_perturbation_ac'          : li_perturbation_ac,
        'li_perturbation_amp'         : li_perturbation_amp,
        'openbc_perturbation_T0_ac'   : bc_perturbation_T0_ac,
        'openbc_perturbation_T0_amp'  : bc_perturbation_T0_amp,
        'openbc_perturbation_vx0_ac'  : bc_perturbation_vx0_ac,
        'openbc_perturbation_vx0_amp' : bc_perturbation_vx0_amp,
        'openbc_perturbation_vy0_ac'  : bc_perturbation_vy0_ac,
        'openbc_perturbation_vy0_amp' : bc_perturbation_vy0_amp}

    inputsToBeModified_lg_domain = {
        'detail_print'                : detail_print,
        'dt'                          : dt_max,
        't_max'                       : simulation_time,
        'dx'                          : dx_max,
        'x_min'                       : large_domain_extent[0][0],
        'x_max'                       : large_domain_extent[1][0],
        'dy'                          : dx_max,
        'y_min'                       : large_domain_extent[0][1],
        'y_max'                       : large_domain_extent[1][1],
        'flow_velocity'               : flow_velocity,
        'temperature'                 : temperature,
        'openbc_md_threshold_ac'      : 0,
        'openbc_md_threshold'         : 0.0,
        'ic_perturbation_ac'          : 0,
        'ic_perturbation_amp'         : 0.0,
        'li_perturbation_ac'          : li_perturbation_ac,
        'li_perturbation_amp'         : li_perturbation_amp,
        'openbc_perturbation_T0_ac'   : 0,
        'openbc_perturbation_T0_amp'  : 0.0,
        'openbc_perturbation_vx0_ac'  : 0,
        'openbc_perturbation_vx0_amp' : 0.0,
        'openbc_perturbation_vy0_ac'  : 0,
        'openbc_perturbation_vy0_amp' : 0.0}

        
    return [inputsToBeModified_sm_domain,
            inputsToBeModified_lg_domain]