DNDSR 0.2.1
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
test_solver.py
Go to the documentation of this file.
1from DNDSR import DNDS, Geom, CFV, EulerP
2import json
3from DNDSR.Geom.utils import *
4import numpy as np
5
6from DNDSR.EulerP.EulerP_Solver import Solver
7import cProfile
8import time
9
10
12 mpi: DNDS.MPIInfo,
13 ifCuda=False,
14):
15 if ifCuda:
16 # import cupy as cp
17
18 # cp.cuda.Device(1).use()
19 pass
20
21 # m_name, dim = "Uniform128_Periodic.cgns", 2
22 m_name, dim = "UP3D_128.cgns", 3
23 meshFile = os.path.join(
24 os.path.dirname(__file__),
25 "..",
26 "..",
27 "data",
28 "mesh",
29 m_name,
30 )
31 outDir = "../data/out/test5_test"
32
33 solver = Solver(mpi)
34 solver.ReadMesh(
35 meshFile,
36 dim=dim,
37 other_options={
38 "meshDirectBisect": 1,
39 "second_level_parts": int(128),
40 },
41 )
42
43 solver.BuildFV(
44 fv_settings={"intOrder": 1, "maxOrder": 1},
45 )
46 solver.BuildEval()
47 solver.WrapEval()
48 solver.BuildDataArray()
49 solver.WrapData()
50 solver.eval.setConfig(
51 {
52 "threadsPerBlock": 128,
53 "serializeCUDAExecution": True,
54 }
55 )
56 data = solver.data
57 if ifCuda:
58 solver.to_device("CUDA")
59
60 u = solver.data["u"]
61 mesh = solver.mesh
62 numCellGlobal = mesh.NumCellGlobal()
63 fv = solver.fv
64 u.setConstant(np.array([1, 0, 0, 0, 2.5]).reshape(-1, 1))
65
66 zCent = 0.5 if dim == 3 else 0.0
67
68 for iCell in range(mesh.NumCell()):
69 x = fv.GetCellBary(iCell)
70 if np.linalg.norm(np.array([0.5, 0.5, zCent]) - x, np.inf) < 0.25:
71 # if abs(x[0] - 0.5) < 0.25:
72 v = 0.1
73 uBox = [0.5, 1, 0, 0, 4]
74 # uBox = [1, 1 * v, 0, 0, 2.5 + 0.5 * 1 * v**2]
75 u[iCell] = np.array(uBox, dtype=np.float64).reshape(-1, 1)
76 # uu = 1
77 # vv = 1
78 # r = 1 + 0.1 * np.cos(x[0] * 2 * np.pi) * np.cos(x[1] * 2 * np.pi)
79 # u[iCell] = np.array(
80 # [r, r * uu, r * vv, 0, 2.5 + 0.5 * r * (uu**2 + vv**2)], dtype=np.float64
81 # ).reshape(-1, 1)
82
83 device_run = DNDS.DeviceBackend.Unknown
84 if ifCuda:
85 solver.data_to_device("CUDA")
86 device_run = DNDS.DeviceBackend.CUDA
87 for n, a in data.items():
88 a.trans.initPersistentPull(device_run)
89
90 solver.runningDevice = device_run
91 u.trans.startPersistentPull(device_run)
92 u.trans.waitPersistentPull(device_run)
93
94 # print(f"son size {u.son.Size()}")
95 # for i in range(100000):
96 # tW0 = time.perf_counter()
97 # u.trans.startPersistentPull(device_run)
98 # u.trans.waitPersistentPull(device_run)
99 # tW1 = time.perf_counter()
100 # if mpi.rank == 0:
101 # print(f"time: {tW1 - tW0:.3e}")
102
103 # 0.5, 1, 0, 0, 4
104
105 tInt = 0.01 * 5
106 nInt = int(150 / 5)
107 CFL = 0.5 * 1
108 t = 0
109 pr = cProfile.Profile()
110 pr.enable()
111
112 iStep = 0
113
114 for II in range(1, nInt + 1):
115 tW0 = time.perf_counter()
116 iStepNew, tNew = solver.IntegrateDt_ExplicitInterval(
117 t, t + tInt, CFL=CFL, step0=iStep, max_step=iStep + 100000
118 )
119 tW1 = time.perf_counter()
120 if mpi.rank == 0:
121 print("=" * 32)
122 print()
123 tStep = (tW1 - tW0) / (iStepNew - iStep)
124 print(f"Average time per step: [{tStep:.4e}]")
125 print(f"cell step / second: [{numCellGlobal / tStep:.4e}]")
126 print()
127 print("=" * 32)
128 t = tNew
129 iStep = iStepNew
130
131 data["uPrim"].to_host()
132 data["p"].to_host()
133 solver.eval.PrintDataVTKHDF(
134 os.path.join(outDir, f"testC_{II}"),
135 os.path.join(outDir, "testC"),
136 arrCellCentScalar=[data["p"]],
137 arrCellCentScalar_names=["p"],
138 uPrimCell=data["uPrim"],
139 t=t,
140 )
141 if ifCuda:
142 data["uPrim"].to_device("CUDA")
143 data["p"].to_device("CUDA")
144 pr.disable()
145 pr.dump_stats(f"profile_rank_{mpi.rank}.prof")
146
147
148if __name__ == "__main__":
150 mpi.setWorld()
151 ifCuda = True
152 if ifCuda:
153 import cupy as cp
154 from cupy.cuda import runtime as curuntime
155
156 device_count = cp.cuda.runtime.getDeviceCount()
157 print(f"{mpi.rank}, nGPU={device_count}")
158 print(cp.cuda.Device(0))
159 if mpi.size > device_count:
160 raise RuntimeError("too many ranks on this machine")
161 cp.cuda.Device(mpi.rank).use()
162 # curuntime.deviceSynchronize()
163 pass
164
165 run_test_solver(mpi, ifCuda=ifCuda)
run_test_solver(DNDS.MPIInfo mpi, ifCuda=False)
Lightweight bundle of an MPI communicator and the calling rank's coordinates.
Definition MPI.hpp:231