本问题主要研究如下汽车悬挂控制臂结构上的受力分析情况以及验证在不给定附加数据的情况下进行参数逆推的可能性,并使用深度学习方法根据线弹性等方程进行求解,结构如下所示,左侧单一圆环内表面受力,右侧两圆环内表面固定,共研究了受力方向为:x 轴负方向、z 轴正方向两种情况,下面以 x 轴正方向受力为例进行说明。
# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)# wrap to a model_listmodel_list=ppsci.arch.ModelList((disp_net,stress_net))
# set optimizerlr_scheduler=ppsci.optimizer.lr_scheduler.ExponentialDecay(**cfg.TRAIN.lr_scheduler)()optimizer=ppsci.optimizer.Adam(lr_scheduler)(model_list)
# set dataloader configtrain_dataloader_cfg={"dataset":"NamedArrayDataset","iters_per_epoch":cfg.TRAIN.iters_per_epoch,"sampler":{"name":"BatchSampler","drop_last":True,"shuffle":True,},"num_workers":1,}
defevaluate(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)# wrap to a model_listmodel_list=ppsci.arch.ModelList((disp_net,stress_net))# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.TRAIN.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_u_v_w_sigmas":ppsci.visualize.VisualizerVtu(pred_input_dict,{"u":lambdaout:out["u"],"v":lambdaout:out["v"],"w":lambdaout:out["w"],"sigma_xx":lambdaout:out["sigma_xx"],"sigma_yy":lambdaout:out["sigma_yy"],"sigma_zz":lambdaout:out["sigma_zz"],"sigma_xy":lambdaout:out["sigma_xy"],"sigma_xz":lambdaout:out["sigma_xz"],"sigma_yz":lambdaout:out["sigma_yz"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model_list,output_dir=cfg.output_dir,seed=cfg.seed,geom=geom,log_freq=cfg.log_freq,eval_with_no_grad=cfg.EVAL.eval_with_no_grad,visualizer=visualizer,pretrained_model_path=cfg.EVAL.pretrained_model_path,)# visualize prediction after finished trainingsolver.visualize()
# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)inverse_lambda_net=ppsci.arch.MLP(**cfg.MODEL.inverse_lambda_net)inverse_mu_net=ppsci.arch.MLP(**cfg.MODEL.inverse_mu_net)# freeze modelsdisp_net.freeze()stress_net.freeze()# wrap to a model_listmodel=ppsci.arch.ModelList((disp_net,stress_net,inverse_lambda_net,inverse_mu_net))
# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds
# set optimizerlr_scheduler=ppsci.optimizer.lr_scheduler.ExponentialDecay(**cfg.TRAIN.lr_scheduler)()optimizer=ppsci.optimizer.Adam(lr_scheduler)((inverse_lambda_net,inverse_mu_net))
# set dataloader configinterior_constraint=ppsci.constraint.InteriorConstraint(equation["LinearElasticity"].equations,{"stress_disp_xx":0,"stress_disp_yy":0,"stress_disp_zz":0,"stress_disp_xy":0,"stress_disp_xz":0,"stress_disp_yz":0,},geom["geo"],{"dataset":"NamedArrayDataset","iters_per_epoch":cfg.TRAIN.iters_per_epoch,"sampler":{"name":"BatchSampler","drop_last":True,"shuffle":True,},"num_workers":1,"batch_size":cfg.TRAIN.batch_size.arm_interior,},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),name="INTERIOR",)
# set validatorLAMBDA_=cfg.NU*cfg.E/((1+cfg.NU)*(1-2*cfg.NU))# 0.5769MU=cfg.E/(2*(1+cfg.NU))# 0.3846geom_validator=ppsci.validate.GeometryValidator({"lambda_":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},{"lambda_":LAMBDA_,"mu":MU,},geom["geo"],{"dataset":"NamedArrayDataset","sampler":{"name":"BatchSampler","drop_last":False,"shuffle":False,},"total_size":cfg.EVAL.total_size.validator,"batch_size":cfg.EVAL.batch_size.validator,},ppsci.loss.MSELoss("sum"),metric={"L2Rel":ppsci.metric.L2Rel()},name="geo_eval",)validator={geom_validator.name:geom_validator}
defevaluate(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)inverse_lambda_net=ppsci.arch.MLP(**cfg.MODEL.inverse_lambda_net)inverse_mu_net=ppsci.arch.MLP(**cfg.MODEL.inverse_mu_net)# wrap to a model_listmodel=ppsci.arch.ModelList((disp_net,stress_net,inverse_lambda_net,inverse_mu_net))# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set validatorLAMBDA_=cfg.NU*cfg.E/((1+cfg.NU)*(1-2*cfg.NU))# 0.57692MU=cfg.E/(2*(1+cfg.NU))# 0.38462geom_validator=ppsci.validate.GeometryValidator({"lambda_":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},{"lambda_":LAMBDA_,"mu":MU,},geom["geo"],{"dataset":"NamedArrayDataset","sampler":{"name":"BatchSampler","drop_last":False,"shuffle":False,},"total_size":cfg.EVAL.total_size.validator,"batch_size":cfg.EVAL.batch_size.validator,},ppsci.loss.MSELoss("sum"),metric={"L2Rel":ppsci.metric.L2Rel()},name="geo_eval",)validator={geom_validator.name:geom_validator}# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.EVAL.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_lambda_mu":ppsci.visualize.VisualizerVtu(pred_input_dict,{"lambda":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model,output_dir=cfg.output_dir,seed=cfg.seed,log_freq=cfg.log_freq,eval_with_no_grad=cfg.EVAL.eval_with_no_grad,validator=validator,visualizer=visualizer,pretrained_model_path=cfg.EVAL.pretrained_model_path,)# evaluate after finished trainingsolver.eval()# visualize prediction after finished trainingsolver.visualize()
fromosimportpathasospimporthydraimportnumpyasnpfromomegaconfimportDictConfigfrompaddleimportdistributedasdistimportppscifromppsci.utilsimportloggerdeftrain(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set parallelenable_parallel=dist.get_world_size()>1# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)# wrap to a model_listmodel_list=ppsci.arch.ModelList((disp_net,stress_net))# set optimizerlr_scheduler=ppsci.optimizer.lr_scheduler.ExponentialDecay(**cfg.TRAIN.lr_scheduler)()optimizer=ppsci.optimizer.Adam(lr_scheduler)(model_list)# specify parametersLAMBDA_=cfg.NU*cfg.E/((1+cfg.NU)*(1-2*cfg.NU))MU=cfg.E/(2*(1+cfg.NU))# set equationequation={"LinearElasticity":ppsci.equation.LinearElasticity(E=None,nu=None,lambda_=LAMBDA_,mu=MU,dim=3)}# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)geom={"geo":control_arm}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set dataloader configtrain_dataloader_cfg={"dataset":"NamedArrayDataset","iters_per_epoch":cfg.TRAIN.iters_per_epoch,"sampler":{"name":"BatchSampler","drop_last":True,"shuffle":True,},"num_workers":1,}# set constraintarm_left_constraint=ppsci.constraint.BoundaryConstraint(equation["LinearElasticity"].equations,{"traction_x":cfg.T[0],"traction_y":cfg.T[1],"traction_z":cfg.T[2]},geom["geo"],{**train_dataloader_cfg,"batch_size":cfg.TRAIN.batch_size.arm_left},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:np.sqrt(np.square(x-cfg.CIRCLE_LEFT_CENTER_XY[0])+np.square(y-cfg.CIRCLE_LEFT_CENTER_XY[1]))<=cfg.CIRCLE_LEFT_RADIUS+1e-1,name="BC_LEFT",)arm_right_constraint=ppsci.constraint.BoundaryConstraint({"u":lambdad:d["u"],"v":lambdad:d["v"],"w":lambdad:d["w"]},{"u":0,"v":0,"w":0},geom["geo"],{**train_dataloader_cfg,"batch_size":cfg.TRAIN.batch_size.arm_right},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:np.sqrt(np.square(x-cfg.CIRCLE_RIGHT_CENTER_XZ[0])+np.square(z-cfg.CIRCLE_RIGHT_CENTER_XZ[1]))<=cfg.CIRCLE_RIGHT_RADIUS+1e-1,weight_dict=cfg.TRAIN.weight.arm_right,name="BC_RIGHT",)arm_surface_constraint=ppsci.constraint.BoundaryConstraint(equation["LinearElasticity"].equations,{"traction_x":0,"traction_y":0,"traction_z":0},geom["geo"],{**train_dataloader_cfg,"batch_size":cfg.TRAIN.batch_size.arm_surface},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:np.sqrt(np.square(x-cfg.CIRCLE_LEFT_CENTER_XY[0])+np.square(y-cfg.CIRCLE_LEFT_CENTER_XY[1]))>cfg.CIRCLE_LEFT_RADIUS+1e-1,name="BC_SURFACE",)arm_interior_constraint=ppsci.constraint.InteriorConstraint(equation["LinearElasticity"].equations,{"equilibrium_x":0,"equilibrium_y":0,"equilibrium_z":0,"stress_disp_xx":0,"stress_disp_yy":0,"stress_disp_zz":0,"stress_disp_xy":0,"stress_disp_xz":0,"stress_disp_yz":0,},geom["geo"],{**train_dataloader_cfg,"batch_size":cfg.TRAIN.batch_size.arm_interior},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),weight_dict={"equilibrium_x":"sdf","equilibrium_y":"sdf","equilibrium_z":"sdf","stress_disp_xx":"sdf","stress_disp_yy":"sdf","stress_disp_zz":"sdf","stress_disp_xy":"sdf","stress_disp_xz":"sdf","stress_disp_yz":"sdf",},name="INTERIOR",)# re-assign to cfg.TRAIN.iters_per_epochifenable_parallel:cfg.TRAIN.iters_per_epoch=len(arm_left_constraint.data_loader)# wrap constraints togethergconstraint={arm_left_constraint.name:arm_left_constraint,arm_right_constraint.name:arm_right_constraint,arm_surface_constraint.name:arm_surface_constraint,arm_interior_constraint.name:arm_interior_constraint,}# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.TRAIN.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_u_v_w_sigmas":ppsci.visualize.VisualizerVtu(pred_input_dict,{"u":lambdaout:out["u"],"v":lambdaout:out["v"],"w":lambdaout:out["w"],"sigma_xx":lambdaout:out["sigma_xx"],"sigma_yy":lambdaout:out["sigma_yy"],"sigma_zz":lambdaout:out["sigma_zz"],"sigma_xy":lambdaout:out["sigma_xy"],"sigma_xz":lambdaout:out["sigma_xz"],"sigma_yz":lambdaout:out["sigma_yz"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model_list,constraint,cfg.output_dir,optimizer,lr_scheduler,cfg.TRAIN.epochs,cfg.TRAIN.iters_per_epoch,seed=cfg.seed,equation=equation,geom=geom,save_freq=cfg.TRAIN.save_freq,log_freq=cfg.log_freq,eval_freq=cfg.TRAIN.eval_freq,eval_during_train=cfg.TRAIN.eval_during_train,eval_with_no_grad=cfg.TRAIN.eval_with_no_grad,visualizer=visualizer,checkpoint_path=cfg.TRAIN.checkpoint_path,)# train modelsolver.train()# plot lossessolver.plot_loss_history(by_epoch=True,smooth_step=1)defevaluate(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)# wrap to a model_listmodel_list=ppsci.arch.ModelList((disp_net,stress_net))# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.TRAIN.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_u_v_w_sigmas":ppsci.visualize.VisualizerVtu(pred_input_dict,{"u":lambdaout:out["u"],"v":lambdaout:out["v"],"w":lambdaout:out["w"],"sigma_xx":lambdaout:out["sigma_xx"],"sigma_yy":lambdaout:out["sigma_yy"],"sigma_zz":lambdaout:out["sigma_zz"],"sigma_xy":lambdaout:out["sigma_xy"],"sigma_xz":lambdaout:out["sigma_xz"],"sigma_yz":lambdaout:out["sigma_yz"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model_list,output_dir=cfg.output_dir,seed=cfg.seed,geom=geom,log_freq=cfg.log_freq,eval_with_no_grad=cfg.EVAL.eval_with_no_grad,visualizer=visualizer,pretrained_model_path=cfg.EVAL.pretrained_model_path,)# visualize prediction after finished trainingsolver.visualize()defexport(cfg:DictConfig):frompaddle.staticimportInputSpec# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)# wrap to a model_listmodel_list=ppsci.arch.ModelList((disp_net,stress_net))# load pretrained modelsolver=ppsci.solver.Solver(model=model_list,pretrained_model_path=cfg.INFER.pretrained_model_path)# export modelsinput_spec=[{key:InputSpec([None,1],"float32",name=key)forkeyincfg.MODEL.disp_net.input_keys},]solver.export(input_spec,cfg.INFER.export_path)definference(cfg:DictConfig):fromdeploy.python_inferimportpinn_predictorfromppsci.visualizeimportvtu# set model predictorpredictor=pinn_predictor.PINNPredictor(cfg)# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.TRAIN.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}output_dict=predictor.predict(pred_input_dict,cfg.INFER.batch_size)# mapping data to output_keysoutput_keys=cfg.MODEL.disp_net.output_keys+cfg.MODEL.stress_net.output_keysoutput_dict={store_key:output_dict[infer_key]forstore_key,infer_keyinzip(output_keys,output_dict.keys())}output_dict.update(pred_input_dict)vtu.save_vtu_from_dict(osp.join(cfg.output_dir,"vis"),output_dict,cfg.MODEL.disp_net.input_keys,output_keys,1,)@hydra.main(version_base=None,config_path="./conf",config_name="forward_analysis.yaml")defmain(cfg:DictConfig):ifcfg.mode=="train":train(cfg)elifcfg.mode=="eval":evaluate(cfg)elifcfg.mode=="export":export(cfg)elifcfg.mode=="infer":inference(cfg)else:raiseValueError(f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'")if__name__=="__main__":main()
fromosimportpathasospimporthydrafromomegaconfimportDictConfigimportppscifromppsci.utilsimportloggerdeftrain(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)inverse_lambda_net=ppsci.arch.MLP(**cfg.MODEL.inverse_lambda_net)inverse_mu_net=ppsci.arch.MLP(**cfg.MODEL.inverse_mu_net)# freeze modelsdisp_net.freeze()stress_net.freeze()# wrap to a model_listmodel=ppsci.arch.ModelList((disp_net,stress_net,inverse_lambda_net,inverse_mu_net))# set optimizerlr_scheduler=ppsci.optimizer.lr_scheduler.ExponentialDecay(**cfg.TRAIN.lr_scheduler)()optimizer=ppsci.optimizer.Adam(lr_scheduler)((inverse_lambda_net,inverse_mu_net))# set equationequation={"LinearElasticity":ppsci.equation.LinearElasticity(E=None,nu=None,lambda_="lambda_",mu="mu",dim=3)}# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set dataloader configinterior_constraint=ppsci.constraint.InteriorConstraint(equation["LinearElasticity"].equations,{"stress_disp_xx":0,"stress_disp_yy":0,"stress_disp_zz":0,"stress_disp_xy":0,"stress_disp_xz":0,"stress_disp_yz":0,},geom["geo"],{"dataset":"NamedArrayDataset","iters_per_epoch":cfg.TRAIN.iters_per_epoch,"sampler":{"name":"BatchSampler","drop_last":True,"shuffle":True,},"num_workers":1,"batch_size":cfg.TRAIN.batch_size.arm_interior,},ppsci.loss.MSELoss("sum"),criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),name="INTERIOR",)constraint={interior_constraint.name:interior_constraint}# set validatorLAMBDA_=cfg.NU*cfg.E/((1+cfg.NU)*(1-2*cfg.NU))# 0.5769MU=cfg.E/(2*(1+cfg.NU))# 0.3846geom_validator=ppsci.validate.GeometryValidator({"lambda_":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},{"lambda_":LAMBDA_,"mu":MU,},geom["geo"],{"dataset":"NamedArrayDataset","sampler":{"name":"BatchSampler","drop_last":False,"shuffle":False,},"total_size":cfg.EVAL.total_size.validator,"batch_size":cfg.EVAL.batch_size.validator,},ppsci.loss.MSELoss("sum"),metric={"L2Rel":ppsci.metric.L2Rel()},name="geo_eval",)validator={geom_validator.name:geom_validator}# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.TRAIN.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_lambda_mu":ppsci.visualize.VisualizerVtu(pred_input_dict,{"lambda":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model,constraint,cfg.output_dir,optimizer,lr_scheduler,cfg.TRAIN.epochs,cfg.TRAIN.iters_per_epoch,seed=cfg.seed,equation=equation,geom=geom,save_freq=cfg.TRAIN.save_freq,log_freq=cfg.log_freq,eval_freq=cfg.TRAIN.eval_freq,eval_during_train=cfg.TRAIN.eval_during_train,eval_with_no_grad=cfg.TRAIN.eval_with_no_grad,validator=validator,visualizer=visualizer,pretrained_model_path=cfg.TRAIN.pretrained_model_path,)# train modelsolver.train()# plot lossessolver.plot_loss_history(by_epoch=False,smooth_step=1,use_semilogy=True)defevaluate(cfg:DictConfig):# set random seed for reproducibilityppsci.utils.misc.set_random_seed(cfg.seed)# initialize loggerlogger.init_logger("ppsci",osp.join(cfg.output_dir,f"{cfg.mode}.log"),"info")# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)inverse_lambda_net=ppsci.arch.MLP(**cfg.MODEL.inverse_lambda_net)inverse_mu_net=ppsci.arch.MLP(**cfg.MODEL.inverse_mu_net)# wrap to a model_listmodel=ppsci.arch.ModelList((disp_net,stress_net,inverse_lambda_net,inverse_mu_net))# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.bounds# set validatorLAMBDA_=cfg.NU*cfg.E/((1+cfg.NU)*(1-2*cfg.NU))# 0.57692MU=cfg.E/(2*(1+cfg.NU))# 0.38462geom_validator=ppsci.validate.GeometryValidator({"lambda_":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},{"lambda_":LAMBDA_,"mu":MU,},geom["geo"],{"dataset":"NamedArrayDataset","sampler":{"name":"BatchSampler","drop_last":False,"shuffle":False,},"total_size":cfg.EVAL.total_size.validator,"batch_size":cfg.EVAL.batch_size.validator,},ppsci.loss.MSELoss("sum"),metric={"L2Rel":ppsci.metric.L2Rel()},name="geo_eval",)validator={geom_validator.name:geom_validator}# set visualizer(optional)# add inferencer datasamples=geom["geo"].sample_interior(cfg.EVAL.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}visualizer={"visulzie_lambda_mu":ppsci.visualize.VisualizerVtu(pred_input_dict,{"lambda":lambdaout:out["lambda_"],"mu":lambdaout:out["mu"],},prefix="vis",)}# initialize solversolver=ppsci.solver.Solver(model,output_dir=cfg.output_dir,seed=cfg.seed,log_freq=cfg.log_freq,eval_with_no_grad=cfg.EVAL.eval_with_no_grad,validator=validator,visualizer=visualizer,pretrained_model_path=cfg.EVAL.pretrained_model_path,)# evaluate after finished trainingsolver.eval()# visualize prediction after finished trainingsolver.visualize()defexport(cfg:DictConfig):frompaddle.staticimportInputSpec# set modeldisp_net=ppsci.arch.MLP(**cfg.MODEL.disp_net)stress_net=ppsci.arch.MLP(**cfg.MODEL.stress_net)inverse_lambda_net=ppsci.arch.MLP(**cfg.MODEL.inverse_lambda_net)inverse_mu_net=ppsci.arch.MLP(**cfg.MODEL.inverse_mu_net)# wrap to a model_listmodel=ppsci.arch.ModelList((disp_net,stress_net,inverse_lambda_net,inverse_mu_net))# load pretrained modelsolver=ppsci.solver.Solver(model=model,pretrained_model_path=cfg.INFER.pretrained_model_path)# export modelsinput_spec=[{key:InputSpec([None,1],"float32",name=key)forkeyincfg.MODEL.disp_net.input_keys},]solver.export(input_spec,cfg.INFER.export_path)definference(cfg:DictConfig):fromdeploy.python_inferimportpinn_predictorfromppsci.visualizeimportvtu# set model predictorpredictor=pinn_predictor.PINNPredictor(cfg)# set geometrycontrol_arm=ppsci.geometry.Mesh(cfg.GEOM_PATH)# geometry bool operationgeo=control_armgeom={"geo":geo}# set boundsBOUNDS_X,BOUNDS_Y,BOUNDS_Z=control_arm.boundssamples=geom["geo"].sample_interior(cfg.EVAL.batch_size.visualizer_vtu,criteria=lambdax,y,z:((BOUNDS_X[0]<x)&(x<BOUNDS_X[1])&(BOUNDS_Y[0]<y)&(y<BOUNDS_Y[1])&(BOUNDS_Z[0]<z)&(z<BOUNDS_Z[1])),)pred_input_dict={k:vfork,vinsamples.items()ifkincfg.MODEL.disp_net.input_keys}output_dict=predictor.predict(pred_input_dict,cfg.INFER.batch_size)# mapping data to output_keysoutput_keys=(cfg.MODEL.disp_net.output_keys+cfg.MODEL.stress_net.output_keys+cfg.MODEL.inverse_lambda_net.output_keys+cfg.MODEL.inverse_mu_net.output_keys)output_dict={store_key:output_dict[infer_key]forstore_key,infer_keyinzip(output_keys,output_dict.keys())}output_dict.update(pred_input_dict)vtu.save_vtu_from_dict(osp.join(cfg.output_dir,"vis"),output_dict,cfg.MODEL.disp_net.input_keys,output_keys,1,)@hydra.main(version_base=None,config_path="./conf",config_name="inverse_parameter.yaml")defmain(cfg:DictConfig):ifcfg.mode=="train":train(cfg)elifcfg.mode=="eval":evaluate(cfg)elifcfg.mode=="export":export(cfg)elifcfg.mode=="infer":inference(cfg)else:raiseValueError(f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'")if__name__=="__main__":main()