Skip to content

Commit 45645c9

Browse files
change constant hyper parameter to uppercase
1 parent 8d0ca82 commit 45645c9

File tree

4 files changed

+56
-64
lines changed

4 files changed

+56
-64
lines changed

docs/zh/examples/cylinder2d_unsteady.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ examples/cylinder/2d_unsteady/cylinder2d_unsteady_Re100.py:36:37
130130

131131
``` py linenums="39"
132132
# set timestamps
133-
time_start, time_end = 1, 50
134-
num_timestamps = 50
135-
train_num_timestamps = 30
133+
TIME_START, TIME_END = 1, 50
134+
NUM_TIMESTAMPS = 50
135+
TRAIN_NUM_TIMESTAMPS = 30
136136

137137
train_timestamps = np.linspace(
138-
time_start, time_end, num_timestamps, endpoint=True
138+
TIME_START, TIME_END, NUM_TIMESTAMPS, endpoint=True
139139
).astype("float32")
140-
train_timestamps = np.random.choice(train_timestamps, train_num_timestamps)
140+
train_timestamps = np.random.choice(train_timestamps, TRAIN_NUM_TIMESTAMPS)
141141
train_timestamps.sort()
142-
t0 = np.array([time_start], dtype="float32")
142+
t0 = np.array([TIME_START], dtype="float32")
143143

144144
val_timestamps = np.linspace(
145-
time_start, time_end, num_timestamps, endpoint=True
145+
TIME_START, TIME_END, NUM_TIMESTAMPS, endpoint=True
146146
).astype("float32")
147147

148148
logger.info(f"train_timestamps: {train_timestamps.tolist()}")
@@ -152,8 +152,8 @@ logger.info(f"val_timestamps: {val_timestamps.tolist()}")
152152
geom = {
153153
"time_rect": ppsci.geometry.TimeXGeometry(
154154
ppsci.geometry.TimeDomain(
155-
time_start,
156-
time_end,
155+
TIME_START,
156+
TIME_END,
157157
timestamps=np.concatenate((t0, train_timestamps), axis=0),
158158
),
159159
ppsci.geometry.PointCloud(

docs/zh/examples/ldc2d_unsteady.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pde_constraint = ppsci.constraint.InteriorConstraint(
207207
equation["NavierStokes"].equations,
208208
{"continuity": 0, "momentum_x": 0, "momentum_y": 0},
209209
geom["time_rect"],
210-
{**train_dataloader_cfg, "batch_size": npoint_pde * ntime_pde},
210+
{**train_dataloader_cfg, "batch_size": NPOINT_PDE * NTIME_PDE},
211211
ppsci.loss.MSELoss("sum"),
212212
evenly=True,
213213
weight_dict={
@@ -323,17 +323,17 @@ examples/ldc/ldc2d_unsteady_Re10.py:145:163
323323

324324
``` py linenums="165"
325325
--8<--
326-
examples/ldc/ldc2d_unsteady_Re10.py:165:204
326+
examples/ldc/ldc2d_unsteady_Re10.py:165:196
327327
--8<--
328328
```
329329

330330
### 3.9 模型训练、评估与可视化
331331

332332
完成上述设置之后,只需要将上述实例化的对象按顺序传递给 `ppsci.solver.Solver`,然后启动训练、评估、可视化。
333333

334-
``` py linenums="206"
334+
``` py linenums="198"
335335
--8<--
336-
examples/ldc/ldc2d_unsteady_Re10.py:206:
336+
examples/ldc/ldc2d_unsteady_Re10.py:198:
337337
--8<--
338338
```
339339

examples/cylinder/2d_unsteady/cylinder2d_unsteady_Re100.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@
174174
optimizer = ppsci.optimizer.Adam(0.001)((model,))
175175

176176
# set validator
177-
npoints_eval = (NPOINT_PDE + NPOINT_INLET_CYLINDER + NPOINT_OUTLET) * NUM_TIMESTAMPS
177+
NPOINT_EVAL = (NPOINT_PDE + NPOINT_INLET_CYLINDER + NPOINT_OUTLET) * NUM_TIMESTAMPS
178178
residual_validator = ppsci.validate.GeometryValidator(
179179
equation["NavierStokes"].equations,
180180
{"continuity": 0, "momentum_x": 0, "momentum_y": 0},
181181
geom["time_rect_eval"],
182182
{
183183
"dataset": "NamedArrayDataset",
184-
"total_size": npoints_eval,
184+
"total_size": NPOINT_EVAL,
185185
"batch_size": 10240,
186186
"sampler": {"name": "BatchSampler"},
187187
},

examples/ldc/ldc2d_unsteady_Re10.py

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
# set random seed for reproducibility
2424
ppsci.utils.misc.set_random_seed(42)
2525
# 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
2727
# initialize logger
28-
logger.init_logger("ppsci", f"{output_dir}/train.log", "info")
28+
logger.init_logger("ppsci", f"{OUTPUT_DIR}/train.log", "info")
2929

3030
# set model
3131
model = ppsci.arch.MLP(
@@ -45,27 +45,27 @@
4545
}
4646

4747
# set dataloader config
48-
iters_per_epoch = 1
48+
ITERS_PER_EPOCH = 1
4949
train_dataloader_cfg = {
5050
"dataset": "IterableNamedArrayDataset",
51-
"iters_per_epoch": iters_per_epoch,
51+
"iters_per_epoch": ITERS_PER_EPOCH,
5252
}
5353

5454
# 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
6262

6363
# set constraint
6464
pde_constraint = ppsci.constraint.InteriorConstraint(
6565
equation["NavierStokes"].equations,
6666
{"continuity": 0, "momentum_x": 0, "momentum_y": 0},
6767
geom["time_rect"],
68-
{**train_dataloader_cfg, "batch_size": npoint_pde * ntime_pde},
68+
{**train_dataloader_cfg, "batch_size": NPOINT_PDE * NTIME_PDE},
6969
ppsci.loss.MSELoss("sum"),
7070
evenly=True,
7171
weight_dict={
@@ -79,7 +79,7 @@
7979
{"u": lambda out: out["u"], "v": lambda out: out["v"]},
8080
{"u": 1, "v": 0},
8181
geom["time_rect"],
82-
{**train_dataloader_cfg, "batch_size": npoint_top * ntime_top},
82+
{**train_dataloader_cfg, "batch_size": NPOINT_TOP * NTIME_TOP},
8383
ppsci.loss.MSELoss("sum"),
8484
criteria=lambda t, x, y: np.isclose(y, 0.05),
8585
name="BC_top",
@@ -88,7 +88,7 @@
8888
{"u": lambda out: out["u"], "v": lambda out: out["v"]},
8989
{"u": 0, "v": 0},
9090
geom["time_rect"],
91-
{**train_dataloader_cfg, "batch_size": npoint_down * ntime_down},
91+
{**train_dataloader_cfg, "batch_size": NPOINT_DOWN * NTIME_DOWN},
9292
ppsci.loss.MSELoss("sum"),
9393
criteria=lambda t, x, y: np.isclose(y, -0.05),
9494
name="BC_down",
@@ -97,7 +97,7 @@
9797
{"u": lambda out: out["u"], "v": lambda out: out["v"]},
9898
{"u": 0, "v": 0},
9999
geom["time_rect"],
100-
{**train_dataloader_cfg, "batch_size": npoint_left * ntime_left},
100+
{**train_dataloader_cfg, "batch_size": NPOINT_LEFT * NTIME_LEFT},
101101
ppsci.loss.MSELoss("sum"),
102102
criteria=lambda t, x, y: np.isclose(x, -0.05),
103103
name="BC_left",
@@ -106,7 +106,7 @@
106106
{"u": lambda out: out["u"], "v": lambda out: out["v"]},
107107
{"u": 0, "v": 0},
108108
geom["time_rect"],
109-
{**train_dataloader_cfg, "batch_size": npoint_right * ntime_right},
109+
{**train_dataloader_cfg, "batch_size": NPOINT_RIGHT * NTIME_RIGHT},
110110
ppsci.loss.MSELoss("sum"),
111111
criteria=lambda t, x, y: np.isclose(x, 0.05),
112112
name="BC_right",
@@ -115,7 +115,7 @@
115115
{"u": lambda out: out["u"], "v": lambda out: out["v"]},
116116
{"u": 0, "v": 0},
117117
geom["time_rect"],
118-
{**train_dataloader_cfg, "batch_size": npoint_ic * ntime_ic},
118+
{**train_dataloader_cfg, "batch_size": NPOINT_IC * NTIME_IC},
119119
ppsci.loss.MSELoss("sum"),
120120
evenly=True,
121121
name="IC",
@@ -131,26 +131,26 @@
131131
}
132132

133133
# set training hyper-parameters
134-
epochs = 20000 if not args.epochs else args.epochs
134+
EPOCHS = 20000 if not args.epochs else args.epochs
135135
lr_scheduler = ppsci.optimizer.lr_scheduler.Cosine(
136-
epochs,
137-
iters_per_epoch,
136+
EPOCHS,
137+
ITERS_PER_EPOCH,
138138
0.001,
139-
warmup_epoch=int(0.05 * epochs),
139+
warmup_epoch=int(0.05 * EPOCHS),
140140
)()
141141

142142
# set optimizer
143143
optimizer = ppsci.optimizer.Adam(lr_scheduler)((model,))
144144

145145
# set validator
146-
npoints_eval = npoint_pde * ntime_all
146+
NPOINT_EVAL = NPOINT_PDE * NTIME_ALL
147147
residual_validator = ppsci.validate.GeometryValidator(
148148
equation["NavierStokes"].equations,
149149
{"momentum_x": 0, "continuity": 0, "momentum_y": 0},
150150
geom["time_rect"],
151151
{
152152
"dataset": "NamedArrayDataset",
153-
"total_size": npoints_eval,
153+
"total_size": NPOINT_EVAL,
154154
"batch_size": 8192,
155155
"sampler": {"name": "BatchSampler"},
156156
},
@@ -163,42 +163,34 @@
163163
validator = {residual_validator.name: residual_validator}
164164

165165
# 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
168167
vis_initial_points = geom["time_rect"].sample_initial_interior(
169-
npoint_ic, evenly=True
168+
(NPOINT_IC + NPOINT_BC), evenly=True
170169
)
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
173172
)
174-
vis_boundary_points = geom["time_rect"].sample_boundary(
175-
npoint_bc * ntime_bc, evenly=True
176-
)
177-
173+
vis_points = vis_initial_points
178174
# manually collate input data for visualization,
179175
# (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:
189178
vis_points[key] = np.concatenate(
190179
(
191180
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+
],
194186
)
195187
)
196188

197189
visualizer = {
198190
"visulzie_u_v": ppsci.visualize.VisualizerVtu(
199191
vis_points,
200192
{"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,
202194
prefix="result_u_v",
203195
)
204196
}
@@ -207,11 +199,11 @@
207199
solver = ppsci.solver.Solver(
208200
model,
209201
constraint,
210-
output_dir,
202+
OUTPUT_DIR,
211203
optimizer,
212204
lr_scheduler,
213-
epochs,
214-
iters_per_epoch,
205+
EPOCHS,
206+
ITERS_PER_EPOCH,
215207
eval_during_train=True,
216208
eval_freq=200,
217209
equation=equation,
@@ -230,12 +222,12 @@
230222
solver = ppsci.solver.Solver(
231223
model,
232224
constraint,
233-
output_dir,
225+
OUTPUT_DIR,
234226
equation=equation,
235227
geom=geom,
236228
validator=validator,
237229
visualizer=visualizer,
238-
pretrained_model_path=f"{output_dir}/checkpoints/latest",
230+
pretrained_model_path=f"{OUTPUT_DIR}/checkpoints/latest",
239231
)
240232
solver.eval()
241233
# visualize prediction for pretrained model(optional)

0 commit comments

Comments
 (0)