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