@@ -39,7 +39,7 @@ def plotting(figname, output_dir, data, griddata_points, griddata_xi, boundary):
39
39
for i , key in enumerate (data ):
40
40
plot_data = griddata (
41
41
griddata_points ,
42
- data [key ].numpy (). flatten (),
42
+ data [key ].flatten (),
43
43
griddata_xi ,
44
44
method = "cubic" ,
45
45
)
@@ -343,7 +343,94 @@ def compute_outs(w, x, y):
343
343
plotting (
344
344
"eval_Mx_Mxy_My_Qx_Qy_w" ,
345
345
cfg .output_dir ,
346
- outs ,
346
+ {k : v .numpy () for k , v in outs .items ()},
347
+ griddata_points ,
348
+ griddata_xi ,
349
+ boundary ,
350
+ )
351
+
352
+
353
+ def export (cfg : DictConfig ):
354
+ from paddle import nn
355
+ from paddle .static import InputSpec
356
+
357
+ # set models
358
+ disp_net = ppsci .arch .MLP (** cfg .MODEL )
359
+
360
+ # load pretrained model
361
+ solver = ppsci .solver .Solver (
362
+ model = disp_net , pretrained_model_path = cfg .INFER .pretrained_model_path
363
+ )
364
+
365
+ class Wrapped_Model (nn .Layer ):
366
+ def __init__ (self , model ):
367
+ super ().__init__ ()
368
+ self .model = model
369
+
370
+ def forward (self , x ):
371
+ model_out = self .model (x )
372
+ outs = self .compute_outs (model_out ["u" ], x ["x" ], x ["y" ])
373
+ return outs
374
+
375
+ def compute_outs (self , w , x , y ):
376
+ D = cfg .E * (cfg .HEIGHT ** 3 ) / (12.0 * (1.0 - cfg .NU ** 2 ))
377
+ w_x2 = hessian (w , x )
378
+ w_y2 = hessian (w , y )
379
+ w_x_y = jacobian (jacobian (w , x ), y )
380
+ M_x = - (w_x2 + cfg .NU * w_y2 ) * D
381
+ M_y = - (cfg .NU * w_x2 + w_y2 ) * D
382
+ M_xy = (1 - cfg .NU ) * w_x_y * D
383
+ Q_x = - jacobian ((w_x2 + w_y2 ), x ) * D
384
+ Q_y = - jacobian ((w_x2 + w_y2 ), y ) * D
385
+ return {"Mx" : M_x , "Mxy" : M_xy , "My" : M_y , "Qx" : Q_x , "Qy" : Q_y , "w" : w }
386
+
387
+ solver .model = Wrapped_Model (solver .model )
388
+
389
+ # export models
390
+ input_spec = [
391
+ {key : InputSpec ([None , 1 ], "float32" , name = key ) for key in disp_net .input_keys },
392
+ ]
393
+ solver .export (input_spec , cfg .INFER .export_path )
394
+
395
+
396
+ def inference (cfg : DictConfig ):
397
+ from deploy .python_infer import pinn_predictor
398
+
399
+ # set model predictor
400
+ predictor = pinn_predictor .PINNPredictor (cfg )
401
+
402
+ # generate samples
403
+ num_x = 201
404
+ num_y = 301
405
+ x_grad , y_grad = np .meshgrid (
406
+ np .linspace (
407
+ start = 0 , stop = cfg .LENGTH , num = num_x , endpoint = True , dtype = np .float32
408
+ ),
409
+ np .linspace (
410
+ start = 0 , stop = cfg .WIDTH , num = num_y , endpoint = True , dtype = np .float32
411
+ ),
412
+ )
413
+ x_faltten = x_grad .reshape (- 1 , 1 )
414
+ y_faltten = y_grad .reshape (- 1 , 1 )
415
+
416
+ output_dict = predictor .predict (
417
+ {"x" : x_faltten , "y" : y_faltten }, cfg .INFER .batch_size
418
+ )
419
+
420
+ # mapping data to cfg.INFER.output_keys
421
+ output_dict = {
422
+ store_key : output_dict [infer_key ]
423
+ for store_key , infer_key in zip (cfg .INFER .output_keys , output_dict .keys ())
424
+ }
425
+
426
+ # plotting
427
+ griddata_points = np .concatenate ([x_faltten , y_faltten ], axis = - 1 )
428
+ griddata_xi = (x_grad , y_grad )
429
+ boundary = [0 , cfg .LENGTH , 0 , cfg .WIDTH ]
430
+ plotting (
431
+ "eval_Mx_Mxy_My_Qx_Qy_w" ,
432
+ cfg .output_dir ,
433
+ output_dict ,
347
434
griddata_points ,
348
435
griddata_xi ,
349
436
boundary ,
@@ -356,8 +443,14 @@ def main(cfg: DictConfig):
356
443
train (cfg )
357
444
elif cfg .mode == "eval" :
358
445
evaluate (cfg )
446
+ elif cfg .mode == "export" :
447
+ export (cfg )
448
+ elif cfg .mode == "infer" :
449
+ inference (cfg )
359
450
else :
360
- raise ValueError (f"cfg.mode should in ['train', 'eval'], but got '{ cfg .mode } '" )
451
+ raise ValueError (
452
+ f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{ cfg .mode } '"
453
+ )
361
454
362
455
363
456
if __name__ == "__main__" :
0 commit comments