Skip to content

Commit 8d28e58

Browse files
【PPSCI Export&Infer No.30】heat_exchanger (#892)
* 【PPSCI Export&Infer No.30】heat_exchanger * fix codestyle bug * update examples/heat_exchanger/heat_exchanger.py * fix codestyle bugs * Update heat_exchanger.py Fix and simplify code --------- Co-authored-by: HydrogenSulfate <[email protected]>
1 parent 78706cd commit 8d28e58

File tree

3 files changed

+103
-68
lines changed

3 files changed

+103
-68
lines changed

docs/zh/examples/heat_exchanger.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
python heat_exchanger.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/HEDeepONet/HEDeepONet_pretrained.pdparams
1313
```
1414

15+
=== "模型导出命令"
16+
17+
``` sh
18+
python heat_exchanger.py mode=export
19+
```
20+
21+
=== "模型推理命令"
22+
23+
``` sh
24+
python heat_exchanger.py mode=infer
25+
```
26+
1527
| 预训练模型 | 指标 |
1628
|:--| :--|
1729
| [heat_exchanger_pretrained.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/HEDeepONet/HEDeepONet_pretrained.pdparams) | The L2 norm error between the actual heat exchanger efficiency and the predicted heat exchanger efficiency: 0.02087<br>MSE.heat_boundary(interior_mse): 0.52005<br>MSE.cold_boundary(interior_mse): 0.16590<br>MSE.wall(interior_mse): 0.01203 |

examples/heat_exchanger/conf/heat_exchanger.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,22 @@ EVAL:
9090
qm_h: 1
9191
qm_c: 1
9292
eta_true: 0.5
93+
94+
# inference settings
95+
INFER:
96+
pretrained_model_path: https://paddle-org.bj.bcebos.com/paddlescience/models/HEDeepONet/HEDeepONet_pretrained.pdparams
97+
export_path: ./inference/ldc2d_steady_Re10
98+
pdmodel_path: ${INFER.export_path}.pdmodel
99+
pdiparams_path: ${INFER.export_path}.pdiparams
100+
onnx_path: ${INFER.export_path}.onnx
101+
device: gpu
102+
engine: native
103+
precision: fp32
104+
ir_optim: true
105+
min_subgraph_size: 5
106+
gpu_mem: 2000
107+
gpu_id: 0
108+
max_batch_size: 1000
109+
num_cpu_threads: 10
110+
batch_size: 1000
111+
input_keys: ['qm_h','qm_c',"x",'t']

examples/heat_exchanger/heat_exchanger.py

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -373,65 +373,8 @@ def train(cfg: DictConfig):
373373
# visualize prediction after finished training
374374
visu_input["qm_c"] = np.full_like(visu_input["qm_c"], cfg.qm_h)
375375
visu_input["qm_h"] = np.full_like(visu_input["qm_c"], cfg.qm_c)
376-
pred = solver.predict(visu_input)
377-
x = visu_input["x"][: cfg.NPOINT]
378-
# plot temperature of heat boundary
379-
plt.figure()
380-
y = np.full_like(pred["T_h"][: cfg.NPOINT].numpy(), cfg.T_hin)
381-
plt.plot(x, y, label="t = 0.0 s")
382-
for i in range(10):
383-
y = pred["T_h"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
384-
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
385-
plt.xlabel("A")
386-
plt.ylabel(r"$T_h$")
387-
plt.legend()
388-
plt.grid()
389-
plt.savefig("T_h.png")
390-
# plot temperature of cold boundary
391-
plt.figure()
392-
y = np.full_like(pred["T_c"][: cfg.NPOINT].numpy(), cfg.T_cin)
393-
plt.plot(x, y, label="t = 0.0 s")
394-
for i in range(10):
395-
y = pred["T_c"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
396-
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
397-
plt.xlabel("A")
398-
plt.ylabel(r"$T_c$")
399-
plt.legend()
400-
plt.grid()
401-
plt.savefig("T_c.png")
402-
# plot temperature of wall
403-
plt.figure()
404-
y = np.full_like(pred["T_w"][: cfg.NPOINT].numpy(), cfg.T_win)
405-
plt.plot(x, y, label="t = 0.0 s")
406-
for i in range(10):
407-
y = pred["T_w"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
408-
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
409-
plt.xlabel("A")
410-
plt.ylabel(r"$T_w$")
411-
plt.legend()
412-
plt.grid()
413-
plt.savefig("T_w.png")
414-
# plot the heat exchanger efficiency as a function of time.
415-
plt.figure()
416-
qm_min = np.min((visu_input["qm_h"][0], visu_input["qm_c"][0]))
417-
eta = (
418-
visu_input["qm_h"][0]
419-
* (pred["T_h"][:: cfg.NPOINT] - pred["T_h"][cfg.NPOINT - 1 :: cfg.NPOINT])
420-
/ (
421-
qm_min
422-
* (pred["T_h"][:: cfg.NPOINT] - pred["T_c"][cfg.NPOINT - 1 :: cfg.NPOINT])
423-
)
424-
).numpy()
425-
x = list(range(1, cfg.NTIME + 1))
426-
plt.plot(x, eta)
427-
plt.xlabel("time")
428-
plt.ylabel(r"$\eta$")
429-
plt.grid()
430-
plt.savefig("eta.png")
431-
error = np.square(eta[-1] - cfg.eta_true)
432-
logger.info(
433-
f"The L2 norm error between the actual heat exchanger efficiency and the predicted heat exchanger efficiency is {error}"
434-
)
376+
pred = solver.predict(visu_input, return_numpy=True)
377+
plot(visu_input, pred, cfg)
435378

436379

437380
def evaluate(cfg: DictConfig):
@@ -593,14 +536,69 @@ def evaluate(cfg: DictConfig):
593536
# visualize prediction after finished training
594537
visu_input["qm_c"] = np.full_like(visu_input["qm_c"], cfg.qm_h)
595538
visu_input["qm_h"] = np.full_like(visu_input["qm_c"], cfg.qm_c)
596-
pred = solver.predict(visu_input)
539+
pred = solver.predict(visu_input, return_numpy=True)
540+
plot(visu_input, pred, cfg)
541+
542+
543+
def export(cfg: DictConfig):
544+
# set model
545+
model = ppsci.arch.HEDeepONets(**cfg.MODEL)
546+
547+
# initialize solver
548+
solver = ppsci.solver.Solver(
549+
model,
550+
pretrained_model_path=cfg.INFER.pretrained_model_path,
551+
)
552+
# export model
553+
from paddle.static import InputSpec
554+
555+
input_spec = [
556+
{key: InputSpec([None, 1], "float32", name=key) for key in model.input_keys},
557+
]
558+
solver.export(input_spec, cfg.INFER.export_path)
559+
560+
561+
def inference(cfg: DictConfig):
562+
from deploy.python_infer import pinn_predictor
563+
564+
predictor = pinn_predictor.PINNPredictor(cfg)
565+
566+
# set time-geometry
567+
timestamps = np.linspace(0.0, 2, cfg.NTIME + 1, endpoint=True)
568+
geom = {
569+
"time_rect": ppsci.geometry.TimeXGeometry(
570+
ppsci.geometry.TimeDomain(0.0, 1, timestamps=timestamps),
571+
ppsci.geometry.Interval(0, cfg.DL),
572+
)
573+
}
574+
input_dict = geom["time_rect"].sample_interior(cfg.NPOINT * cfg.NTIME, evenly=True)
575+
test_h = np.random.rand(1).reshape([-1, 1]).astype("float32")
576+
test_c = np.random.rand(1).reshape([-1, 1]).astype("float32")
577+
# rearrange train data and eval data
578+
input_dict["qm_h"] = np.tile(test_h, (cfg.NPOINT * cfg.NTIME, 1))
579+
input_dict["qm_c"] = np.tile(test_c, (cfg.NPOINT * cfg.NTIME, 1))
580+
input_dict["qm_c"] = np.full_like(input_dict["qm_c"], cfg.qm_h)
581+
input_dict["qm_h"] = np.full_like(input_dict["qm_c"], cfg.qm_c)
582+
output_dict = predictor.predict(
583+
{key: input_dict[key] for key in cfg.INFER.input_keys}, cfg.INFER.batch_size
584+
)
585+
586+
# mapping data to cfg.INFER.output_keys
587+
output_dict = {
588+
store_key: output_dict[infer_key]
589+
for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys())
590+
}
591+
plot(input_dict, output_dict, cfg)
592+
593+
594+
def plot(visu_input, pred, cfg: DictConfig):
597595
x = visu_input["x"][: cfg.NPOINT]
598596
# plot temperature of heat boundary
599597
plt.figure()
600-
y = np.full_like(pred["T_h"][: cfg.NPOINT].numpy(), cfg.T_hin)
598+
y = np.full_like(pred["T_h"][: cfg.NPOINT], cfg.T_hin)
601599
plt.plot(x, y, label="t = 0.0 s")
602600
for i in range(10):
603-
y = pred["T_h"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
601+
y = pred["T_h"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)]
604602
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
605603
plt.xlabel("A")
606604
plt.ylabel(r"$T_h$")
@@ -609,10 +607,10 @@ def evaluate(cfg: DictConfig):
609607
plt.savefig("T_h.png")
610608
# plot temperature of cold boundary
611609
plt.figure()
612-
y = np.full_like(pred["T_c"][: cfg.NPOINT].numpy(), cfg.T_cin)
610+
y = np.full_like(pred["T_c"][: cfg.NPOINT], cfg.T_cin)
613611
plt.plot(x, y, label="t = 0.0 s")
614612
for i in range(10):
615-
y = pred["T_c"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
613+
y = pred["T_c"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)]
616614
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
617615
plt.xlabel("A")
618616
plt.ylabel(r"$T_c$")
@@ -621,10 +619,10 @@ def evaluate(cfg: DictConfig):
621619
plt.savefig("T_c.png")
622620
# plot temperature of wall
623621
plt.figure()
624-
y = np.full_like(pred["T_w"][: cfg.NPOINT].numpy(), cfg.T_win)
622+
y = np.full_like(pred["T_w"][: cfg.NPOINT], cfg.T_win)
625623
plt.plot(x, y, label="t = 0.0 s")
626624
for i in range(10):
627-
y = pred["T_w"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)].numpy()
625+
y = pred["T_w"][cfg.NPOINT * i * 2 : cfg.NPOINT * (i * 2 + 1)]
628626
plt.plot(x, y, label=f"t = {(i+1)*0.1:,.1f} s")
629627
plt.xlabel("A")
630628
plt.ylabel(r"$T_w$")
@@ -641,7 +639,7 @@ def evaluate(cfg: DictConfig):
641639
qm_min
642640
* (pred["T_h"][:: cfg.NPOINT] - pred["T_c"][cfg.NPOINT - 1 :: cfg.NPOINT])
643641
)
644-
).numpy()
642+
)
645643
x = list(range(1, cfg.NTIME + 1))
646644
plt.plot(x, eta)
647645
plt.xlabel("time")
@@ -660,8 +658,14 @@ def main(cfg: DictConfig):
660658
train(cfg)
661659
elif cfg.mode == "eval":
662660
evaluate(cfg)
661+
elif cfg.mode == "export":
662+
export(cfg)
663+
elif cfg.mode == "infer":
664+
inference(cfg)
663665
else:
664-
raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'")
666+
raise ValueError(
667+
f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'"
668+
)
665669

666670

667671
if __name__ == "__main__":

0 commit comments

Comments
 (0)