def crossbridge_elasticity(C, lambda_a):

    F_a = Matrix([[lambda_a, 0, 0], [0, 1/sqrt(lambda_a), 0], [0, 0, 1/sqrt(lambda_a)]])
    C_e = F_a.inv().T*C*F_a.inv()
    S_XB = 2*F_a.inv()*diff(psi_XB, I4_f_e)*(f0*f0.T)*F_a.inv().T

    # Substitute the current values of the invariants
    print_ccode((f0.T*C_e*f0)[0], assign_to="real I4_f_e")

    return S_XB
Esempio n. 2
0
def crossbridge_elasticity(C, lambda_a):

    F_a = Matrix([[lambda_a, 0, 0], [0, 1 / sqrt(lambda_a), 0],
                  [0, 0, 1 / sqrt(lambda_a)]])
    C_e = F_a.inv().T * C * F_a.inv()
    S_XB = 2 * F_a.inv() * diff(psi_XB, I4_f_e) * (f0 * f0.T) * F_a.inv().T

    # Substitute the current values of the invariants
    print_ccode((f0.T * C_e * f0)[0], assign_to="real I4_f_e")

    return S_XB
def background_elasticity(C):

    # Modified right Cauchy-Green tensor
    C_bar = J**(-Rational(2, 3))*(C)

    # Define the second Piola-Kirchhoff stress in terms of the invariants
    S_bar =   2*(diff(psi_iso, I1_bar) + diff(psi_iso, I2_bar))*I \
            - 2*diff(psi_iso, I2_bar)*C_bar \
            + 2*diff(psi_iso, I4_f_bar)*(f0*f0.T) \
            + 2*diff(psi_iso, I4_s_bar)*(s0*s0.T) \
            + diff(psi_iso, I8_fs_bar)*(f0*s0.T + s0*f0.T) \
            + diff(psi_iso, I8_fn_bar)*(f0*n0.T + n0*f0.T)
    S_bar_contract_C = sum([sum([S_bar[a, b]*C[a, b]
                                 for a in range(3)]) for b in range(3)])
    Dev_S_bar = S_bar - Rational(1, 3)*(S_bar_contract_C)*C.inv()

    S_iso_inf = J**(-Rational(2, 3))*Matrix(Dev_S_bar)
    S_vol_inf = J*diff(psi_vol, J)*C.inv()

    # Substitute the current values of the invariants
    substitutions = {I8_fn_bar: (f0.T*C_bar*n0)[0]}
    print_ccode(C_bar.trace(), assign_to="real I1_bar")
    print_ccode(Rational(1, 2)*(I1_bar*I1_bar - (C_bar*C_bar).trace()), assign_to="real I2_bar")
    print_ccode((f0.T*C_bar*f0)[0], assign_to="real I4_f_bar")
    print_ccode((s0.T*C_bar*s0)[0], assign_to="real I4_s_bar")
    print_ccode((f0.T*C_bar*s0)[0], assign_to="real I8_fs_bar")
    print_ccode((f0.T*C_bar*n0)[0], assign_to="real I8_fn_bar")

    return S_vol_inf, S_iso_inf
symbols("cloc11, cloc12, cloc13, cloc21, cloc22, cloc23, cloc31, cloc32, cloc33")

C = Matrix([[c11, c12, c13],
            [c21, c22, c23],
            [c31, c32, c33]])

print "// Invariants for background material"
S_vol_inf, S_iso_inf = background_elasticity(C)

print
print "// Invariant for crossbridge elasticity"
S_XB = crossbridge_elasticity(C, lambda_a)

print
print "// Isochoric part of the background elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_iso_inf[i, j], assign_to="S_loc_iso(%i, %i)" % (i + 1, j + 1))

print
print "// Volumetric part of the background elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_vol_inf[i, j], assign_to="S_loc_vol(%i, %i)" % (i + 1, j + 1))

print
print "// Cross-bridge elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_XB[i, j], assign_to="S_XB(%i, %i)" % (i + 1, j + 1))
P_a = 0 # something
P_a = -N*E_3*XB_PreR*v
C = N*(E_3*XB_PreR + E_4*XB_PostR)

# Define an arbitrary deformation gradient tensor
f11, f12, f13, f21, f22, f23, f31, f32, f33 = \
symbols("f11, f12, f13, f21, f22, f23, f31, f32, f33")

F = Matrix([[f11, f12, f13],
            [f21, f22, f23],
            [f31, f32, f33]])

# Compute the necessary elastic decompositions
F_e = F*F_a.inv()
C_e = F_e.T*F_e


# Compute the more complicted third  term on the right-hand side of the ODE
term_3a = 2*C_e*diff(psi_2, I4_f_e)*(f0*f0.T)*F_a.inv().T
term_3b = Matrix([[diff(F_a[0, 0], lmbda_a), diff(F_a[0, 1], lmbda_a), diff(F_a[0, 2], lmbda_a)], \
                        [diff(F_a[1, 0], lmbda_a), diff(F_a[1, 1], lmbda_a), diff(F_a[1, 2], lmbda_a)], \
                        [diff(F_a[2, 0], lmbda_a), diff(F_a[2, 1], lmbda_a), diff(F_a[2, 2], lmbda_a)]])
term_3  = sum([sum([term_3a[a, b]*term_3b[a, b]
                        for a in range(3)]) for b in range(3)])

# Define the entire right-hand side of the ODE
lmbda_a_dot = 1/C*(P_a - diff(psi_2, lmbda_a) + term_3)

print_ccode((f0.T*C_e*f0)[0], assign_to = "real I4_f_e")
print_ccode(lmbda_a_dot, assign_to = "lambda_a_dot")
Esempio n. 6
0
for i in xrange(0, len(var)):
	stdout.write("\tdouble " + str(var[i]) + " = u[" + str(i) + "]" + ";\n")

stdout.write("\n")
stdout.write("	//parameters\n")

for i in xrange(0, len(varPars)):
	stdout.write("\tdouble " + str(varPars[i]) + " = par[" + str(i) + "]" + ";\n")
for i in xrange(0, len(constPars)):
	stdout.write("\tdouble " + str(constPars[i]) + " = PAR_" + str(constPars[i]) + "_PAR" + ";\n")

stdout.write("\n\t//Help parameters\n")
for i in xrange(0, len(replSet)):
	x, y = replSet[i]
	stdout.write("\t")
	print_ccode(x, assign_to = "\tdouble " + str(y))
stdout.write("\n\t//System\n")

for i in xrange(0, len(ode)):
	stdout.write("\t")
	print_ccode( ode[i].subs(replSet), assign_to = "f[" + str(i) + "]")
	#pprint(ode[i].subs(replSet))
	

stdout.write("\n")
stdout.write("	//Jacobian\n")
stdout.write("\n")
stdout.write("	if (ijac == 0)\n")
stdout.write("	{\n")
stdout.write("		return 0;\n")
stdout.write("	}\n")
        'ade_with_f': f"CudaDeviceFunction void relax_and_collide_ADE_with_F(real_t {pop_in_str}[{q}], real_t {omega_ade}, vector_t u, vector_t {F_str}) \n{{",
        'ade': f"CudaDeviceFunction void relax_and_collide_ADE(real_t {pop_in_str}[{q}], real_t {omega_ade}, vector_t u) \n{{",
    }
    result = model_switcher.get(choice, lambda: "Invalid argument")
    print(result)


make_header(model)

print("\t//=== THIS IS AUTOMATICALLY GENERATED CODE ===")
# print(f"real_t {sv} = omega;")
# print("real_t bulk_visc = 1./6. ;")
# print("real_t {sb} = 1./(3*bulk_visc + 0.5);")
# print(f"real_t {sb} = omega_bulk;\n")  # s_b = 1./(3*bulk_visc + 0.5)
print_u2(d)
print_ccode(get_m00(q, pop_in_str), assign_to=f'\treal_t {m00}')


def make_variables(choice):
    model_switcher = {
        'hydro_compressible': f"\n\treal_t {temp_pop_str}[{q}];\n",
        'hydro_incompressible': f"\n\treal_t {temp_pop_str}[{q}];\n",
        'ade_with_f': f"\n\treal_t {temp_pop_str}[{q}];\n",
        'ade': f"\n\treal_t {temp_pop_str}[{q}];\n",
    }
    # Get the function from switcher dictionary
    result = model_switcher.get(choice, lambda: "Invalid argument")
    print(result)


make_variables(model)
Esempio n. 8
0
#We first need the three equations we are going to try and set to zero, i.e. the first partial derivitives wrt e h and F.
print "/*This code was automatically generated by "+str(sys.argv[0])+"*/\n"
print "#include \"allele_stat.h\""
print "#include \"quartet.h\""
print "#include \"typedef.h\""
print 
for x in range(0, 3):
	system_eq.append(sympy.diff(lnL, params[x]) )
	print "inline float_t H"+str(x)+" (const quartet_t &q, const allele_stat &a) {"
	print "\tconst float_t M=q.base[a.major];"
	print "\tconst float_t m=q.base[a.minor];"
	print "\tconst float_t e1=q.base[a.e1];"
	print "\tconst float_t e2=q.base[a.e2];"
	sys.stdout.write("\treturn ")
	print_ccode(  system_eq[-1] )
	print ";\n}\n"

#Then we need to make the Jacobian, which is a matrix with ...
for x in range(0, 3):
	for y in range(0, 3):
		print "inline float_t J"+str(x)+str(y)+" (const quartet_t &q, const allele_stat &a) {"
		print "\tconst float_t M=q.base[a.major];"
		print "\tconst float_t m=q.base[a.minor];"
		print "\tconst float_t e1=q.base[a.e1];"
		print "\tconst float_t e2=q.base[a.e2];"
		sys.stdout.write("\treturn ")

		print_ccode(system_eq[x]-sys )
#		print_ccode(sympy.diff(system_eq[x], params[y]) )
		print ";\n}\n"
        'ade_with_f': f"CudaDeviceFunction void relax_and_collide_ADE_with_F(real_t {pop_in_str}[{q}], real_t {omega_ade}, vector_t u, vector_t {F_str}) \n{{",
        'ade': f"CudaDeviceFunction void relax_and_collide_ADE(real_t {pop_in_str}[{q}], real_t {omega_ade}, vector_t u) \n{{",
    }
    result = model_switcher.get(choice, lambda: "Invalid argument")
    print(result)


make_header(model)

print("\t//=== THIS IS AUTOMATICALLY GENERATED CODE ===")
# print(f"real_t {sv} = omega;")
# print("real_t bulk_visc = 1./6. ;")
# print("real_t {sb} = 1./(3*bulk_visc + 0.5);")
# print(f"real_t {sb} = omega_bulk;\n")  # s_b = 1./(3*bulk_visc + 0.5)
print_u2(d)
print_ccode(get_m00(q, pop_in_str), assign_to=f'\treal_t {m00}')


def make_variables(choice):
    model_switcher = {
        'hydro': f"\n\treal_t {temp_pop_str}[{q}];\n",
        'ade_with_f': f"\n\treal_t {temp_pop_str}[{q}];\n",
        'ade': f"\n\treal_t {temp_pop_str}[{q}];\n",
    }
    # Get the function from switcher dictionary
    result = model_switcher.get(choice, lambda: "Invalid argument")
    print(result)


make_variables(model)
Esempio n. 10
0
def elastic_stresses(C):

    # Modified right Cauchy-Green tensor
    C_bar = J**(-Rational(2, 3))*(C)

    # Define the second Piola-Kirchhoff stress in terms of the invariants
    S_bar =   2*(diff(psi_iso, I1_bar) + diff(psi_iso, I2_bar))*I \
            - 2*diff(psi_iso, I2_bar)*C_bar \
            + 2*diff(psi_iso, I4_f_bar)*(f0*f0.T) \
            + 2*diff(psi_iso, I4_s_bar)*(s0*s0.T) \
            + diff(psi_iso, I8_fs_bar)*(f0*s0.T + s0*f0.T) \
            + diff(psi_iso, I8_fn_bar)*(f0*n0.T + n0*f0.T)
    S_bar_contract_C = sum([sum([S_bar[a, b]*C[a, b]
                                 for a in range(3)]) for b in range(3)])
    Dev_S_bar = S_bar - Rational(1, 3)*(S_bar_contract_C)*C.inv()

    S_iso_inf = J**(-Rational(2, 3))*Matrix(Dev_S_bar)
    S_vol_inf = J*diff(psi_vol, J)*C.inv()

    # Substitute the current values of the invariants
    substitutions = {I8_fn_bar: (f0.T*C_bar*n0)[0]}
    print_ccode(C_bar.trace(), assign_to="double I1_bar")
    print_ccode(Rational(1, 2)*(I1_bar*I1_bar - (C_bar*C_bar).trace()), assign_to="double I2_bar")
    print_ccode((f0.T*C_bar*f0)[0], assign_to="double I4_f_bar")
    print_ccode((s0.T*C_bar*s0)[0], assign_to="double I4_s_bar")
    print_ccode((f0.T*C_bar*s0)[0], assign_to="double I8_fs_bar")
    print_ccode((f0.T*C_bar*n0)[0], assign_to="double I8_fn_bar")

    return S_vol_inf, S_iso_inf
Esempio n. 11
0
for parameter in material_parameters.keys():
    print("double %s\t= %g;" % (parameter.name, material_parameters[parameter]))

f11, f12, f13, f21, f22, f23, f31, f32, f33 = \
symbols("f11, f12, f13, f21, f22, f23, f31, f32, f33")

F = Matrix([[f11, f12, f13],
            [f21, f22, f23],
            [f31, f32, f33]])

C = F.T*F

for i in range(9):
    print("double %s = %g; " % (F[i], 0.0))

print_ccode(F.det(), assign_to="double J")

# Strain energy function in terms of the invariants of the right
# Cauchy-Green tensor
psi_iso =  a/(2*b)*exp(b*(I1_bar - 3)) \
         + a_f/(2*b_f)*(exp(b_f*(I4_f_bar - 1)**2) - 1) \
         + a_s/(2*b_s)*(exp(b_s*(I4_s_bar - 1)**2) - 1) \
         + a_fs/(2*b_fs)*(exp(b_fs*I8_fs_bar**2) - 1)
psi_vol = kappa*(1/(beta**2)*(beta*ln(J) + 1/(J**beta) - 1))

# Identity Matrix
I = eye(3)

# Reference fibre, sheet and sheet-normal directions
f0 = Matrix([1, 0, 0])
s0 = Matrix([0, 1, 0])
Esempio n. 12
0
symbols("cloc11, cloc12, cloc13, cloc21, cloc22, cloc23, cloc31, cloc32, cloc33")

C = Matrix([[c11, c12, c13], [c21, c22, c23], [c31, c32, c33]])

print "// Invariants for background material"
S_vol_inf, S_iso_inf = background_elasticity(C)

print
print "// Invariant for crossbridge elasticity"
S_XB = crossbridge_elasticity(C, lambda_a)

print
print "// Isochoric part of the background elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_iso_inf[i, j],
                    assign_to="S_loc_iso(%i, %i)" % (i + 1, j + 1))

print
print "// Volumetric part of the background elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_vol_inf[i, j],
                    assign_to="S_loc_vol(%i, %i)" % (i + 1, j + 1))

print
print "// Cross-bridge elasticity"
for i in range(3):
    for j in range(3):
        print_ccode(S_XB[i, j], assign_to="S_XB(%i, %i)" % (i + 1, j + 1))
Esempio n. 13
0
def generate(ex, vnames, idx):
    """
    Generate the C++ code by simplifying the expressions.
    """
    # print(ex)

    mi = [0, 1, 2, 4, 5, 8]
    midx = ['00', '01', '02', '11', '12', '22']

    # total number of expressions
    # print("--------------------------------------------------------")
    num_e = 0
    lexp = []
    lname = []
    for i, e in enumerate(ex):
        if type(e) == list:
            num_e = num_e + len(e)
            for j, ev in enumerate(e):
                lexp.append(ev)
                lname.append(vnames[i]+repr(j)+idx)
        elif type(e) == Matrix:
            num_e = num_e + len(e)
            for j, k in enumerate(mi):
                lexp.append(e[k])
                lname.append(vnames[i]+midx[j]+idx)
        else:
            num_e = num_e + 1
            lexp.append(e)
            lname.append(vnames[i]+idx)

    # print(num_e)
    # print(len(lname))

    print('// Dendro: {{{ ')
    print('// Dendro: original ops: ', count_ops(lexp))

    # print("--------------------------------------------------------")
    # print("Now trying Common Subexpression Detection and Collection")
    # print("--------------------------------------------------------")

    # Common Subexpression Detection and Collection
    # for i in range(len(ex)):
    #     # print("--------------------------------------------------------")
    #     # print(ex[i])
    #     # print("--------------------------------------------------------")
    #     ee_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(5))
    #     ee_syms = numbered_symbols(prefix=ee_name)
    #     _v = cse(ex[i],symbols=ee_syms)
    #     # print(type(_v))
    #     for (v1,v2) in _v[0]:
    #         print("double %s = %s;" % (v1, v2))
    #     print("%s = %s" % (vnames[i], _v[1][0]))

    #mex = Matrix(ex)
    ee_name = 'DENDRO_' #''.join(random.choice(string.ascii_uppercase) for _ in range(5))
    ee_syms = numbered_symbols(prefix=ee_name)
    _v = cse(lexp, symbols=ee_syms, optimizations='basic')

    custom_functions = {'grad': 'grad', 'grad2': 'grad2', 'agrad': 'agrad', 'kograd': 'kograd'}

    rops=0
    print('// Dendro: printing temp variables')
    for (v1, v2) in _v[0]:
        # print("double %s = %s;" % (v1, v2)) # replace_pow(v2)))
        print('double ', end='')
        print_ccode(v2, assign_to=v1, user_functions=custom_functions)
        rops = rops + count_ops(v2)

    print()
    print('// Dendro: printing variables')
    for i, e in enumerate(_v[1]):
        print("//--")
        # print("%s = %s;" % (lname[i], e)) # replace_pow(e)))
        #f = open(str(lname[i])+'.gv','w')
        #print(dotprint(e), file=f)
        #f.close()
        print_ccode(e, assign_to=lname[i], user_functions=custom_functions)
        rops = rops + count_ops(e)

    print('// Dendro: reduced ops: ', rops)
    print('// Dendro: }}} ')
Esempio n. 14
0
for x in range(0, 7):
	for y in range(0, 7):
		print "inline float_t J"+str(x)+str(y)+" (const Genotype_pair &pair, const Constants <float_t, const std::pair <const Genotype_pair &, const Relatedness &> > &con) {"
		sys.stdout.write("\treturn ")
                out=(sympy.diff(system_eq[x], params[y]))
		#out=sympy.simplify(out)
		pre(out)
		keys=dag_sort(keys)
		for key in keys:
			out=exact_sub(out, constants[key], key)
	        string=sympy.printing.ccode(out)
		print string, ";\n}\n"

print "inline float_t lnL_NR (const Genotype_pair &pair, const Relatedness &rel) {"
sys.stdout.write("\treturn ")
print_ccode(sympy.simplify(lnL))
print ";\n}\n"

keys=dag_replace(keys)

for key in keys:
	print "inline void set_"+str(key).split('.')[1].replace('[','').replace(']','') +" (Constants <float_t, const std::pair <const Genotype_pair &, const Relatedness &> > &con, const std::pair <const Genotype_pair &, const Relatedness &> &d ) {"
	print "\tconst Genotype_pair *pair=&d.first;"
	print "\tconst Relatedness *rel=&d.second;"
	print "\tcon.c["+str(key).split('.')[1].replace('[','').replace(']','').strip('c')+"]=",
        string=sympy.printing.ccode(constants[key])
        string=string.replace("rel.", "rel->").replace("pair.","pair->")
	print string, ";\n}\n"

print "static void (*cfn["+str(len(constants))+"])(Constants <float_t, const std::pair <const Genotype_pair &, const Relatedness &> > &, const std::pair <const Genotype_pair &, const Relatedness &> &)={",         print ", "+"&set_"+str(keys[0]).split('.')[1].replace('[','').replace(']',''),
for key in keys[1:]:
Esempio n. 15
0
# We first need the three equations we are going to try and set to zero, i.e. the first partial derivitives wrt e h and F.
print "/*This code was automatically generated by " + str(sys.argv[0]) + "*/\n"
print '#include "allele_stat.h"'
print '#include "quartet.h"'
print '#include "typedef.h"'
print
# numpy.set_printoptions(precission=18)
for x in range(0, 3):
    system_eq.append(sympy.diff(lnL, params[x]))
    print "inline float_t H" + str(x) + " (const quartet_t &q, const allele_stat &a) {"
    print "\tconst float_t M=q.base[a.major];"
    print "\tconst float_t m=q.base[a.minor];"
    print "\tconst float_t E=q.base[a.e1]+q.base[a.e2];"
    sys.stdout.write("\treturn ")
    print_ccode(system_eq[-1])
    print ";\n}\n"

# Then we need to make the Jacobian, which is a matrix with ...
for x in range(0, 3):
    for y in range(0, 3):
        print "inline float_t J" + str(x) + str(y) + " (const quartet_t &q, const allele_stat &a) {"
        print "\tconst float_t M=q.base[a.major];"
        print "\tconst float_t m=q.base[a.minor];"
        print "\tconst float_t E=q.base[a.e1]+q.base[a.e2];"
        sys.stdout.write("\treturn ")
        print_ccode(sympy.diff(system_eq[x], params[y]))
        print ";\n}\n"
print "inline float_t lnL_NR (const quartet_t &q, const allele_stat &a) {"
print "\tconst float_t M=q.base[a.major];"
print "\tconst float_t m=q.base[a.minor];"
print("CudaDeviceFunction void relax_and_collide_CM_phase_field("
      "real_t %s[9], "
      "real_t tau, "
      "vector_t u, "
      "vector_t F_phi"
      ") \n{" % pop_in_str)

print_u2()
print("real_t %s = 1./tau;" % omega_v)
# print("real_t bulk_visc = 1./6. ;")
# print("real_t %s = 1./(3*bulk_visc + 0.5);" % sb)
print("real_t %s = omega_bulk;" % omega_b)  # s_b = 1./(3*bulk_visc + 0.5)
print("")

print_ccode(get_m00(q, pop_in_str), assign_to='real_t m00')

print("\nreal_t %s[9]; real_t %s[9]; real_t %s[9];\n" %
      (temp_pop_str, cm_eq_pop_str, F_cm_str))
print("for (int i = 0; i < 9; i++) {\n\t"
      "%s[i] = %s[i];}" % (temp_pop_str, pop_in_str))

populations = get_print_symbols_in_indx_notation(q, pop_in_str)
temp_populations = get_print_symbols_in_indx_notation(q, temp_pop_str)
cm_eq = get_print_symbols_in_indx_notation(q, cm_eq_pop_str)
F_cm = get_print_symbols_in_indx_notation(q, F_cm_str)
m = Mraw_D2Q9 * temp_populations

print("\n//raw moments from density-probability functions")
print("//[m00, m10, m01, m20, m02, m11, m21, m12, m22]")
print_as_vector(m, outprint_symbol=pop_in_str)
print("CudaDeviceFunction void relax_and_collide_CM_hydro("
      f"real_t {pop_in_str}[{q}], "
      "real_t tau, "
      "vector_t u, "
      "vector_t Fhydro, "
      f"real_t {rho}) "
      "\n {")

print_u2()
print("real_t %s = 1./tau;" % omega_v)
# print("real_t bulk_visc = 1./6. ;")
# print("real_t %s = 1./(3*bulk_visc + 0.5);" % sb) 
print("real_t %s = omega_bulk;" % omega_b)  # s_b = 1./(3*bulk_visc + 0.5)
print("")

print_ccode(get_m00(q, pop_in_str), assign_to='real_t m00')

print("\nreal_t %s[9]; real_t %s[9]; real_t %s[9];\n" % (temp_pop_str, cm_eq_pop_str, F_cm_str))
print("for (int i = 0; i < 9; i++) {\n\t"
      "%s[i] = %s[i];}" % (temp_pop_str, pop_in_str))

populations = get_DF(print_symbol=pop_in_str)
temp_populations = get_DF(print_symbol=temp_pop_str)
cm_eq = get_DF(print_symbol=cm_eq_pop_str)
F_cm = get_DF(print_symbol=F_cm_str)
m = Mraw_D2Q9 * temp_populations

print("\n//raw moments from density-probability functions")
print("//[m00, m10, m01, m20, m02, m11, m21, m12, m22]")
print_as_vector(m, print_symbol=pop_in_str)
Esempio n. 18
0
#We first need the three equations we are going to try and set to zero, i.e. the first partial derivitives wrt e h and F.
print "/*This code was automatically generated by " + str(sys.argv[0]) + "*/\n"
print "#include \"allele.h\""
print "#include \"quartet.h\""
print "#include \"typedef.h\""
print
#numpy.set_printoptions(precission=18)
for x in range(0, 3):
    system_eq.append(sympy.diff(lnL, params[x]))
    print "inline float_t H" + str(
        x) + " (const quartet_t &q, const Allele &a) {"
    print "\tconst float_t M=q.base[a.major];"
    print "\tconst float_t m=q.base[a.minor];"
    print "\tconst float_t E=q.base[a.e1]+q.base[a.e2];"
    sys.stdout.write("\treturn ")
    print_ccode(system_eq[-1])
    print ";\n}\n"

#Then we need to make the Jacobian, which is a matrix with ...
for x in range(0, 3):
    for y in range(0, 3):
        print "inline float_t J" + str(x) + str(
            y) + " (const quartet_t &q, const Allele &a) {"
        print "\tconst float_t M=q.base[a.major];"
        print "\tconst float_t m=q.base[a.minor];"
        print "\tconst float_t E=q.base[a.e1]+q.base[a.e2];"
        sys.stdout.write("\treturn ")
        print_ccode(sympy.diff(system_eq[x], params[y]))
        print ";\n}\n"
print "inline float_t lnL_NR (const quartet_t &q, const Allele &a) {"
print "\tconst float_t M=q.base[a.major];"
Esempio n. 19
0
A_0_v = Rational(2)
A_1_v = Rational(0)
A_2_v = Rational(-1)
theta_final = expand(theta.subs([
    (tau, tau_v),
    (theta_0, theta_0_v),
    (theta_1, theta_1_v),
    (R_0, R_0_v),
    (R_1, R_1_v),
    (A_0, A_0_v),
    (A_1, A_1_v),
    (A_2, A_2_v)
    ]))
display(theta_final)
print(cxxcode(theta_final)) # with namespaces
print_ccode(theta_final)
print_paraview(make_cartesian(theta_final))


C_1 = Symbol("C_1")
C_2 = Symbol("C_2")
tau = Symbol("tau")
theta_armin = (-20*C_1 * log(R) + (5*R**4)/4 - 2*R**2 * (24*tau**2+5))/(75*tau) + C_2
theta_armin_01 = theta_armin.subs([
    (tau, 0.1),
    (C_1, -0.40855716127979214),
    (C_2, 2.4471587630476663)
    ])

print(cxxcode(theta_armin)) # with namespaces
print_ccode(theta_armin)
Esempio n. 20
0
P_0 = Symbol('P_0', real=True)

Density = Symbol('Density', real=True)
Viscosity = Symbol('Viscosity', real=True)
epsilon = Symbol('epsilon', real=True)

# Set the manufactured solution. The solution
# is from Salari K, and Knupp P, "Code verification
# by the method of manufactured solutions,"
# SAND 2000-1444, Sandia National Laboratories,
# Albuquerque, NM, 2000.

u = u_0 * (sin(x * x + y * y) + epsilon)
v = v_0 * (cos(x * x + y * y) + epsilon)
P = P_0 * (sin(x * x + y * y) + 2.0)

# Put the manufactured solution through the Euler
# equations to get the source term for each eqn.

print("\nSource-Rho: ")
Sp = diff(Density * u, x) + diff(Density * v, y)
print_ccode(Sp.simplify())

print("\nSource-U: ")
Su = diff(Density * u * u + P, x) + diff(Density * u * v, y)
print_ccode(Su.simplify())

print("\nSource-V: ")
Sv = diff(Density * u * v, x) + diff(Density * v * v + P, y)
print_ccode(Sv.simplify())
Esempio n. 21
0
#We first need the three equations we are going to try and set to zero, i.e. the first partial derivitives wrt e h and F.
print "/*This code was automatically generated by " + str(sys.argv[0]) + "*/\n"
print "#include \"allele_stat.h\""
print "#include \"quartet.h\""
print "#include \"typedef.h\""
print
for x in range(0, 3):
    system_eq.append(sympy.diff(lnL, params[x]))
    print "inline float_t H" + str(
        x) + " (const quartet_t &q, const allele_stat &a) {"
    print "\tconst float_t M=q.base[a.major];"
    print "\tconst float_t m=q.base[a.minor];"
    print "\tconst float_t e1=q.base[a.e1];"
    print "\tconst float_t e2=q.base[a.e2];"
    sys.stdout.write("\treturn ")
    print_ccode(system_eq[-1])
    print ";\n}\n"

#Then we need to make the Jacobian, which is a matrix with ...
for x in range(0, 3):
    for y in range(0, 3):
        print "inline float_t J" + str(x) + str(
            y) + " (const quartet_t &q, const allele_stat &a) {"
        print "\tconst float_t M=q.base[a.major];"
        print "\tconst float_t m=q.base[a.minor];"
        print "\tconst float_t e1=q.base[a.e1];"
        print "\tconst float_t e2=q.base[a.e2];"
        sys.stdout.write("\treturn ")

        print_ccode(system_eq[x] - sys)
        #		print_ccode(sympy.diff(system_eq[x], params[y]) )