def BOUT_print(the_vars, rational=False): """Print the variables in BOUT++ style""" for cur_var_key in the_vars.keys(): if rational: the_vars[cur_var_key] = nsimplify(the_vars[cur_var_key]) the_vars[cur_var_key] = exprToStr(the_vars[cur_var_key]) the_vars[cur_var_key] = re.sub(r'\b' + "x" + r'\b', "geom:xl", the_vars[cur_var_key]) the_vars[cur_var_key] = re.sub(r'\b' + "y" + r'\b', "geom:yl", the_vars[cur_var_key]) print("\n[{}]".format(cur_var_key)) print(the_vars[cur_var_key])
# # where # # zShift = int_{theta_0}^{theta} nu(theta) dtheta # # and nu(theta) is the field-line pitch # zShift = 0.0*theta #0.1*theta nu = diff(zShift, theta) shiftangle = zShift.subs( [(theta, 2*pi)] ) - zShift.subs( [(theta, 0.0)] ) y = symbols("y") z = symbols("z") replace = [ (phi, z + zShift), # z = phi - zShift (theta, y) ] # y = theta # Replace and print sources, solutions etc. print("[mesh]") print("zShift = "+exprToStr( zShift.subs(replace) )) print("nu = "+exprToStr( nu.subs(replace) )) print("shiftangle = "+exprToStr(shiftangle)) print("\n[f]") print("solution = "+exprToStr( f.subs(replace) )) print("\nsource = "+exprToStr( Sf.subs(replace) ))
nu = diff(zShift, theta) I = diff(zShift,psi) H = diff(zShift,theta) shiftangle = zShift.subs( [(theta, 2*pin)] ) - zShift.subs( [(theta, 0.0)] ) yShift = 0.2*psi#0.05*(0.5-psi)*sin(theta) #0.2*psi # eta = diff(yShift,psi) G = 1 - diff(yShift,theta) replace = [ (phi, z + zShift), (theta, y + yShift), (psi, x) ] # z = phi - zShift #replace = [ (phi, z + zShift), (psi, x) ] # z = phi - zShift # Replace and print sources, solutions etc. print("[mesh]") print("zShift = "+exprToStr( zShift.subs(replace) )) print("nu = "+exprToStr( nu.subs(replace) )) print("shiftangle = "+exprToStr(shiftangle.subs(replace))) print("I = "+exprToStr(I.subs(replace))) print("HH = "+exprToStr(H.subs(replace))) print("") print("eta = "+exprToStr(eta.subs(replace))) print("yShift = "+exprToStr(yShift.subs(replace))) print("G = "+exprToStr(G.subs(replace))) print("\n[f]") print("solution = "+exprToStr( f.subs(replace) )) print("\nsource = "+exprToStr( Sf.subs(replace) )) print("dfdpsi = "+exprToStr( dfdpsi.subs(replace) )) print("dfdtheta = "+exprToStr( dfdtheta.subs(replace) ))
# Turn solution into real x and z coordinates replace = [(y, metric.y * 2 * pi / Ly)] f = f.subs(replace) k = k.subs(replace) ############################## # Calculate time derivatives dfdt = Div_par(k * Grad_par(f)) ############################# # Calculate sources Sf = diff(f, t) - dfdt # Substitute back to get input y coordinates replace = [(metric.y, y * Ly / (2 * pi))] k = k.subs(replace) f = f.subs(replace) Sf = Sf.subs(replace) print("[mesh]") print("k = " + exprToStr(k)) print("\n[f]") print("solution = " + exprToStr(f)) print("\nsource = " + exprToStr(Sf))
import sympy from sympy import sin, cos, diff x, t = sympy.symbols('y t') v = sin(x) rho = cos(x) + 4 + .01 * x**2 * sin(t) m = rho * v sympy.diff(m, x) sympy.diff(-m, x) rho_s = diff(rho, t) + diff(m, x) p0 = sympy.symbols('p0') p = rho * p0 v_s = rho * diff(v, t) + v * diff(v, x) + diff(p, x) m_s = rho * v_s from boutdata import mms print('[rho]') print('solution = ', mms.exprToStr(rho)) print('source = ', mms.exprToStr(rho_s)) #print('[m]') #print('solution =',mms.exprToStr(m)) #print('source =',mms.exprToStr(m_s)) print('[v_n]') print('solution =', mms.exprToStr(v)) print('source =', mms.exprToStr(v_s))
# Turn solution into real x and z coordinates replace = [ (y, metric.y*2*pi/Ly) ] f = f.subs(replace) k = k.subs(replace) ############################## # Calculate time derivatives dfdt = Div_par( k * Grad_par(f) ) ############################# # Calculate sources Sf = diff(f, t) - dfdt # Substitute back to get input y coordinates replace = [ (metric.y, y*Ly/(2*pi) ) ] k = k.subs(replace) f = f.subs(replace) Sf = Sf.subs(replace) print("[mesh]") print("k = " + exprToStr(k)) print("\n[f]") print("solution = " + exprToStr(f)) print("\nsource = " + exprToStr(Sf))
from sympy import symbols, cos, sin, diff from boutdata.mms import exprToStr, y, t, pi # Define the manufactured solution f = y + cos(y) - sin(t) * cos(0.5 * y) g = y**2 + sin(y) + cos(t) * cos(0.1 * y * y) # Calculate gradients in x for boundaries dfdy = diff(f, y) dgdy = diff(g, y) # Calculate sources Sf = diff(f, t) - dgdy Sg = diff(g, t) - dfdy ####################### print("\n[f]") print("solution = " + exprToStr(f)) print("ddy = " + exprToStr(dfdy)) print("source = " + exprToStr(Sf)) print("\n[g]") print("solution = " + exprToStr(g)) print("ddy = " + exprToStr(dgdy)) print("source = " + exprToStr(Sg))
from sympy import symbols, cos, sin, diff from boutdata.mms import exprToStr, y, t, pi # Define the manufactured solution f = y + cos(y) - sin(t)*cos(0.5*y) g = y**2 + sin(y) + cos(t)*cos(0.1*y*y) # Calculate gradients in x for boundaries dfdy = diff(f, y) dgdy = diff(g, y) # Calculate sources Sf = diff(f, t) - dgdy Sg = diff(g, t) - dfdy ####################### print("\n[f]") print("solution = " + exprToStr(f)) print("ddy = " + exprToStr(dfdy)) print("source = " + exprToStr(Sf)) print("\n[g]") print("solution = " + exprToStr(g)) print("ddy = " + exprToStr(dgdy)) print("source = " + exprToStr(Sg))
############################# # Calculate sources Sn = diff(n, t) - dndt Sp = diff(p, t) - dpdt Snv = diff(nv, t) - dnvdt # Substitute back to get input y coordinates replace = [ (metric.y, y*Ly/(2*pi) ) ] n = n.subs(replace) p = p.subs(replace) nv = nv.subs(replace) Sn = Sn.subs(replace) Sp = Sp.subs(replace) Snv = Snv.subs(replace) print("[n]") print("solution = " + exprToStr(n)) print("\nsource = " + exprToStr(Sn)) print("\n[p]") print("solution = " + exprToStr(p)) print("\nsource = " + exprToStr(Sp)) print("\n[nv]") print("solution = " + exprToStr(nv)) print("\nsource = " + exprToStr(Snv))
# where # # zShift = int_{theta_0}^{theta} nu(theta) dtheta # # and nu(theta) is the field-line pitch # zShift = 0.0 * theta #0.1*theta nu = diff(zShift, theta) shiftangle = zShift.subs([(theta, 2 * pi)]) - zShift.subs([(theta, 0.0)]) y = symbols("y") z = symbols("z") replace = [ (phi, z + zShift), # z = phi - zShift (theta, y) ] # y = theta # Replace and print sources, solutions etc. print("[mesh]") print("zShift = " + exprToStr(zShift.subs(replace))) print("nu = " + exprToStr(nu.subs(replace))) print("shiftangle = " + exprToStr(shiftangle)) print("\n[f]") print("solution = " + exprToStr(f.subs(replace))) print("\nsource = " + exprToStr(Sf.subs(replace)))
############################# # Calculate sources Sn = diff(n, t) - dndt Sp = diff(p, t) - dpdt Snv = diff(nv, t) - dnvdt # Substitute back to get input y coordinates replace = [(metric.y, y * Ly / (2 * pi))] n = n.subs(replace) p = p.subs(replace) nv = nv.subs(replace) Sn = Sn.subs(replace) Sp = Sp.subs(replace) Snv = Snv.subs(replace) print("[n]") print("solution = " + exprToStr(n)) print("\nsource = " + exprToStr(Sn)) print("\n[p]") print("solution = " + exprToStr(p)) print("\nsource = " + exprToStr(Sp)) print("\n[nv]") print("solution = " + exprToStr(nv)) print("\nsource = " + exprToStr(Snv))