<commit_before>
def pznd(state, parameters):
    '''Calculate sources and sinks for a simple PZND model'''
    
    if not check_pznd_parameters(parameters):
        raise TypeError("Missing Parameter")
    
    P=state.scalar_fields["Phytoplankton"]
    Z=state.scalar_fields["Zooplankton"]
    N=state.scalar_fields["Nutrient"]
    D=state.scalar_fields["Detritus"]
    I=state.scalar_fields["PhotosyntheticRadiation"]
    Pnew=state.scalar_fields["IteratedPhytoplankton"]
    Znew=state.scalar_fields["IteratedZooplankton"]
    Nnew=state.scalar_fields["IteratedNutrient"]
    Dnew=state.scalar_fields["IteratedDetritus"]


    P_source=state.scalar_fields["PhytoplanktonSource"]
    Z_source=state.scalar_fields["ZooplanktonSource"]
    N_source=state.scalar_fields["NutrientSource"]
    D_source=state.scalar_fields["DetritusSource"]
    try:
        PP=state.scalar_fields["PrimaryProduction"]
    except KeyError:
        PP=None
    try:
        PG=state.scalar_fields["PhytoplanktonGrazing"]
    except KeyError:
        PG=None
    

    alpha=parameters["alpha"]
    beta=parameters["beta"]
    gamma=parameters["gamma"]
    g=parameters["g"]
    k_N=parameters["k_N"]
    k=parameters["k"]
    v=parameters["v"]
    mu_P=parameters["mu_P"]
    mu_Z=parameters["mu_Z"]
    mu_D=parameters["mu_D"]
    p_P=parameters["p_P"]
    p_D=1-p_P

    for n in range(P.node_count):
        # Values of fields on this node.
        P_n=max(.5*(P.node_val(n)+Pnew.node_val(n)), 0.0)
        Z_n=max(.5*(Z.node_val(n)+Znew.node_val(n)), 0.0)
        N_n=max(.5*(N.node_val(n)+Nnew.node_val(n)), 0.0)
        D_n=max(.5*(D.node_val(n)+Dnew.node_val(n)), 0.0)
        I_n=max(I.node_val(n), 0.0)

        # Light limited phytoplankton growth rate.
        J=(v*alpha*I_n)/(v**2+alpha**2*I_n**2)**0.5

        # Nitrate limiting factor.
        Q=N_n/(k_N+N_n)

        # Total phytoplankton growth rate.
        P_r=J*P_n*Q

        # Zooplankton grazing of phytoplankton.
        G_P=(g * p_P * P_n**2 * Z_n)/(k**2 + p_P*P_n**2 + p_D*D_n**2)

        # Zooplankton grazing of detritus.
        G_D=(g * (1-p_P) * D_n**2 * Z_n)/(k**2 + p_P*P_n**2 + p_D*D_n**2)

        # Death rate of phytoplankton.
        De_P=mu_P*P_n*P_n/(P_n+1)

        # Death rate of zooplankton.
        De_Z=mu_Z*Z_n**2

        # Detritus remineralisation.
        De_D=mu_D*D_n

        P_source.addto(n, P_r - G_P - De_P)
        
        if PP:
            PP.set(n, P_r)
	if PG:
	    PG.set(n, G_P)

        Z_source.addto(n, gamma*beta*(G_P+G_D) - De_Z)
        
        N_source.addto(n, -P_r + De_D + (1-gamma)*beta*(G_P+G_D))

        D_source.addto(n, -De_D + De_P + De_Z +(1-beta)*G_P - beta*G_D)                     

def check_pznd_parameters(parameters):
    from sys import stderr

    valid=True

    if not parameters.has_key("alpha"):
        stderr.write("PZND parameter alpha missing.\n")
        stderr.write("alpha is this initial slope of the P-I curve.\n\n")<commit_msg>Made some internal variable names more consistent<commit_after>
def pznd(state, parameters):
    '''Calculate sources and sinks for a simple PZND model'''
    
    if not check_pznd_parameters(parameters):
        raise TypeError("Missing Parameter")
    
    P=state.scalar_fields["Phytoplankton"]
    Z=state.scalar_fields["Zooplankton"]
    N=state.scalar_fields["Nutrient"]
    D=state.scalar_fields["Detritus"]
    I=state.scalar_fields["PhotosyntheticRadiation"]
    Pnew=state.scalar_fields["IteratedPhytoplankton"]
    Znew=state.scalar_fields["IteratedZooplankton"]
    Nnew=state.scalar_fields["IteratedNutrient"]
    Dnew=state.scalar_fields["IteratedDetritus"]


    P_source=state.scalar_fields["PhytoplanktonSource"]
    Z_source=state.scalar_fields["ZooplanktonSource"]
    N_source=state.scalar_fields["NutrientSource"]
    D_source=state.scalar_fields["DetritusSource"]
    try:
        PP=state.scalar_fields["PrimaryProduction"]
    except KeyError:
        PP=None
    try:
        PG=state.scalar_fields["PhytoplanktonGrazing"]
    except KeyError:
        PG=None
    

    alpha=parameters["alpha"]
    beta=parameters["beta"]
    gamma=parameters["gamma"]
    g=parameters["g"]
    k_N=parameters["k_N"]
    k=parameters["k"]
    v=parameters["v"]
    mu_P=parameters["mu_P"]
    mu_Z=parameters["mu_Z"]
    mu_D=parameters["mu_D"]
    p_P=parameters["p_P"]
    p_D=1-p_P

    for n in range(P.node_count):
        # Values of fields on this node.
        P_n=max(.5*(P.node_val(n)+Pnew.node_val(n)), 0.0)
        Z_n=max(.5*(Z.node_val(n)+Znew.node_val(n)), 0.0)
        N_n=max(.5*(N.node_val(n)+Nnew.node_val(n)), 0.0)
        D_n=max(.5*(D.node_val(n)+Dnew.node_val(n)), 0.0)
        I_n=max(I.node_val(n), 0.0)

        # Light limited phytoplankton growth rate.
        J=(v*alpha*I_n)/(v**2+alpha**2*I_n**2)**0.5

        # Nitrate limiting factor.
        Q=N_n/(k_N+N_n)

        # Total phytoplankton growth rate.
        R_P=J*P_n*Q

        # Zooplankton grazing of phytoplankton.
        G_P=(g * p_P * P_n**2 * Z_n)/(k**2 + p_P*P_n**2 + p_D*D_n**2)

        # Zooplankton grazing of detritus.
        G_D=(g * (1-p_P) * D_n**2 * Z_n)/(k**2 + p_P*P_n**2 + p_D*D_n**2)

        # Death rate of phytoplankton.
        De_P=mu_P*P_n*P_n/(P_n+1)

        # Death rate of zooplankton.
        De_Z=mu_Z*Z_n**2

        # Detritus remineralisation.
        De_D=mu_D*D_n

        P_source.addto(n, R_P - G_P - De_P)
        
        if PP:
            PP.set(n, R_P)
        if PG:
            PG.set(n, G_P)

        Z_source.addto(n, gamma*beta*(G_P+G_D) - De_Z)
        
        N_source.addto(n, -R_P + De_D + (1-gamma)*beta*(G_P+G_D))

        D_source.addto(n, -De_D + De_P + De_Z +(1-beta)*G_P - beta*G_D)                     

def check_pznd_parameters(parameters):
    from sys import stderr

    valid=True

    if not parameters.has_key("alpha"):
        stderr.write("PZND parameter alpha missing.\n")
        stderr.write("alpha is this initial slope of the P-I curve.\n\n")
{'fraction_non_alphanumeric': 0.14467089876378217, 'fraction_numerical': 0.011025726695623121, 'mean_word_length': 2.82555910543131, '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': '906316', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2310, 'n_words': 345}