|
23 | 23 | # set random seed for reproducibility
|
24 | 24 | ppsci.utils.misc.set_random_seed(42)
|
25 | 25 | # set output directory
|
26 |
| - output_dir = ( |
| 26 | + OUTPUT_DIR = ( |
27 | 27 | "./output_cylinder2d_unsteady" if not args.output_dir else args.output_dir
|
28 | 28 | )
|
29 | 29 | # initialize logger
|
30 |
| - logger.init_logger("ppsci", f"{output_dir}/train.log", "info") |
| 30 | + logger.init_logger("ppsci", f"{OUTPUT_DIR}/train.log", "info") |
31 | 31 |
|
32 | 32 | # set model
|
33 | 33 | model = ppsci.arch.MLP(
|
|
37 | 37 | equation = {"NavierStokes": ppsci.equation.NavierStokes(0.02, 1.0, 2, True)}
|
38 | 38 |
|
39 | 39 | # set timestamps
|
40 |
| - time_start, time_end = 1, 50 |
41 |
| - num_timestamps = 50 |
42 |
| - train_num_timestamps = 30 |
| 40 | + TIME_START, TIME_END = 1, 50 |
| 41 | + NUM_TIMESTAMPS = 50 |
| 42 | + TRAIN_NUM_TIMESTAMPS = 30 |
43 | 43 |
|
44 | 44 | train_timestamps = np.linspace(
|
45 |
| - time_start, time_end, num_timestamps, endpoint=True |
| 45 | + TIME_START, TIME_END, NUM_TIMESTAMPS, endpoint=True |
46 | 46 | ).astype("float32")
|
47 |
| - train_timestamps = np.random.choice(train_timestamps, train_num_timestamps) |
| 47 | + train_timestamps = np.random.choice(train_timestamps, TRAIN_NUM_TIMESTAMPS) |
48 | 48 | train_timestamps.sort()
|
49 |
| - t0 = np.array([time_start], dtype="float32") |
| 49 | + t0 = np.array([TIME_START], dtype="float32") |
50 | 50 |
|
51 | 51 | val_timestamps = np.linspace(
|
52 |
| - time_start, time_end, num_timestamps, endpoint=True |
| 52 | + TIME_START, TIME_END, NUM_TIMESTAMPS, endpoint=True |
53 | 53 | ).astype("float32")
|
54 | 54 |
|
55 | 55 | logger.info(f"train_timestamps: {train_timestamps.tolist()}")
|
|
59 | 59 | geom = {
|
60 | 60 | "time_rect": ppsci.geometry.TimeXGeometry(
|
61 | 61 | ppsci.geometry.TimeDomain(
|
62 |
| - time_start, |
63 |
| - time_end, |
| 62 | + TIME_START, |
| 63 | + TIME_END, |
64 | 64 | timestamps=np.concatenate((t0, train_timestamps), axis=0),
|
65 | 65 | ),
|
66 | 66 | ppsci.geometry.PointCloud(
|
|
77 | 77 | }
|
78 | 78 |
|
79 | 79 | # set dataloader config
|
80 |
| - iters_per_epoch = 1 |
| 80 | + ITERS_PER_EPOCH = 1 |
81 | 81 |
|
82 | 82 | # pde/bc/sup constraint use t1~tn, initial constraint use t0
|
83 |
| - npoint_pde, ntime_pde = 9420, len(train_timestamps) |
84 |
| - npoint_inlet_cylinder = 161 |
85 |
| - npoint_outlet = 81 |
86 |
| - alias_dict = {"x": "Points:0", "y": "Points:1", "u": "U:0", "v": "U:1"} |
| 83 | + NPOINT_PDE, ntime_pde = 9420, len(train_timestamps) |
| 84 | + NPOINT_INLET_CYLINDER = 161 |
| 85 | + NPOINT_OUTLET = 81 |
| 86 | + ALIAS_DICT = {"x": "Points:0", "y": "Points:1", "u": "U:0", "v": "U:1"} |
87 | 87 |
|
88 | 88 | # set constraint
|
89 | 89 | pde_constraint = ppsci.constraint.InteriorConstraint(
|
|
92 | 92 | geom["time_rect"],
|
93 | 93 | {
|
94 | 94 | "dataset": "IterableNamedArrayDataset",
|
95 |
| - "batch_size": npoint_pde * ntime_pde, |
96 |
| - "iters_per_epoch": iters_per_epoch, |
| 95 | + "batch_size": NPOINT_PDE * ntime_pde, |
| 96 | + "iters_per_epoch": ITERS_PER_EPOCH, |
97 | 97 | },
|
98 | 98 | ppsci.loss.MSELoss("mean"),
|
99 | 99 | name="EQ",
|
|
105 | 105 | "file_path": "./datasets/domain_inlet_cylinder.csv",
|
106 | 106 | "input_keys": ["x", "y"],
|
107 | 107 | "label_keys": ["u", "v"],
|
108 |
| - "alias_dict": alias_dict, |
| 108 | + "alias_dict": ALIAS_DICT, |
109 | 109 | "weight_dict": {"u": 10, "v": 10},
|
110 | 110 | "timestamps": train_timestamps,
|
111 | 111 | },
|
|
120 | 120 | "file_path": "./datasets/domain_outlet.csv",
|
121 | 121 | "input_keys": ["x", "y"],
|
122 | 122 | "label_keys": ["p"],
|
123 |
| - "alias_dict": alias_dict, |
| 123 | + "alias_dict": ALIAS_DICT, |
124 | 124 | "timestamps": train_timestamps,
|
125 | 125 | },
|
126 | 126 | },
|
|
134 | 134 | "file_path": "./datasets/initial/ic0.1.csv",
|
135 | 135 | "input_keys": ["x", "y"],
|
136 | 136 | "label_keys": ["u", "v", "p"],
|
137 |
| - "alias_dict": alias_dict, |
| 137 | + "alias_dict": ALIAS_DICT, |
138 | 138 | "weight_dict": {"u": 10, "v": 10, "p": 10},
|
139 | 139 | "timestamps": t0,
|
140 | 140 | },
|
|
149 | 149 | "file_path": "./datasets/probe/probe1_50.csv",
|
150 | 150 | "input_keys": ["t", "x", "y"],
|
151 | 151 | "label_keys": ["u", "v"],
|
152 |
| - "alias_dict": alias_dict, |
| 152 | + "alias_dict": ALIAS_DICT, |
153 | 153 | "weight_dict": {"u": 10, "v": 10},
|
154 | 154 | "timestamps": train_timestamps,
|
155 | 155 | },
|
|
167 | 167 | }
|
168 | 168 |
|
169 | 169 | # set training hyper-parameters
|
170 |
| - epochs = 40000 if not args.epochs else args.epochs |
171 |
| - eval_freq = 400 |
| 170 | + EPOCHS = 40000 if not args.epochs else args.epochs |
| 171 | + EVAL_FREQ = 400 |
172 | 172 |
|
173 | 173 | # set optimizer
|
174 | 174 | optimizer = ppsci.optimizer.Adam(0.001)((model,))
|
175 | 175 |
|
176 | 176 | # set validator
|
177 |
| - npoints_eval = (npoint_pde + npoint_inlet_cylinder + npoint_outlet) * num_timestamps |
| 177 | + npoints_eval = (NPOINT_PDE + NPOINT_INLET_CYLINDER + NPOINT_OUTLET) * NUM_TIMESTAMPS |
178 | 178 | residual_validator = ppsci.validate.GeometryValidator(
|
179 | 179 | equation["NavierStokes"].equations,
|
180 | 180 | {"continuity": 0, "momentum_x": 0, "momentum_y": 0},
|
|
193 | 193 |
|
194 | 194 | # set visualizer(optional)
|
195 | 195 | vis_points = geom["time_rect_eval"].sample_interior(
|
196 |
| - (npoint_pde + npoint_inlet_cylinder + npoint_outlet) * num_timestamps |
| 196 | + (NPOINT_PDE + NPOINT_INLET_CYLINDER + NPOINT_OUTLET) * NUM_TIMESTAMPS |
197 | 197 | )
|
198 | 198 | visualizer = {
|
199 | 199 | "visulzie_u": ppsci.visualize.VisualizerVtu(
|
200 | 200 | vis_points,
|
201 | 201 | {"u": lambda d: d["u"], "v": lambda d: d["v"], "p": lambda d: d["p"]},
|
202 |
| - num_timestamps=num_timestamps, |
| 202 | + num_timestamps=NUM_TIMESTAMPS, |
203 | 203 | prefix="result_u",
|
204 | 204 | )
|
205 | 205 | }
|
|
208 | 208 | solver = ppsci.solver.Solver(
|
209 | 209 | model,
|
210 | 210 | constraint,
|
211 |
| - output_dir, |
| 211 | + OUTPUT_DIR, |
212 | 212 | optimizer,
|
213 | 213 | None,
|
214 |
| - epochs, |
215 |
| - iters_per_epoch, |
| 214 | + EPOCHS, |
| 215 | + ITERS_PER_EPOCH, |
216 | 216 | eval_during_train=True,
|
217 |
| - eval_freq=eval_freq, |
| 217 | + eval_freq=EVAL_FREQ, |
218 | 218 | equation=equation,
|
219 | 219 | geom=geom,
|
220 | 220 | validator=validator,
|
|
228 | 228 | solver.visualize()
|
229 | 229 |
|
230 | 230 | # directly evaluate model from pretrained_model_path(optional)
|
231 |
| - logger.init_logger("ppsci", f"{output_dir}/eval.log", "info") |
| 231 | + logger.init_logger("ppsci", f"{OUTPUT_DIR}/eval.log", "info") |
232 | 232 | solver = ppsci.solver.Solver(
|
233 | 233 | model,
|
234 | 234 | constraint,
|
235 |
| - output_dir, |
| 235 | + OUTPUT_DIR, |
236 | 236 | equation=equation,
|
237 | 237 | geom=geom,
|
238 | 238 | validator=validator,
|
239 | 239 | visualizer=visualizer,
|
240 |
| - pretrained_model_path=f"{output_dir}/checkpoints/latest", |
| 240 | + pretrained_model_path=f"{OUTPUT_DIR}/checkpoints/latest", |
241 | 241 | )
|
242 | 242 | solver.eval()
|
243 | 243 | # visualize prediction from pretrained_model_path(optional)
|
|
0 commit comments