|
23 | 23 | # set random seed for reproducibility
|
24 | 24 | ppsci.utils.misc.set_random_seed(42)
|
25 | 25 | # set output directory
|
26 |
| - output_dir = "./ldc2d_unsteady_Re10" if not args.output_dir else args.output_dir |
| 26 | + OUTPUT_DIR = "./ldc2d_unsteady_Re10" if not args.output_dir else args.output_dir |
27 | 27 | # initialize logger
|
28 |
| - logger.init_logger("ppsci", f"{output_dir}/train.log", "info") |
| 28 | + logger.init_logger("ppsci", f"{OUTPUT_DIR}/train.log", "info") |
29 | 29 |
|
30 | 30 | # set model
|
31 | 31 | model = ppsci.arch.MLP(
|
|
45 | 45 | }
|
46 | 46 |
|
47 | 47 | # set dataloader config
|
48 |
| - iters_per_epoch = 1 |
| 48 | + ITERS_PER_EPOCH = 1 |
49 | 49 | train_dataloader_cfg = {
|
50 | 50 | "dataset": "IterableNamedArrayDataset",
|
51 |
| - "iters_per_epoch": iters_per_epoch, |
| 51 | + "iters_per_epoch": ITERS_PER_EPOCH, |
52 | 52 | }
|
53 | 53 |
|
54 | 54 | # pde/bc constraint use t1~tn, initial constraint use t0
|
55 |
| - ntime_all = len(timestamps) |
56 |
| - npoint_pde, ntime_pde = 99**2, ntime_all - 1 |
57 |
| - npoint_top, ntime_top = 101, ntime_all - 1 |
58 |
| - npoint_down, ntime_down = 101, ntime_all - 1 |
59 |
| - npoint_left, ntime_left = 99, ntime_all - 1 |
60 |
| - npoint_right, ntime_right = 99, ntime_all - 1 |
61 |
| - npoint_ic, ntime_ic = 99**2, 1 |
| 55 | + NTIME_ALL = len(timestamps) |
| 56 | + NPOINT_PDE, NTIME_PDE = 99**2, NTIME_ALL - 1 |
| 57 | + NPOINT_TOP, NTIME_TOP = 101, NTIME_ALL - 1 |
| 58 | + NPOINT_DOWN, NTIME_DOWN = 101, NTIME_ALL - 1 |
| 59 | + NPOINT_LEFT, NTIME_LEFT = 99, NTIME_ALL - 1 |
| 60 | + NPOINT_RIGHT, NTIME_RIGHT = 99, NTIME_ALL - 1 |
| 61 | + NPOINT_IC, NTIME_IC = 99**2, 1 |
62 | 62 |
|
63 | 63 | # set constraint
|
64 | 64 | pde_constraint = ppsci.constraint.InteriorConstraint(
|
65 | 65 | equation["NavierStokes"].equations,
|
66 | 66 | {"continuity": 0, "momentum_x": 0, "momentum_y": 0},
|
67 | 67 | geom["time_rect"],
|
68 |
| - {**train_dataloader_cfg, "batch_size": npoint_pde * ntime_pde}, |
| 68 | + {**train_dataloader_cfg, "batch_size": NPOINT_PDE * NTIME_PDE}, |
69 | 69 | ppsci.loss.MSELoss("sum"),
|
70 | 70 | evenly=True,
|
71 | 71 | weight_dict={
|
|
79 | 79 | {"u": lambda out: out["u"], "v": lambda out: out["v"]},
|
80 | 80 | {"u": 1, "v": 0},
|
81 | 81 | geom["time_rect"],
|
82 |
| - {**train_dataloader_cfg, "batch_size": npoint_top * ntime_top}, |
| 82 | + {**train_dataloader_cfg, "batch_size": NPOINT_TOP * NTIME_TOP}, |
83 | 83 | ppsci.loss.MSELoss("sum"),
|
84 | 84 | criteria=lambda t, x, y: np.isclose(y, 0.05),
|
85 | 85 | name="BC_top",
|
|
88 | 88 | {"u": lambda out: out["u"], "v": lambda out: out["v"]},
|
89 | 89 | {"u": 0, "v": 0},
|
90 | 90 | geom["time_rect"],
|
91 |
| - {**train_dataloader_cfg, "batch_size": npoint_down * ntime_down}, |
| 91 | + {**train_dataloader_cfg, "batch_size": NPOINT_DOWN * NTIME_DOWN}, |
92 | 92 | ppsci.loss.MSELoss("sum"),
|
93 | 93 | criteria=lambda t, x, y: np.isclose(y, -0.05),
|
94 | 94 | name="BC_down",
|
|
97 | 97 | {"u": lambda out: out["u"], "v": lambda out: out["v"]},
|
98 | 98 | {"u": 0, "v": 0},
|
99 | 99 | geom["time_rect"],
|
100 |
| - {**train_dataloader_cfg, "batch_size": npoint_left * ntime_left}, |
| 100 | + {**train_dataloader_cfg, "batch_size": NPOINT_LEFT * NTIME_LEFT}, |
101 | 101 | ppsci.loss.MSELoss("sum"),
|
102 | 102 | criteria=lambda t, x, y: np.isclose(x, -0.05),
|
103 | 103 | name="BC_left",
|
|
106 | 106 | {"u": lambda out: out["u"], "v": lambda out: out["v"]},
|
107 | 107 | {"u": 0, "v": 0},
|
108 | 108 | geom["time_rect"],
|
109 |
| - {**train_dataloader_cfg, "batch_size": npoint_right * ntime_right}, |
| 109 | + {**train_dataloader_cfg, "batch_size": NPOINT_RIGHT * NTIME_RIGHT}, |
110 | 110 | ppsci.loss.MSELoss("sum"),
|
111 | 111 | criteria=lambda t, x, y: np.isclose(x, 0.05),
|
112 | 112 | name="BC_right",
|
|
115 | 115 | {"u": lambda out: out["u"], "v": lambda out: out["v"]},
|
116 | 116 | {"u": 0, "v": 0},
|
117 | 117 | geom["time_rect"],
|
118 |
| - {**train_dataloader_cfg, "batch_size": npoint_ic * ntime_ic}, |
| 118 | + {**train_dataloader_cfg, "batch_size": NPOINT_IC * NTIME_IC}, |
119 | 119 | ppsci.loss.MSELoss("sum"),
|
120 | 120 | evenly=True,
|
121 | 121 | name="IC",
|
|
131 | 131 | }
|
132 | 132 |
|
133 | 133 | # set training hyper-parameters
|
134 |
| - epochs = 20000 if not args.epochs else args.epochs |
| 134 | + EPOCHS = 20000 if not args.epochs else args.epochs |
135 | 135 | lr_scheduler = ppsci.optimizer.lr_scheduler.Cosine(
|
136 |
| - epochs, |
137 |
| - iters_per_epoch, |
| 136 | + EPOCHS, |
| 137 | + ITERS_PER_EPOCH, |
138 | 138 | 0.001,
|
139 |
| - warmup_epoch=int(0.05 * epochs), |
| 139 | + warmup_epoch=int(0.05 * EPOCHS), |
140 | 140 | )()
|
141 | 141 |
|
142 | 142 | # set optimizer
|
143 | 143 | optimizer = ppsci.optimizer.Adam(lr_scheduler)((model,))
|
144 | 144 |
|
145 | 145 | # set validator
|
146 |
| - npoints_eval = npoint_pde * ntime_all |
| 146 | + NPOINT_EVAL = NPOINT_PDE * NTIME_ALL |
147 | 147 | residual_validator = ppsci.validate.GeometryValidator(
|
148 | 148 | equation["NavierStokes"].equations,
|
149 | 149 | {"momentum_x": 0, "continuity": 0, "momentum_y": 0},
|
150 | 150 | geom["time_rect"],
|
151 | 151 | {
|
152 | 152 | "dataset": "NamedArrayDataset",
|
153 |
| - "total_size": npoints_eval, |
| 153 | + "total_size": NPOINT_EVAL, |
154 | 154 | "batch_size": 8192,
|
155 | 155 | "sampler": {"name": "BatchSampler"},
|
156 | 156 | },
|
|
163 | 163 | validator = {residual_validator.name: residual_validator}
|
164 | 164 |
|
165 | 165 | # set visualizer(optional)
|
166 |
| - npoint_bc = npoint_top + npoint_down + npoint_left + npoint_right |
167 |
| - ntime_bc = ntime_top |
| 166 | + NPOINT_BC = NPOINT_TOP + NPOINT_DOWN + NPOINT_LEFT + NPOINT_RIGHT |
168 | 167 | vis_initial_points = geom["time_rect"].sample_initial_interior(
|
169 |
| - npoint_ic, evenly=True |
| 168 | + (NPOINT_IC + NPOINT_BC), evenly=True |
170 | 169 | )
|
171 |
| - vis_interior_points = geom["time_rect"].sample_interior( |
172 |
| - npoint_pde * ntime_pde, evenly=True |
| 170 | + vis_pde_points = geom["time_rect"].sample_interior( |
| 171 | + (NPOINT_PDE + NPOINT_BC) * NTIME_PDE, evenly=True |
173 | 172 | )
|
174 |
| - vis_boundary_points = geom["time_rect"].sample_boundary( |
175 |
| - npoint_bc * ntime_bc, evenly=True |
176 |
| - ) |
177 |
| - |
| 173 | + vis_points = vis_initial_points |
178 | 174 | # manually collate input data for visualization,
|
179 | 175 | # (interior+boundary) x all timestamps
|
180 |
| - vis_initial_points = { |
181 |
| - key: np.concatenate( |
182 |
| - (vis_initial_points[key], vis_boundary_points[key][:npoint_bc]) |
183 |
| - ) |
184 |
| - for key in vis_initial_points |
185 |
| - } |
186 |
| - vis_points = vis_initial_points |
187 |
| - for t in range(ntime_pde): |
188 |
| - for key in vis_interior_points: |
| 176 | + for t in range(NTIME_PDE): |
| 177 | + for key in vis_points: |
189 | 178 | vis_points[key] = np.concatenate(
|
190 | 179 | (
|
191 | 180 | vis_points[key],
|
192 |
| - vis_interior_points[key][t * npoint_pde : (t + 1) * npoint_pde], |
193 |
| - vis_boundary_points[key][t * npoint_bc : (t + 1) * npoint_bc], |
| 181 | + vis_pde_points[key][ |
| 182 | + t |
| 183 | + * (NPOINT_PDE + NPOINT_BC) : (t + 1) |
| 184 | + * (NPOINT_PDE + NPOINT_BC) |
| 185 | + ], |
194 | 186 | )
|
195 | 187 | )
|
196 | 188 |
|
197 | 189 | visualizer = {
|
198 | 190 | "visulzie_u_v": ppsci.visualize.VisualizerVtu(
|
199 | 191 | vis_points,
|
200 | 192 | {"u": lambda d: d["u"], "v": lambda d: d["v"], "p": lambda d: d["p"]},
|
201 |
| - num_timestamps=ntime_all, |
| 193 | + num_timestamps=NTIME_ALL, |
202 | 194 | prefix="result_u_v",
|
203 | 195 | )
|
204 | 196 | }
|
|
207 | 199 | solver = ppsci.solver.Solver(
|
208 | 200 | model,
|
209 | 201 | constraint,
|
210 |
| - output_dir, |
| 202 | + OUTPUT_DIR, |
211 | 203 | optimizer,
|
212 | 204 | lr_scheduler,
|
213 |
| - epochs, |
214 |
| - iters_per_epoch, |
| 205 | + EPOCHS, |
| 206 | + ITERS_PER_EPOCH, |
215 | 207 | eval_during_train=True,
|
216 | 208 | eval_freq=200,
|
217 | 209 | equation=equation,
|
|
230 | 222 | solver = ppsci.solver.Solver(
|
231 | 223 | model,
|
232 | 224 | constraint,
|
233 |
| - output_dir, |
| 225 | + OUTPUT_DIR, |
234 | 226 | equation=equation,
|
235 | 227 | geom=geom,
|
236 | 228 | validator=validator,
|
237 | 229 | visualizer=visualizer,
|
238 |
| - pretrained_model_path=f"{output_dir}/checkpoints/latest", |
| 230 | + pretrained_model_path=f"{OUTPUT_DIR}/checkpoints/latest", |
239 | 231 | )
|
240 | 232 | solver.eval()
|
241 | 233 | # visualize prediction for pretrained model(optional)
|
|
0 commit comments