<commit_before>'''

    @brief  Solve the benchmark "differentially heated cavity" natural convection problem using finite elements.

    @detail
        
        Solve the natural convection test problem from

            @article
            {danaila2014newton,
              title={A Newton method with adaptive finite elements for solving phase-change problems with natural convection},
              author={<NAME> and <NAME> and <NAME>{\'e}d{\'e}ric and <NAME>{\'e}phane},
              journal={Journal of Computational Physics},
              volume={274},
              pages={826--840},
              year={2014},
              publisher={Academic Press}
            }
        
        Match the notation in danaila2014newton as best as possible.

    
    @author <NAME> <<EMAIL>>
    
'''

from fenics import \
    UnitSquareMesh, FiniteElement, VectorElement, MixedElement, \
    FunctionSpace, VectorFunctionSpace, \
    Function, TrialFunction, TestFunctions, split, \
    DirichletBC, Constant, Expression, \
    dx, \
    dot, inner, grad, nabla_grad, sym, div, \
    errornorm, norm, \
    File, \
    Progress, set_log_level, PROGRESS, \
    project, interpolate, \
    solve, parameters, info, derivative, \
    LinearVariationalProblem, LinearVariationalSolver, NonlinearVariationalProblem, NonlinearVariationalSolver, \
    SubDomain, EdgeFunction, near, adapt


def arguments():
    """Returns tuple containing dictionary of calling function's
       named arguments and a list of calling function's unnamed
       positional arguments.
    """
    from inspect import getargvalues, stack
    posname, kwname, args = getargvalues(stack()[1][0])[-3:]
    posargs = args.pop(posname, [])
    args.update(args.pop(kwname, []))
    return args, posargs

    
def run(
    output_dir = 'output_nsb_pcm', \
    Ra = 1.e6, \
    Pr = 0.71, \
    K = 1., \
    g = (0., -1.), \
    mu = 1., \
    mesh = UnitSquareMesh(20, 20, "crossed"), \
    s_u = ('0.', '0.'), \
    s_p = '0.', \
    s_theta = '0.', \
    initial_values_expression = ( \
        '0.', \
        '0.', \
        '0.', \
        '0.5*near(x[0],  0.) -0.5*near(x[0],  1.)'), \
    bc_expressions = [ \
        [0, ('0.', '0.'), 3, 'near(x[0],  0.) | near(x[0],  1.) | near(x[1], 0.) | near(x[1],  1.)'], \
        [2, '0.', 2, 'near(x[0],  0.)'], \
        [2, '0.', 2, 'near(x[0],  1.)']], \
    final_time = 1., \
    time_step_size = 1.e-3, \
    adaptive_time = True, \
    gamma = 1.e-7, \
    pressure_degree = 1, \
    temperature_degree = 1, \
    linearize = True, \
    newton_relative_tolerance = 1.e-9, \
    max_newton_iterations = 10, \
    stop_when_steady = True, \
    steady_relative_tolerance = 1.e-8, \
    debug_b_factor = 1., \
    debug_c_factor = 1., \
    exact_solution_expression = [] \
    ):

    #
    print("Running nsb_pcm with the following arguments: \n")
    
    print(arguments())
    
    # @todo Try 3D
    dim = 2
    
    '''
    Per Equation 8 from danaila2014newton, we implement an equation scaled with v_ref = nu_liquid/H => t_ref = nu_liquid/H^2 => Re = 1.
    ''' 
    Re = 1.
    
    # Compute derived parameters
    velocity_degree = pressure_degree + 1
    

    # Define function spaces for the system
    VxV = VectorFunctionSpace(mesh, 'P', velocity_degree)

    Q = FunctionSpace(mesh, 'P', pressure_degree) # @todo mixing up test function space

    V = FunctionSpace(mesh, 'P', temperature_degree)

    '''
    MixedFunctionSpace used to be available but is now deprecated. 
    The way that fenics separates function spaces and elements is confusing.<commit_msg>Cleaned up stdout<commit_after>'''

    @brief  Solve the benchmark "differentially heated cavity" natural convection problem using finite elements.

    @detail
        
        Solve the natural convection test problem from

            @article
            {danaila2014newton,
              title={A Newton method with adaptive finite elements for solving phase-change problems with natural convection},
              author={<NAME> and <NAME> and <NAME>{\'e}d{\'<NAME> <NAME>{\'e}phane},
              journal={Journal of Computational Physics},
              volume={274},
              pages={826--840},
              year={2014},
              publisher={Academic Press}
            }
        
        Match the notation in danaila2014newton as best as possible.

    
    @author <NAME> <<EMAIL>>
    
'''

from fenics import \
    UnitSquareMesh, FiniteElement, VectorElement, MixedElement, \
    FunctionSpace, VectorFunctionSpace, \
    Function, TrialFunction, TestFunctions, split, \
    DirichletBC, Constant, Expression, \
    dx, \
    dot, inner, grad, nabla_grad, sym, div, \
    errornorm, norm, \
    File, \
    Progress, set_log_level, PROGRESS, \
    project, interpolate, \
    solve, parameters, info, derivative, \
    LinearVariationalProblem, LinearVariationalSolver, NonlinearVariationalProblem, NonlinearVariationalSolver, \
    SubDomain, EdgeFunction, near, adapt


def arguments():
    """Returns tuple containing dictionary of calling function's
       named arguments and a list of calling function's unnamed
       positional arguments.
    """
    from inspect import getargvalues, stack
    posname, kwname, args = getargvalues(stack()[1][0])[-3:]
    posargs = args.pop(posname, [])
    args.update(args.pop(kwname, []))
    return args, posargs

    
def run(
    output_dir = 'output_nsb_pcm', \
    Ra = 1.e6, \
    Pr = 0.71, \
    K = 1., \
    g = (0., -1.), \
    mu = 1., \
    mesh = UnitSquareMesh(20, 20, "crossed"), \
    s_u = ('0.', '0.'), \
    s_p = '0.', \
    s_theta = '0.', \
    initial_values_expression = ( \
        '0.', \
        '0.', \
        '0.', \
        '0.5*near(x[0],  0.) -0.5*near(x[0],  1.)'), \
    bc_expressions = [ \
        [0, ('0.', '0.'), 3, 'near(x[0],  0.) | near(x[0],  1.) | near(x[1], 0.) | near(x[1],  1.)'], \
        [2, '0.', 2, 'near(x[0],  0.)'], \
        [2, '0.', 2, 'near(x[0],  1.)']], \
    final_time = 1., \
    time_step_size = 1.e-3, \
    adaptive_time = True, \
    gamma = 1.e-7, \
    pressure_degree = 1, \
    temperature_degree = 1, \
    linearize = True, \
    newton_relative_tolerance = 1.e-9, \
    max_newton_iterations = 10, \
    stop_when_steady = True, \
    steady_relative_tolerance = 1.e-8, \
    debug_b_factor = 1., \
    debug_c_factor = 1., \
    exact_solution_expression = [] \
    ):

    #
    print("Running nsb_pcm with the following arguments:")
    
    print(arguments())
    
    # @todo Try 3D
    dim = 2
    
    '''
    Per Equation 8 from danaila2014newton, we implement an equation scaled with v_ref = nu_liquid/H => t_ref = nu_liquid/H^2 => Re = 1.
    ''' 
    Re = 1.
    
    # Compute derived parameters
    velocity_degree = pressure_degree + 1
    

    # Define function spaces for the system
    VxV = VectorFunctionSpace(mesh, 'P', velocity_degree)

    Q = FunctionSpace(mesh, 'P', pressure_degree) # @todo mixing up test function space

    V = FunctionSpace(mesh, 'P', temperature_degree)

    '''
    MixedFunctionSpace used to be available but is now deprecated. 
    The way that fenics separates function spaces and elements is confusing.
{'fraction_non_alphanumeric': 0.13433250069386624, 'fraction_numerical': 0.027754648903691368, 'mean_word_length': 2.293875685557587, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5160402', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2297, 'n_words': 619}