Skip to content

Commit a7ebb63

Browse files
add shock_wave export&infer
1 parent baba485 commit a7ebb63

File tree

4 files changed

+180
-1
lines changed

4 files changed

+180
-1
lines changed

docs/zh/examples/shock_wave.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@
2828
python shock_wave.py -cn=shock_wave_Ma0.728 mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/shockwave/shock_wave_Ma0728_pretrained.pdparams
2929
```
3030

31+
=== "模型导出命令"
32+
33+
=== "Ma=2.0"
34+
35+
``` sh
36+
python shock_wave.py mode=export
37+
```
38+
=== "Ma=0.728"
39+
40+
``` sh
41+
python shock_wave.py -cn=shock_wave_Ma0.728 mode=export
42+
```
43+
44+
=== "模型推理命令"
45+
46+
=== "Ma=2.0"
47+
48+
``` sh
49+
python shock_wave.py mode=infer
50+
```
51+
=== "Ma=0.728"
52+
53+
``` sh
54+
python shock_wave.py -cn=shock_wave_Ma0.728 mode=infer
55+
```
56+
3157
## 1. 背景简介
3258

3359
激波是自然界以及工程应用中经常发现的现象。它们不仅广泛地存在于航空航天领域的可压缩流动中,而且也表现在理论与应用物理以及工程应用等其它领域。在超声速与高超声速流动中,激波的出现对流体流动的整体特征会产生重要影响。激波捕捉问题已在CFD领域发展了数十年,以弱解的数学理论为基础的激波捕捉方法以其简单易实现的特点发展迅速,并在复杂超声速、高超声速流动数值模拟中得到了广泛应用。

examples/shock_wave/conf/shock_wave_Ma0.728.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ hydra:
1111
- TRAIN.checkpoint_path
1212
- TRAIN.pretrained_model_path
1313
- EVAL.pretrained_model_path
14+
- INFER.pretrained_model_path
15+
- INFER.export_path
1416
- mode
1517
- output_dir
1618
- log_freq
@@ -70,3 +72,20 @@ TRAIN:
7072
EVAL:
7173
pretrained_model_path: null
7274
eval_with_no_grad: true
75+
76+
INFER:
77+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/shockwave/shock_wave_Ma0728_pretrained.pdparams
78+
export_path: ./inference/shock_wave_Ma0.728
79+
pdmodel_path: ${INFER.export_path}.pdmodel
80+
pdiparams_path: ${INFER.export_path}.pdiparams
81+
device: gpu
82+
engine: native
83+
precision: fp32
84+
onnx_path: ${INFER.export_path}.onnx
85+
ir_optim: true
86+
min_subgraph_size: 10
87+
gpu_mem: 4000
88+
gpu_id: 0
89+
max_batch_size: 256
90+
num_cpu_threads: 4
91+
batch_size: 256

examples/shock_wave/conf/shock_wave_Ma2.0.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ hydra:
1111
- TRAIN.checkpoint_path
1212
- TRAIN.pretrained_model_path
1313
- EVAL.pretrained_model_path
14+
- INFER.pretrained_model_path
15+
- INFER.export_path
1416
- mode
1517
- output_dir
1618
- log_freq
@@ -70,3 +72,20 @@ TRAIN:
7072
EVAL:
7173
pretrained_model_path: null
7274
eval_with_no_grad: true
75+
76+
INFER:
77+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/shockwave/shock_wave_Ma2_pretrained.pdparams
78+
export_path: ./inference/shock_wave_Ma2.0
79+
pdmodel_path: ${INFER.export_path}.pdmodel
80+
pdiparams_path: ${INFER.export_path}.pdiparams
81+
device: gpu
82+
engine: native
83+
precision: fp32
84+
onnx_path: ${INFER.export_path}.onnx
85+
ir_optim: true
86+
min_subgraph_size: 10
87+
gpu_mem: 4000
88+
gpu_id: 0
89+
max_batch_size: 256
90+
num_cpu_threads: 4
91+
batch_size: 256

examples/shock_wave/shock_wave.py

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,115 @@ def evaluate(cfg: DictConfig):
518518
plt.savefig(osp.join(cfg.output_dir, f"shock_wave(Ma_{cfg.MA:.3f}).png"))
519519

520520

521+
def export(cfg: DictConfig):
522+
from paddle.static import InputSpec
523+
524+
# set models
525+
model = ppsci.arch.MLP(**cfg.MODEL)
526+
solver = ppsci.solver.Solver(
527+
model,
528+
pretrained_model_path=cfg.INFER.pretrained_model_path,
529+
)
530+
531+
# export models
532+
input_spec = [
533+
{key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys},
534+
]
535+
solver.export(input_spec, cfg.INFER.export_path)
536+
537+
538+
def inference(cfg: DictConfig):
539+
from deploy.python_infer import pinn_predictor
540+
541+
# set model predictor
542+
predictor = pinn_predictor.PINNPredictor(cfg)
543+
544+
# visualize prediction
545+
t = np.linspace(cfg.T, cfg.T, 1)
546+
x = np.linspace(0.0, cfg.Lx, cfg.Nd)
547+
y = np.linspace(0.0, cfg.Ly, cfg.Nd)
548+
_, x_grid, y_grid = np.meshgrid(t, x, y)
549+
550+
x_test = misc.cartesian_product(t, x, y)
551+
x_test_dict = misc.convert_to_dict(
552+
x_test,
553+
cfg.MODEL.input_keys,
554+
)
555+
x_test_dict = {
556+
x_key: x_test_dict[x_key].astype(np.float32) for x_key in x_test_dict
557+
}
558+
output_dict = predictor.predict(
559+
x_test_dict,
560+
cfg.INFER.batch_size,
561+
)
562+
563+
# mapping data to cfg.MODEL.output_keys
564+
output_dict = {
565+
store_key: output_dict[infer_key]
566+
for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys())
567+
}
568+
569+
u, v, p, rho = (
570+
output_dict["u"],
571+
output_dict["v"],
572+
output_dict["p"],
573+
output_dict["rho"],
574+
)
575+
576+
zero_mask = (
577+
(x_test[:, 1] - cfg.rx) ** 2 + (x_test[:, 2] - cfg.ry) ** 2
578+
) < cfg.rd**2
579+
u[zero_mask] = 0
580+
v[zero_mask] = 0
581+
p[zero_mask] = 0
582+
rho[zero_mask] = 0
583+
584+
u = u.reshape(cfg.Nd, cfg.Nd)
585+
v = v.reshape(cfg.Nd, cfg.Nd)
586+
p = p.reshape(cfg.Nd, cfg.Nd)
587+
rho = rho.reshape(cfg.Nd, cfg.Nd)
588+
589+
fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=(15, 15))
590+
591+
plt.subplot(2, 2, 1)
592+
plt.contourf(x_grid[:, 0, :], y_grid[:, 0, :], u * 241.315, 60)
593+
plt.title("U m/s")
594+
plt.xlabel("x")
595+
plt.ylabel("y")
596+
axe = plt.gca()
597+
axe.set_aspect(1)
598+
plt.colorbar()
599+
600+
plt.subplot(2, 2, 2)
601+
plt.contourf(x_grid[:, 0, :], y_grid[:, 0, :], v * 241.315, 60)
602+
plt.title("V m/s")
603+
plt.xlabel("x")
604+
plt.ylabel("y")
605+
axe = plt.gca()
606+
axe.set_aspect(1)
607+
plt.colorbar()
608+
609+
plt.subplot(2, 2, 3)
610+
plt.contourf(x_grid[:, 0, :], y_grid[:, 0, :], p * 33775, 60)
611+
plt.title("P Pa")
612+
plt.xlabel("x")
613+
plt.ylabel("y")
614+
axe = plt.gca()
615+
axe.set_aspect(1)
616+
plt.colorbar()
617+
618+
plt.subplot(2, 2, 4)
619+
plt.contourf(x_grid[:, 0, :], y_grid[:, 0, :], rho * 0.58, 60)
620+
plt.title("Rho kg/m^3")
621+
plt.xlabel("x")
622+
plt.ylabel("y")
623+
axe = plt.gca()
624+
axe.set_aspect(1)
625+
plt.colorbar()
626+
627+
plt.savefig(osp.join(cfg.output_dir, f"shock_wave(Ma_{cfg.MA:.3f}).png"))
628+
629+
521630
@hydra.main(
522631
version_base=None, config_path="./conf", config_name="shock_wave_Ma2.0.yaml"
523632
)
@@ -526,8 +635,14 @@ def main(cfg: DictConfig):
526635
train(cfg)
527636
elif cfg.mode == "eval":
528637
evaluate(cfg)
638+
elif cfg.mode == "export":
639+
export(cfg)
640+
elif cfg.mode == "infer":
641+
inference(cfg)
529642
else:
530-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
643+
raise ValueError(
644+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
645+
)
531646

532647

533648
if __name__ == "__main__":

0 commit comments

Comments
 (0)