<commit_before>import fenics
import globals
import default


inner, dot, grad, div, sym = fenics.inner, fenics.dot, fenics.grad, fenics.div, fenics.sym

def a(mu, u, v):

    def D(u):
    
        return sym(grad(u))
    
    return 2.*mu*inner(D(u), D(v))


def b(u, q):
    
    return -div(u)*q
    

def c(w, z, v):
   
    return dot(dot(grad(z), w), v)


class FormFactory():
    
    def __init__(self, W, parameters = default.parameters, m_B = default.m_B, ddtheta_m_B = default.ddtheta_m_B, regularization = default.regularization):
        
        self.parameters = parameters
        
        self.m_B = m_B
        
        self.ddtheta_m_B = ddtheta_m_B
        
        self.regularization = regularization
        
        self.W = W
        
        self.Re = fenics.Constant(globals.Re)
    
    
    def make_nonlinear_form(self, dt, w_k, w_n, automatic_jacobian=True):
    
        dt = fenics.Constant(dt)
        
        Ra = fenics.Constant(self.parameters['Ra']), 
        
        Pr, Ste = fenics.Constant(self.parameters['Pr']), fenics.Constant(self.parameters['Ste'])
        
        C, K =  fenics.Constant(self.parameters['C']), fenics.Constant(self.parameters['K'])
        
        g, gamma = fenics.Constant(self.parameters['g']), fenics.Constant(self.parameters['gamma'])
        
        mu_l, mu_s = fenics.Constant(self.parameters['mu_l']), fenics.Constant(self.parameters['mu_s'])
        
        m_B = self.m_B
        
        a_s, theta_s = fenics.Constant(self.regularization['a_s']), fenics.Constant(self.regularization['theta_s'])
        
        R_s = fenics.Constant(self.regularization['R_s'])
        
        heaviside_tanh = lambda theta, f_s, f_l: f_l + (f_s - f_l)/2.*(1. + fenics.tanh(a_s*(theta_s - theta)/R_s))
        
        S_s = fenics.Constant(0.)
        
        S_l = fenics.Constant(1./Ste)
        
        S = lambda theta : heaviside_tanh(theta, f_s=S_s, f_l=S_l)
        
        u_n, p_n, theta_n = fenics.split(w_n)
        
        
        # @todo: Variable viscosity 
        
        #mu_sl = lambda theta : heaviside_tanh(theta, f_s=mu_s, f_l=mu_l)
        
        mu_sl = fenics.Constant(mu_l)
        
        
        #
        w_w = fenics.TrialFunction(self.W)
        
        u_w, p_w, theta_w = fenics.split(w_w)
        
        v, q, phi = fenics.TestFunctions(self.W)
        
        u_k, p_k, theta_k = fenics.split(w_k)
        
        F = (
            b(u_k, q) - gamma*p_k*q
            + dot(u_k, v)/dt + c(u_k, u_k, v) + a(mu_sl, u_k, v) + b(v, p_k)
            + dot(m_B(theta_k)*g, v) 
            + C*(theta_k + S(theta_k))*phi/dt - dot(u_k, grad(phi))*C*theta_k + dot(K/Pr*grad(theta_k), grad(phi)) 
            - dot(u_n, v)/dt 
            - C*(theta_n + S(theta_n))*phi/dt
            )*fenics.dx

        if automatic_jacobian:

            JF = fenics.derivative(F, w_k, w_w)
            
        else:
        
            ddtheta_m_B = self.ddtheta_m_B
            
            ddtheta_heaviside_tanh = lambda theta, f_s, f_l: -(a_s*(fenics.tanh((a_s*(theta_s - theta))/R_s)**2 - 1.)*(f_l/2. - f_s/2.))/R_s
            
            ddtheta_S = lambda theta : ddtheta_heaviside_tanh(theta, f_s=S_s, f_l=S_l)   
        
            # @todo: Variable viscosity 
            
            #ddtheta_mu_sl = lambda theta : ddtheta_heaviside_tanh(theta, f_s=mu_s, f_l=mu_l)        
        
            ddtheta_mu_sl = fenics.Constant(0.)
        
            JF = (
                b(u_w, q) - gamma*p_w*q 
                + dot(u_w, v)/dt + c(u_k, u_w, v) + c(u_w, u_k, v) + a(theta_w*ddtheta_mu_sl, u_k, v) + a(mu_sl, u_w, v) + b(v, p_w) 
                + dot(theta_w*ddtheta_m_B(theta_k)*g, v)
                + C*theta_w*phi/dt + C*theta_w*ddtheta_S(theta_k)*phi/dt
                - dot(u_w, grad(phi))*C*theta_k - dot(u_k, grad(phi))*C*theta_w
                + K/Pr*dot(grad(theta_w), grad(phi))
                )*fenics.dx

        
        return F, JF
        <commit_msg>Improved readability and removed some grouping of terms<commit_after>import fenics
import globals
import default


inner, dot, grad, div, sym = fenics.inner, fenics.dot, fenics.grad, fenics.div, fenics.sym

def a(mu, u, v):

    def D(u):
    
        return sym(grad(u))
    
    return 2.*mu*inner(D(u), D(v))


def b(u, q):
    
    return -div(u)*q
    

def c(w, z, v):
   
    return dot(dot(grad(z), w), v)


class FormFactory():
    
    def __init__(self, W, parameters = default.parameters, m_B = default.m_B, ddtheta_m_B = default.ddtheta_m_B, regularization = default.regularization):
        
        self.parameters = parameters
        
        self.m_B = m_B
        
        self.ddtheta_m_B = ddtheta_m_B
        
        self.regularization = regularization
        
        self.W = W
        
        self.Re = fenics.Constant(globals.Re)
    
    
    def make_nonlinear_form(self, dt, w_k, w_n, automatic_jacobian=True):
    
        dt = fenics.Constant(dt)
        
        Ra = fenics.Constant(self.parameters['Ra']), 
        
        Pr, Ste = fenics.Constant(self.parameters['Pr']), fenics.Constant(self.parameters['Ste'])
        
        C, K =  fenics.Constant(self.parameters['C']), fenics.Constant(self.parameters['K'])
        
        g, gamma = fenics.Constant(self.parameters['g']), fenics.Constant(self.parameters['gamma'])
        
        mu_l, mu_s = fenics.Constant(self.parameters['mu_l']), fenics.Constant(self.parameters['mu_s'])
        
        m_B = self.m_B
        
        a_s, theta_s = fenics.Constant(self.regularization['a_s']), fenics.Constant(self.regularization['theta_s'])
        
        R_s = fenics.Constant(self.regularization['R_s'])
        
        heaviside_tanh = lambda theta, f_s, f_l: f_l + (f_s - f_l)/2.*(1. + fenics.tanh(a_s*(theta_s - theta)/R_s))
        
        S_s = fenics.Constant(0.)
        
        S_l = fenics.Constant(1./Ste)
        
        S = lambda theta : heaviside_tanh(theta, f_s=S_s, f_l=S_l)
        
        u_n, p_n, theta_n = fenics.split(w_n)
        
        
        # @todo: Variable viscosity 
        
        #mu_sl = lambda theta : heaviside_tanh(theta, f_s=mu_s, f_l=mu_l)
        
        mu_sl = fenics.Constant(mu_l)
        
        
        #
        w_w = fenics.TrialFunction(self.W)
        
        u_w, p_w, theta_w = fenics.split(w_w)
        
        v, q, phi = fenics.TestFunctions(self.W)
        
        u_k, p_k, theta_k = fenics.split(w_k)
        
        f_B = lambda theta : m_B(theta)*g

        F = (
            b(u_k, q) - gamma*p_k*q
            + dot(u_k, v)/dt + c(u_k, u_k, v) + a(mu_sl, u_k, v) + b(v, p_k) + dot(f_B(theta_k), v) - dot(u_n, v)/dt 
            + C*theta_k*phi/dt - dot(u_k, grad(phi))*C*theta_k + K/Pr*dot(grad(theta_k), grad(phi)) - C*theta_n*phi/dt 
            + C*S(theta_k)*phi/dt - C*S(theta_n)*phi/dt
            )*fenics.dx

        if automatic_jacobian:

            JF = fenics.derivative(F, w_k, w_w)
            
        else:
        
            ddtheta_m_B = self.ddtheta_m_B
            
            ddtheta_f_B = lambda theta : ddtheta_m_B(theta)*g
            
            ddtheta_heaviside_tanh = lambda theta, f_s, f_l: -(a_s*(fenics.tanh((a_s*(theta_s - theta))/R_s)**2 - 1.)*(f_l/2. - f_s/2.))/R_s
            
            ddtheta_S = lambda theta : ddtheta_heaviside_tanh(theta, f_s=S_s, f_l=S_l)   
        
            # @todo: Variable viscosity 
            
            #ddtheta_mu_sl = lambda theta : ddtheta_heaviside_tanh(theta, f_s=mu_s, f_l=mu_l)        
        
            ddtheta_mu_sl = fenics.Constant(0.)
        
            JF = (
                b(u_w, q) - gamma*p_w*q 
                + dot(u_w, v)/dt + c(u_k, u_w, v) + c(u_w, u_k, v) + a(theta_w*ddtheta_mu_sl, u_k, v) + a(mu_sl, u_w, v) + b(v, p_w) 
                + dot(theta_w*ddtheta_f_B(theta_k), v)
                + C*theta_w*phi/dt + C*theta_w*ddtheta_S(theta_k)*phi/dt
                - dot(u_w, grad(phi))*C*theta_k - dot(u_k, grad(phi))*C*theta_w
                + K/Pr*dot(grad(theta_w), grad(phi))
                )*fenics.dx

        
        return F, JF
        
{'fraction_non_alphanumeric': 0.13538461538461538, 'fraction_numerical': 0.0024615384615384616, 'mean_word_length': 1.7790697674418605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6674296', 'n_tokens_mistral': 3246, 'n_tokens_neox': 3101, 'n_words': 503}