def test_df_to_m(self): # SETUP d = 2 q = 9 # DYNAMIC IMPORTS ex = dynamic_import("SymbolicCollisions.core.cm_symbols", f"ex_D{d}Q{q}") ey = dynamic_import("SymbolicCollisions.core.cm_symbols", f"ey_D{d}Q{q}") if d == 3: ez = dynamic_import("SymbolicCollisions.core.cm_symbols", f"ez_D{d}Q{q}") else: ez = None pop_in_str = 'x_in' # symbol defining populations temp_pop_str = 'temp' # symbol defining populations temp_populations = get_print_symbols_in_indx_notation(q, temp_pop_str) matrixGenerator = MatrixGenerator(ex, ey, ez, moments_dict[f'D{d}Q{q}']) Mraw = matrixGenerator.get_raw_moments_matrix() m = Mraw * temp_populations expected_results = "\tx_in[0] = temp[0] + temp[1] + temp[2] + temp[3] + temp[4] + temp[5] + temp[6] + temp[7] + temp[8];\n" \ "\tx_in[1] = temp[1] - temp[3] + temp[5] - temp[6] - temp[7] + temp[8];\n" \ "\tx_in[2] = temp[2] - temp[4] + temp[5] + temp[6] - temp[7] - temp[8];\n" \ "\tx_in[3] = temp[1] + temp[3] + temp[5] + temp[6] + temp[7] + temp[8];\n" \ "\tx_in[4] = temp[2] + temp[4] + temp[5] + temp[6] + temp[7] + temp[8];\n" \ "\tx_in[5] = temp[5] - temp[6] + temp[7] - temp[8];\n" \ "\tx_in[6] = temp[5] + temp[6] - temp[7] - temp[8];\n" \ "\tx_in[7] = temp[5] - temp[6] - temp[7] + temp[8];\n" \ "\tx_in[8] = temp[5] + temp[6] + temp[7] + temp[8];\n" f = io.StringIO() with redirect_stdout(f): print_as_vector(m, outprint_symbol=pop_in_str) out = f.getvalue() assert out == expected_results
def test_notation_ijk(self): q = 27 populations = get_print_symbols_in_indx_notation(q, print_symbol='b') expected_results = "\ta000 = b[0];\n" \ "\ta100 = b[1];\n" \ "\ta200 = b[2];\n" \ "\ta010 = b[3];\n" \ "\ta020 = b[4];\n" \ "\ta001 = b[5];\n" \ "\ta002 = b[6];\n" \ "\ta111 = b[7];\n" \ "\ta211 = b[8];\n" \ "\ta121 = b[9];\n" \ "\ta221 = b[10];\n" \ "\ta112 = b[11];\n" \ "\ta212 = b[12];\n" \ "\ta122 = b[13];\n" \ "\ta222 = b[14];\n" \ "\ta110 = b[15];\n" \ "\ta210 = b[16];\n" \ "\ta120 = b[17];\n" \ "\ta220 = b[18];\n" \ "\ta101 = b[19];\n" \ "\ta201 = b[20];\n" \ "\ta102 = b[21];\n" \ "\ta202 = b[22];\n" \ "\ta011 = b[23];\n" \ "\ta021 = b[24];\n" \ "\ta012 = b[25];\n" \ "\ta022 = b[26];\n" f = io.StringIO() with redirect_stdout(f): print_as_vector(populations, outprint_symbol='a', output_order_of_moments=e_D3Q27) out = f.getvalue() assert out == expected_results
def test_notation_i(self): q = 27 populations = get_print_symbols_in_indx_notation(q, print_symbol='b') expected_results = "\ta[0] = b[0];\n" \ "\ta[1] = b[1];\n" \ "\ta[2] = b[2];\n" \ "\ta[3] = b[3];\n" \ "\ta[4] = b[4];\n" \ "\ta[5] = b[5];\n" \ "\ta[6] = b[6];\n" \ "\ta[7] = b[7];\n" \ "\ta[8] = b[8];\n" \ "\ta[9] = b[9];\n" \ "\ta[10] = b[10];\n" \ "\ta[11] = b[11];\n" \ "\ta[12] = b[12];\n" \ "\ta[13] = b[13];\n" \ "\ta[14] = b[14];\n" \ "\ta[15] = b[15];\n" \ "\ta[16] = b[16];\n" \ "\ta[17] = b[17];\n" \ "\ta[18] = b[18];\n" \ "\ta[19] = b[19];\n" \ "\ta[20] = b[20];\n" \ "\ta[21] = b[21];\n" \ "\ta[22] = b[22];\n" \ "\ta[23] = b[23];\n" \ "\ta[24] = b[24];\n" \ "\ta[25] = b[25];\n" \ "\ta[26] = b[26];\n" \ f = io.StringIO() with redirect_stdout(f): print_as_vector(populations, outprint_symbol='a') out = f.getvalue() assert out == expected_results
'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) print(f"\tfor (int i = 0; i < {q}; i++) {{\n\t" f"\t{temp_pop_str}[i] = {pop_in_str}[i];}}") 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, m_eq_pop_str) F_cm = get_print_symbols_in_indx_notation(q, F_str) m = Mraw * temp_populations print("\n\t//raw moments from density-probability functions") # print("\t//[m00, m10, m01, m20, m02, m11, m21, m12, m22]") print_as_vector(m, outprint_symbol=pop_in_str) print("\n\t//collision in moments space") # print("//calculate equilibrium distributions in cm space") # print("real_t {cm_eq_pop_str}[{q}];\n") # print_as_vector(hardcoded_cm_eq, cm_eq_pop_str) # save time, verbosity # print("//calculate forces in cm space")
from sympy import Symbol from sympy.matrices import Matrix, eye, zeros, ones, diag from sympy import pretty_print from SymbolicCollisions.core.cm_symbols import Shift_ortho_Straka_d2q5 from SymbolicCollisions.core.printers import get_print_symbols_in_indx_notation """ See 'New Cascaded Thermal Lattice Boltzmann Method for simulations for advection-diffusion and convective heat transfer' by K.V. Sharma, R. Straka, F.W. Tavares, 2017 """ # Smat = get_shift_matrix(K_ortho_Straka_d2q5, ex_Straka_d2_q5, ey_Straka_d2_q5) # pretty_print(Smat) Smat = Shift_ortho_Straka_d2q5 k = get_print_symbols_in_indx_notation(q=4, print_symbol='k') Relax = diag(Symbol('w2'), Symbol('w3'), Symbol('w4'), Symbol('w5')) # cm_neq = get_print_symbols_in_indx_notation(q=4, print_symbol='cm_neq') k = Smat.inv()*Relax*cm_neq pretty_print(k)
print("\n\n=== PRETTY CODE: relax relax_MRT_relax_raw_mom_into_ortho ===\n\n") DF_in_str = 'f_in' # symbol defining DF mom_DF_str = 'm' mom_relaxed_DF_str = 'm_relaxed' print("CudaDeviceFunction void relax_MRT_relax_raw_mom_into_ortho(" "real_t %s[9], " "real_t tau, " "\n{" % DF_in_str) print("\nreal_t %s = 1./tau;" % omega_v) print("\nreal_t %s[9]; real_t %s[9]; \n" % (mom_DF_str, mom_relaxed_DF_str)) populations = get_print_symbols_in_indx_notation(print_symbol=DF_in_str) m_DF = get_print_symbols_in_indx_notation(print_symbol=mom_DF_str) m_relaxed_DF = get_print_symbols_in_indx_notation( print_symbol=mom_relaxed_DF_str) m = Mraw_D2Q9 * 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=mom_DF_str) print("\n//collision in orthogonal moments space") print_as_vector(S_relax2 * m_DF, outprint_symbol=mom_relaxed_DF_str) print("\n//back to density-probability functions") populations = Mraw_D2Q9.inv() * m_relaxed_DF print_as_vector(populations, outprint_symbol=DF_in_str)
# from sympy import pprint # pprint(Mraw) # pprint(Nraw) pop_in_str = 'h' # symbol defining populations temp_pop_str = 'temp' # symbol defining populations # GENERATE CODE print( f"CudaDeviceFunction void set_eq(real_t {pop_in_str}[{q}], real_t {Enthalpy}, real_t {rho}, vector_t u) \n{{" ) print("\t//=== THIS IS AUTOMATICALLY GENERATED CODE ===") print_sigma_cht() print_u2(d) print(f"\treal_t {temp_pop_str}[{q}];\n") populations = get_print_symbols_in_indx_notation(q, pop_in_str) temp_populations = get_print_symbols_in_indx_notation(q, temp_pop_str) print("\n\t//equilibrium in central moments space") print_as_vector(hardcoded_cm_eq, outprint_symbol=pop_in_str) print("\n\t//back to raw moments") print_as_vector(Nraw.inv() * populations, outprint_symbol=temp_pop_str) print("\n\t//back to density-probability functions") eq_populations = Mraw.inv() * temp_populations print_as_vector(eq_populations, outprint_symbol=pop_in_str) print("\n}\n")
import os import re from SymbolicCollisions.core.cm_symbols import e_D3Q27, moments_dict from SymbolicCollisions.core.printers import print_as_vector, get_print_symbols_in_indx_notation # SETUP d = 3 q = 27 populations = get_print_symbols_in_indx_notation(q, print_symbol='f') print_as_vector(populations, outprint_symbol='f') print() print_as_vector(populations, outprint_symbol='f', output_order_of_moments=e_D3Q27) print() print_as_vector(populations, outprint_symbol='f', output_order_of_moments=moments_dict[f'D{d}Q{q}']) print("Example: Parsing a file.") # home = pwd.getpwuid(os.getuid()).pw_dir # main_folder = os.path.join(home, 'DATA_FOR_PLOTS', 'HotBarman3D') my_dir = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(my_dir, "some_code2.txt") with open(path, "r") as f: stuff = f.read() print(stuff)
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(print_symbol=pop_in_str) temp_populations = get_print_symbols_in_indx_notation( print_symbol=temp_pop_str) cm_eq = get_print_symbols_in_indx_notation(print_symbol=cm_eq_pop_str) F_cm = get_print_symbols_in_indx_notation(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, outprint_symbol=pop_in_str) print("\n//central moments from raw moments") cm = NrawD2Q9 * populations print_as_vector(cm, outprint_symbol=temp_pop_str) print("\n//collision in central moments space")
from SymbolicCollisions.core.DiscreteCMTransforms import \ DiscreteCMTransforms, get_mom_vector_from_discrete_def, get_mom_vector_from_shift_mat lattice = 'D2Q9' ccmt = ContinuousCMTransforms(dzeta3D, u3D, F3D, rho) dcmt = DiscreteCMTransforms(e_D2Q9, u2D, F2D, rho) start = time.process_time() pop_in_str = 'f' md = moments_dict['D2Q9'] mmd = Matrix(moments_dict['D2Q9']) # populations_m = get_print_symbols_in_m_notation(e_D2Q9, pop_in_str) populations_m = get_print_symbols_in_m_notation(mmd, pop_in_str) populations_inx = get_print_symbols_in_indx_notation(e_D2Q9.shape[0], pop_in_str) md = moments_dict['D2Q9'] mmd = Matrix(moments_dict['D2Q9']) ed = [e_D2Q9[i, :] for i in range(e_D2Q9.shape[0])] ed2 = get_print_symbols_in_m_notation(e_D2Q9, 'cantbempty') print_as_vector(populations_m, outprint_symbol='g') print() print_as_vector(populations_m, outprint_symbol='g', output_order_of_moments=e_D2Q9) # this is wrong! print() print_as_vector(populations_m, outprint_symbol='g', output_order_of_moments=mmd)
print("\n\n=== PRETTY CODE: relax relax_MRT_orthoGS ===\n\n") DF_in_str = 'f_in' # symbol defining DF mom_DF_str = 'm' # eq 10.30 from The Lattice Boltzmann Method: Principles and Practice # T. Krüger, H. Kusumaatmaja, A. Kuzmin, O. Shardt, G. Silva, E.M. Viggen print("CudaDeviceFunction void relax_MRT_orthoGS(" "real_t %s[9], " "real_t tau, " "\n{" % DF_in_str) print("real_t %s = 1./tau;" % omega_v) print("\nreal_t %s[9]; \n" % mom_DF_str) DF = get_print_symbols_in_indx_notation(print_symbol=DF_in_str) m_DF = get_print_symbols_in_indx_notation(print_symbol=mom_DF_str) m = M_ortho_GS * DF print("\n//orthogonal moments from density-probability functions") print("//[m00, energy, energy^2, " "x momentum flux, x energy flux, " "y momentum flux, y energy flux, " "stress tensor (diagonal), stress tensor (off-diagonal)]") print_as_vector(m, outprint_symbol=mom_DF_str) print("\n//collision in orthogonal moments space") print_as_vector(S_relax_MRT_GS * m_DF, outprint_symbol=mom_DF_str) print("\n//back to density-probability functions") DF = M_ortho_GS.inv() * m_DF