@@ -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
533648if __name__ == "__main__" :
0 commit comments