@@ -373,65 +373,8 @@ def train(cfg: DictConfig):
373
373
# visualize prediction after finished training
374
374
visu_input ["qm_c" ] = np .full_like (visu_input ["qm_c" ], cfg .qm_h )
375
375
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 )
435
378
436
379
437
380
def evaluate (cfg : DictConfig ):
@@ -593,14 +536,69 @@ def evaluate(cfg: DictConfig):
593
536
# visualize prediction after finished training
594
537
visu_input ["qm_c" ] = np .full_like (visu_input ["qm_c" ], cfg .qm_h )
595
538
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 ):
597
595
x = visu_input ["x" ][: cfg .NPOINT ]
598
596
# plot temperature of heat boundary
599
597
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 )
601
599
plt .plot (x , y , label = "t = 0.0 s" )
602
600
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 )]
604
602
plt .plot (x , y , label = f"t = { (i + 1 )* 0.1 :,.1f} s" )
605
603
plt .xlabel ("A" )
606
604
plt .ylabel (r"$T_h$" )
@@ -609,10 +607,10 @@ def evaluate(cfg: DictConfig):
609
607
plt .savefig ("T_h.png" )
610
608
# plot temperature of cold boundary
611
609
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 )
613
611
plt .plot (x , y , label = "t = 0.0 s" )
614
612
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 )]
616
614
plt .plot (x , y , label = f"t = { (i + 1 )* 0.1 :,.1f} s" )
617
615
plt .xlabel ("A" )
618
616
plt .ylabel (r"$T_c$" )
@@ -621,10 +619,10 @@ def evaluate(cfg: DictConfig):
621
619
plt .savefig ("T_c.png" )
622
620
# plot temperature of wall
623
621
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 )
625
623
plt .plot (x , y , label = "t = 0.0 s" )
626
624
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 )]
628
626
plt .plot (x , y , label = f"t = { (i + 1 )* 0.1 :,.1f} s" )
629
627
plt .xlabel ("A" )
630
628
plt .ylabel (r"$T_w$" )
@@ -641,7 +639,7 @@ def evaluate(cfg: DictConfig):
641
639
qm_min
642
640
* (pred ["T_h" ][:: cfg .NPOINT ] - pred ["T_c" ][cfg .NPOINT - 1 :: cfg .NPOINT ])
643
641
)
644
- ). numpy ()
642
+ )
645
643
x = list (range (1 , cfg .NTIME + 1 ))
646
644
plt .plot (x , eta )
647
645
plt .xlabel ("time" )
@@ -660,8 +658,14 @@ def main(cfg: DictConfig):
660
658
train (cfg )
661
659
elif cfg .mode == "eval" :
662
660
evaluate (cfg )
661
+ elif cfg .mode == "export" :
662
+ export (cfg )
663
+ elif cfg .mode == "infer" :
664
+ inference (cfg )
663
665
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
+ )
665
669
666
670
667
671
if __name__ == "__main__" :
0 commit comments