From a93b1354525369af5fed0bf59f726c6020054d05 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 25 Nov 2024 18:47:35 +0800 Subject: [PATCH 01/17] add_e_i --- docs/zh/examples/labelfree_DNN_surrogate.md | 15 ++ examples/pipe/conf/poiseuille_flow.yaml | 18 ++ examples/pipe/poiseuille_flow.py | 257 +++++++++++++++++++- 3 files changed, 288 insertions(+), 2 deletions(-) diff --git a/docs/zh/examples/labelfree_DNN_surrogate.md b/docs/zh/examples/labelfree_DNN_surrogate.md index bf72c0011d..f5586b51b3 100644 --- a/docs/zh/examples/labelfree_DNN_surrogate.md +++ b/docs/zh/examples/labelfree_DNN_surrogate.md @@ -34,6 +34,21 @@ python aneurysm_flow.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/aneurysm_flow.pdparams ``` + +=== "模型导出命令" + + ``` sh + python poiseuille_flow.py mode=export + ``` + + +=== "模型推理命令" + + ``` sh + python poiseuille_flow.py mode=infer + ``` + + | 预训练模型 | 指标 | |:--| :--| |[aneurysm_flow.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/aneurysm_flow.pdparams)| L-2 error u : 2.548e-4
L-2 error v : 7.169e-5 | diff --git a/examples/pipe/conf/poiseuille_flow.yaml b/examples/pipe/conf/poiseuille_flow.yaml index c12105d074..63f0e7dd07 100644 --- a/examples/pipe/conf/poiseuille_flow.yaml +++ b/examples/pipe/conf/poiseuille_flow.yaml @@ -76,3 +76,21 @@ TRAIN: EVAL: pretrained_model_path: null eval_with_no_grad: true + +# export settings +EXPORT: + pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/poiseuille_flow_pretrained.pdparams" + +# inference settings +INFER: + pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/poiseuille_flow_pretrained.pdparams" + device: gpu + engine: native + precision: fp32 + ir_optim: true + min_subgraph_size: 5 + gpu_mem: 2000 + gpu_id: 0 + max_batch_size: 8192 + num_cpu_threads: 10 + batch_size: 8192 \ No newline at end of file diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 519a856d6c..c51d2f98d3 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -429,15 +429,268 @@ def forward(self, output_dict, label_dict): plt.savefig(osp.join(PLOT_DIR, "pipe_unformUQ.png"), bbox_inches="tight") +def export(cfg): + from paddle.static import InputSpec + model_u = ppsci.arch.MLP(**cfg.MODEL.u_net) + model_v = ppsci.arch.MLP(**cfg.MODEL.v_net) + model_p = ppsci.arch.MLP(**cfg.MODEL.p_net) + + solver_u = ppsci.solver.Solver(model_u, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + solver_v = ppsci.solver.Solver(model_v, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + solver_p = ppsci.solver.Solver(model_p, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + + input_spec_u = [ + {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.u_net.input_keys}, + ] + input_spec_v = [ + {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.v_net.input_keys}, + ] + input_spec_p = [ + {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.p_net.input_keys}, + ] + + export_path_u = os.path.join(cfg.output_dir, "u_net") + export_path_v = os.path.join(cfg.output_dir, "v_net") + export_path_p = os.path.join(cfg.output_dir, "p_net") + + solver_u.export(input_spec_u, export_path_u) + solver_v.export(input_spec_v, export_path_v) + solver_p.export(input_spec_p, export_path_p) + + print(f"Inference models have been exported to {cfg.output_dir}.") + + +def inference(cfg: DictConfig): + NU_MEAN = 0.001 + NU_STD = 0.9 + L = 1.0 # length of pipe + R = 0.05 # radius of pipe + RHO = 1 # density + P_OUT = 0 # pressure at the outlet of pipe + P_IN = 0.1 # pressure at the inlet of pipe + N_x = 10 + N_y = 50 + N_p = 50 + X_IN = 0 + X_OUT = X_IN + L + Y_START = -R + Y_END = Y_START + 2 * R + NU_START = NU_MEAN - NU_MEAN * NU_STD # 0.0001 + NU_END = NU_MEAN + NU_MEAN * NU_STD # 0.1 + + data_1d_x = np.linspace( + X_IN, X_OUT, N_x, endpoint=True, dtype=paddle.get_default_dtype() + ) + data_1d_y = np.linspace( + Y_START, Y_END, N_y, endpoint=True, dtype=paddle.get_default_dtype() + ) + data_1d_nu = np.linspace( + NU_START, NU_END, N_p, endpoint=True, dtype=paddle.get_default_dtype() + ) + data_2d_xy = ( + np.array(np.meshgrid(data_1d_x, data_1d_y, data_1d_nu)).reshape(3, -1).T + ) + + model_u = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("u",), 3, 50, "swish") + model_v = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("v",), 3, 50, "swish") + model_p = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("p",), 3, 50, "swish") + + class Transform: + def input_trans(self, input): + self.input = input + x, y = input["x"], input["y"] + nu = input["nu"] + b = 2 * np.pi / (X_OUT - X_IN) + c = np.pi * (X_IN + X_OUT) / (X_IN - X_OUT) + sin_x = X_IN * paddle.sin(b * x + c) + cos_x = X_IN * paddle.cos(b * x + c) + return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} + + def output_trans_u(self, input, out): + return {"u": out["u"] * (R ** 2 - self.input["y"] ** 2)} + + def output_trans_v(self, input, out): + return {"v": (R ** 2 - self.input["y"] ** 2) * out["v"]} + + def output_trans_p(self, input, out): + return { + "p": ( + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + ) + } + + transform = Transform() + model_u.register_input_transform(transform.input_trans) + model_v.register_input_transform(transform.input_trans) + model_p.register_input_transform(transform.input_trans) + model_u.register_output_transform(transform.output_trans_u) + model_v.register_output_transform(transform.output_trans_v) + model_p.register_output_transform(transform.output_trans_p) + model = ppsci.arch.ModelList((model_u, model_v, model_p)) + + input_dict = { + "x": data_2d_xy[:, 0:1], + "y": data_2d_xy[:, 1:2], + "nu": data_2d_xy[:, 2:3], + } + u_analytical = np.zeros([N_y, N_x, N_p]) + dP = P_IN - P_OUT + + for i in range(N_p): + uy = (R ** 2 - data_1d_y ** 2) * dP / (2 * L * data_1d_nu[i] * RHO) + u_analytical[:, :, i] = np.tile(uy.reshape([N_y, 1]), N_x) + + label_dict = {"u": np.ones_like(input_dict["x"])} + weight_dict = {"u": np.ones_like(input_dict["x"])} + + num_test = 500 + data_1d_nu_distribution = np.random.normal(NU_MEAN, 0.2 * NU_MEAN, num_test) + data_2d_xy_test = ( + np.array( + np.meshgrid((X_IN - X_OUT) / 2.0, 0, data_1d_nu_distribution), np.float32 + ) + .reshape(3, -1) + .T + ) + input_dict_KL = { + "x": data_2d_xy_test[:, 0:1], + "y": data_2d_xy_test[:, 1:2], + "nu": data_2d_xy_test[:, 2:3], + } + u_max_a = (R ** 2) * dP / (2 * L * data_1d_nu_distribution * RHO) + label_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} + weight_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} + + dataset_vel = { + "name": "NamedArrayDataset", + "input": input_dict, + "label": label_dict, + "weight": weight_dict, + } + dataset_kl = { + "name": "NamedArrayDataset", + "input": input_dict_KL, + "label": label_dict_KL, + "weight": weight_dict_KL, + } + eval_cfg = { + "sampler": { + "name": "BatchSampler", + "shuffle": False, + "drop_last": False, + }, + "batch_size": 2000, + } + eval_cfg["dataset"] = dataset_vel + + solver = ppsci.solver.Solver( + model, + output_dir=cfg.output_dir, + pretrained_model_path=cfg.INFER.pretrained_model_path, + ) + + # inference + output_dict = solver.predict(input_dict, return_numpy=True) + u_pred = output_dict["u"].reshape(N_y, N_x, N_p) + + PLOT_DIR = osp.join(cfg.output_dir, "visu") + os.makedirs(PLOT_DIR, exist_ok=True) + + fontsize = 16 + idx_X = int(round(N_x / 2)) # pipe velocity section at L/2 + nu_index = [3, 6, 9, 12, 14, 20, 49] # pick 7 nu samples + ytext = [0.55, 0.5, 0.4, 0.28, 0.1, 0.05, 0.001] # text y position + + plt.figure(1) + plt.clf() + for idxP in range(len(nu_index)): + ax1 = plt.subplot(111) + plt.plot( + data_1d_y, + u_analytical[:, idx_X, nu_index[idxP]], + color="darkblue", + linestyle="-", + lw=3.0, + alpha=1.0, + ) + plt.plot( + data_1d_y, + u_pred[:, idx_X, nu_index[idxP]], + color="red", + linestyle="--", + dashes=(5, 5), + lw=2.0, + alpha=1.0, + ) + plt.text( + -0.012, + ytext[idxP], + rf"$\nu = $ {data_1d_nu[nu_index[idxP]]:.2g}", + {"color": "k", "fontsize": fontsize - 4}, + ) + + plt.ylabel(r"$u(y)$", fontsize=fontsize) + plt.xlabel(r"$y$", fontsize=fontsize) + ax1.tick_params(axis="x", labelsize=fontsize) + ax1.tick_params(axis="y", labelsize=fontsize) + ax1.set_xlim([-0.05, 0.05]) + ax1.set_ylim([0.0, 0.62]) + plt.savefig(osp.join(PLOT_DIR, "pipe_uProfiles.png"), bbox_inches="tight") + + # Distribution of center velocity + # Predicted result + input_dict_test = { + "x": data_2d_xy[:, 0:1], + "y": data_2d_xy[:, 1:2], + "nu": data_2d_xy[:, 2:3], + } + output_dict_test = solver.predict(input_dict_test, return_numpy=True) + u_max_pred = output_dict_test["u"] + + # Analytical result, y = 0 + u_max_a = (R ** 2) * (P_IN - P_OUT) / (2 * L * data_1d_nu * RHO) + + plt.figure(2) + plt.clf() + ax1 = plt.subplot(111) + sns.kdeplot( + u_max_a, + fill=True, + color="black", + label="Analytical", + linestyle="-", + linewidth=3, + ) + sns.kdeplot( + u_max_pred, + fill=False, + color="red", + label="DNN", + linestyle="--", + linewidth=3.5, + ) + plt.legend(prop={"size": fontsize}) + plt.xlabel(r"$u_c$", fontsize=fontsize) + plt.ylabel(r"PDF", fontsize=fontsize) + ax1.tick_params(axis="x", labelsize=fontsize) + ax1.tick_params(axis="y", labelsize=fontsize) + plt.savefig(osp.join(PLOT_DIR, "pipe_unformUQ.png"), bbox_inches="tight") + + @hydra.main(version_base=None, config_path="./conf", config_name="poiseuille_flow.yaml") def main(cfg: DictConfig): if cfg.mode == "train": train(cfg) elif cfg.mode == "eval": evaluate(cfg) + elif cfg.mode == "export": + export(cfg) + elif cfg.mode == "infer": + inference(cfg) else: - raise ValueError(f"cfg.mode should in ['train', 'eval'], but got '{cfg.mode}'") + raise ValueError(f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'") if __name__ == "__main__": - main() + main() \ No newline at end of file From 5f104f5cac984cacd37297928b54b17784467b96 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 25 Nov 2024 18:59:33 +0800 Subject: [PATCH 02/17] fix codestyle --- examples/pipe/conf/poiseuille_flow.yaml | 2 +- examples/pipe/poiseuille_flow.py | 46 +++++++++++++++++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/examples/pipe/conf/poiseuille_flow.yaml b/examples/pipe/conf/poiseuille_flow.yaml index 63f0e7dd07..80ba16c0ba 100644 --- a/examples/pipe/conf/poiseuille_flow.yaml +++ b/examples/pipe/conf/poiseuille_flow.yaml @@ -93,4 +93,4 @@ INFER: gpu_id: 0 max_batch_size: 8192 num_cpu_threads: 10 - batch_size: 8192 \ No newline at end of file + batch_size: 8192 diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index c51d2f98d3..bf74521cd3 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -431,22 +431,38 @@ def forward(self, output_dict, label_dict): def export(cfg): from paddle.static import InputSpec + model_u = ppsci.arch.MLP(**cfg.MODEL.u_net) model_v = ppsci.arch.MLP(**cfg.MODEL.v_net) model_p = ppsci.arch.MLP(**cfg.MODEL.p_net) - solver_u = ppsci.solver.Solver(model_u, pretrained_model_path=cfg.EXPORT.pretrained_model_path) - solver_v = ppsci.solver.Solver(model_v, pretrained_model_path=cfg.EXPORT.pretrained_model_path) - solver_p = ppsci.solver.Solver(model_p, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + solver_u = ppsci.solver.Solver( + model_u, pretrained_model_path=cfg.EXPORT.pretrained_model_path + ) + solver_v = ppsci.solver.Solver( + model_v, pretrained_model_path=cfg.EXPORT.pretrained_model_path + ) + solver_p = ppsci.solver.Solver( + model_p, pretrained_model_path=cfg.EXPORT.pretrained_model_path + ) input_spec_u = [ - {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.u_net.input_keys}, + { + key: InputSpec([None, 1], "float32", name=key) + for key in cfg.MODEL.u_net.input_keys + }, ] input_spec_v = [ - {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.v_net.input_keys}, + { + key: InputSpec([None, 1], "float32", name=key) + for key in cfg.MODEL.v_net.input_keys + }, ] input_spec_p = [ - {key: InputSpec([None, 1], "float32", name=key) for key in cfg.MODEL.p_net.input_keys}, + { + key: InputSpec([None, 1], "float32", name=key) + for key in cfg.MODEL.p_net.input_keys + }, ] export_path_u = os.path.join(cfg.output_dir, "u_net") @@ -507,16 +523,16 @@ def input_trans(self, input): return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} def output_trans_u(self, input, out): - return {"u": out["u"] * (R ** 2 - self.input["y"] ** 2)} + return {"u": out["u"] * (R**2 - self.input["y"] ** 2)} def output_trans_v(self, input, out): - return {"v": (R ** 2 - self.input["y"] ** 2) * out["v"]} + return {"v": (R**2 - self.input["y"] ** 2) * out["v"]} def output_trans_p(self, input, out): return { "p": ( - (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L - + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] ) } @@ -538,7 +554,7 @@ def output_trans_p(self, input, out): dP = P_IN - P_OUT for i in range(N_p): - uy = (R ** 2 - data_1d_y ** 2) * dP / (2 * L * data_1d_nu[i] * RHO) + uy = (R**2 - data_1d_y ** 2) * dP / (2 * L * data_1d_nu[i] * RHO) u_analytical[:, :, i] = np.tile(uy.reshape([N_y, 1]), N_x) label_dict = {"u": np.ones_like(input_dict["x"])} @@ -558,7 +574,7 @@ def output_trans_p(self, input, out): "y": data_2d_xy_test[:, 1:2], "nu": data_2d_xy_test[:, 2:3], } - u_max_a = (R ** 2) * dP / (2 * L * data_1d_nu_distribution * RHO) + u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) label_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} weight_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} @@ -689,8 +705,10 @@ def main(cfg: DictConfig): elif cfg.mode == "infer": inference(cfg) else: - raise ValueError(f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'") + raise ValueError( + f"cfg.mode should in ['train', 'eval', 'export', 'infer'], but got '{cfg.mode}'" + ) if __name__ == "__main__": - main() \ No newline at end of file + main() From 7d56895209cdd75c7328f70dc0e71cf2a40a1a3f Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 25 Nov 2024 19:14:42 +0800 Subject: [PATCH 03/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index bf74521cd3..14a75ab74e 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -554,7 +554,7 @@ def output_trans_p(self, input, out): dP = P_IN - P_OUT for i in range(N_p): - uy = (R**2 - data_1d_y ** 2) * dP / (2 * L * data_1d_nu[i] * RHO) + uy = (R**2 - data_1d_y**2) * dP / (2 * L * data_1d_nu[i] * RHO) u_analytical[:, :, i] = np.tile(uy.reshape([N_y, 1]), N_x) label_dict = {"u": np.ones_like(input_dict["x"])} @@ -665,7 +665,7 @@ def output_trans_p(self, input, out): u_max_pred = output_dict_test["u"] # Analytical result, y = 0 - u_max_a = (R ** 2) * (P_IN - P_OUT) / (2 * L * data_1d_nu * RHO) + u_max_a = (R**2) * (P_IN - P_OUT) / (2 * L * data_1d_nu * RHO) plt.figure(2) plt.clf() From 3605f48734c5cdfcdf69e8468d5ea84a6ed1536f Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 25 Nov 2024 19:37:53 +0800 Subject: [PATCH 04/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 14a75ab74e..1e1a367969 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -560,36 +560,12 @@ def output_trans_p(self, input, out): label_dict = {"u": np.ones_like(input_dict["x"])} weight_dict = {"u": np.ones_like(input_dict["x"])} - num_test = 500 - data_1d_nu_distribution = np.random.normal(NU_MEAN, 0.2 * NU_MEAN, num_test) - data_2d_xy_test = ( - np.array( - np.meshgrid((X_IN - X_OUT) / 2.0, 0, data_1d_nu_distribution), np.float32 - ) - .reshape(3, -1) - .T - ) - input_dict_KL = { - "x": data_2d_xy_test[:, 0:1], - "y": data_2d_xy_test[:, 1:2], - "nu": data_2d_xy_test[:, 2:3], - } - u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) - label_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} - weight_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} - dataset_vel = { "name": "NamedArrayDataset", "input": input_dict, "label": label_dict, "weight": weight_dict, } - dataset_kl = { - "name": "NamedArrayDataset", - "input": input_dict_KL, - "label": label_dict_KL, - "weight": weight_dict_KL, - } eval_cfg = { "sampler": { "name": "BatchSampler", From 3dc25c1d0e3473019931342f34c356d2cb491628 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 26 Nov 2024 15:50:20 +0800 Subject: [PATCH 05/17] fix --- examples/pipe/poiseuille_flow.py | 46 +++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 1e1a367969..19c6296c04 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -560,13 +560,37 @@ def output_trans_p(self, input, out): label_dict = {"u": np.ones_like(input_dict["x"])} weight_dict = {"u": np.ones_like(input_dict["x"])} + num_test = 500 + data_1d_nu_distribution = np.random.normal(NU_MEAN, 0.2 * NU_MEAN, num_test) + data_2d_xy_test = ( + np.array( + np.meshgrid((X_IN - X_OUT) / 2.0, 0, data_1d_nu_distribution), np.float32 + ) + .reshape(3, -1) + .T + ) + input_dict_KL = { + "x": data_2d_xy_test[:, 0:1], + "y": data_2d_xy_test[:, 1:2], + "nu": data_2d_xy_test[:, 2:3], + } + u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) + label_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} + weight_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} + dataset_vel = { "name": "NamedArrayDataset", "input": input_dict, "label": label_dict, "weight": weight_dict, } - eval_cfg = { + dataset_kl = { + "name": "NamedArrayDataset", + "input": input_dict_KL, + "label": label_dict_KL, + "weight": weight_dict_KL, + } + infer_cfg = { "sampler": { "name": "BatchSampler", "shuffle": False, @@ -574,7 +598,8 @@ def output_trans_p(self, input, out): }, "batch_size": 2000, } - eval_cfg["dataset"] = dataset_vel + infer_cfg["dataset"] = dataset_vel + infer_cfg["dataset"] = dataset_kl solver = ppsci.solver.Solver( model, @@ -582,18 +607,15 @@ def output_trans_p(self, input, out): pretrained_model_path=cfg.INFER.pretrained_model_path, ) - # inference output_dict = solver.predict(input_dict, return_numpy=True) u_pred = output_dict["u"].reshape(N_y, N_x, N_p) - - PLOT_DIR = osp.join(cfg.output_dir, "visu") - os.makedirs(PLOT_DIR, exist_ok=True) - fontsize = 16 idx_X = int(round(N_x / 2)) # pipe velocity section at L/2 nu_index = [3, 6, 9, 12, 14, 20, 49] # pick 7 nu samples ytext = [0.55, 0.5, 0.4, 0.28, 0.1, 0.05, 0.001] # text y position + PLOT_DIR = osp.join(cfg.output_dir, "visu") + os.makedirs(PLOT_DIR, exist_ok=True) plt.figure(1) plt.clf() for idxP in range(len(nu_index)): @@ -633,15 +655,13 @@ def output_trans_p(self, input, out): # Distribution of center velocity # Predicted result input_dict_test = { - "x": data_2d_xy[:, 0:1], - "y": data_2d_xy[:, 1:2], - "nu": data_2d_xy[:, 2:3], + "x": data_2d_xy_test[:, 0:1], + "y": data_2d_xy_test[:, 1:2], + "nu": data_2d_xy_test[:, 2:3], } output_dict_test = solver.predict(input_dict_test, return_numpy=True) u_max_pred = output_dict_test["u"] - - # Analytical result, y = 0 - u_max_a = (R**2) * (P_IN - P_OUT) / (2 * L * data_1d_nu * RHO) + u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) plt.figure(2) plt.clf() From 4d0f834c72bf6f74ae1ddd2305f14061779753b6 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Mon, 2 Dec 2024 22:36:37 +0800 Subject: [PATCH 06/17] Update examples/pipe/poiseuille_flow.py Co-authored-by: HydrogenSulfate <490868991@qq.com> --- examples/pipe/poiseuille_flow.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 19c6296c04..97ec3533f3 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -473,8 +473,6 @@ def export(cfg): solver_v.export(input_spec_v, export_path_v) solver_p.export(input_spec_p, export_path_p) - print(f"Inference models have been exported to {cfg.output_dir}.") - def inference(cfg: DictConfig): NU_MEAN = 0.001 From 17f004865fc5d19f01fd650617c0e7e3c254cb67 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 11:27:50 +0800 Subject: [PATCH 07/17] Update poiseuille_flow.py --- examples/pipe/poiseuille_flow.py | 77 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 36 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 19c6296c04..9492fb682a 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -430,50 +430,55 @@ def forward(self, output_dict, label_dict): def export(cfg): - from paddle.static import InputSpec - model_u = ppsci.arch.MLP(**cfg.MODEL.u_net) model_v = ppsci.arch.MLP(**cfg.MODEL.v_net) model_p = ppsci.arch.MLP(**cfg.MODEL.p_net) - solver_u = ppsci.solver.Solver( - model_u, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) - solver_v = ppsci.solver.Solver( - model_v, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) - solver_p = ppsci.solver.Solver( - model_p, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) + class Transform: + def input_trans(self, input): + self.input = input + x, y = input["x"], input["y"] + nu = input["nu"] + b = 2 * np.pi / (X_OUT - X_IN) + c = np.pi * (X_IN + X_OUT) / (X_IN - X_OUT) + sin_x = X_IN * paddle.sin(b * x + c) + cos_x = X_IN * paddle.cos(b * x + c) + return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} - input_spec_u = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.u_net.input_keys - }, - ] - input_spec_v = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.v_net.input_keys - }, - ] - input_spec_p = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.p_net.input_keys - }, - ] + def output_trans_u(self, input, out): + return {"u": out["u"] * (R**2 - self.input["y"] ** 2)} + + def output_trans_v(self, input, out): + return {"v": (R**2 - self.input["y"] ** 2) * out["v"]} - export_path_u = os.path.join(cfg.output_dir, "u_net") - export_path_v = os.path.join(cfg.output_dir, "v_net") - export_path_p = os.path.join(cfg.output_dir, "p_net") + def output_trans_p(self, input, out): + return { + "p": ( + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + ) + } - solver_u.export(input_spec_u, export_path_u) - solver_v.export(input_spec_v, export_path_v) - solver_p.export(input_spec_p, export_path_p) + transform = Transform() + model_u.register_input_transform(transform.input_trans) + model_v.register_input_transform(transform.input_trans) + model_p.register_input_transform(transform.input_trans) + model_u.register_output_transform(transform.output_trans_u) + model_v.register_output_transform(transform.output_trans_v) + model_p.register_output_transform(transform.output_trans_p) + model = ppsci.arch.ModelList((model_u, model_v, model_p)) - print(f"Inference models have been exported to {cfg.output_dir}.") + model_input_keys = ["x", "y", "nu"] + input_spec = { + key: InputSpec(shape=[None, 1], dtype="float32", name=key) for key in model_input_keys + } + + solver = ppsci.solver.Solver( + model, + pretrained_model_path=cfg.EXPORT.pretrained_model_path + ) + export_path = os.path.join(cfg.output_dir) + solver.export(input_spec, export_path) def inference(cfg: DictConfig): From 2a1c019004a194631caaacf7ad685d36726f0138 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 11:31:10 +0800 Subject: [PATCH 08/17] Update poiseuille_flow.py --- examples/pipe/poiseuille_flow.py | 72 +++++++++++++++++--------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 19c6296c04..770162ea60 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -436,44 +436,48 @@ def export(cfg): model_v = ppsci.arch.MLP(**cfg.MODEL.v_net) model_p = ppsci.arch.MLP(**cfg.MODEL.p_net) - solver_u = ppsci.solver.Solver( - model_u, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) - solver_v = ppsci.solver.Solver( - model_v, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) - solver_p = ppsci.solver.Solver( - model_p, pretrained_model_path=cfg.EXPORT.pretrained_model_path - ) + class Transform: + def input_trans(self, input): + self.input = input + x, y = input["x"], input["y"] + nu = input["nu"] + b = 2 * np.pi / (X_OUT - X_IN) + c = np.pi * (X_IN + X_OUT) / (X_IN - X_OUT) + sin_x = X_IN * paddle.sin(b * x + c) + cos_x = X_IN * paddle.cos(b * x + c) + return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} - input_spec_u = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.u_net.input_keys - }, - ] - input_spec_v = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.v_net.input_keys - }, - ] - input_spec_p = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in cfg.MODEL.p_net.input_keys - }, - ] + def output_trans_u(self, input, out): + return {"u": out["u"] * (R**2 - self.input["y"] ** 2)} + + def output_trans_v(self, input, out): + return {"v": (R**2 - self.input["y"] ** 2) * out["v"]} - export_path_u = os.path.join(cfg.output_dir, "u_net") - export_path_v = os.path.join(cfg.output_dir, "v_net") - export_path_p = os.path.join(cfg.output_dir, "p_net") + def output_trans_p(self, input, out): + return { + "p": ( + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + ) + } - solver_u.export(input_spec_u, export_path_u) - solver_v.export(input_spec_v, export_path_v) - solver_p.export(input_spec_p, export_path_p) + transform = Transform() + model_u.register_input_transform(transform.input_trans) + model_v.register_input_transform(transform.input_trans) + model_p.register_input_transform(transform.input_trans) + model_u.register_output_transform(transform.output_trans_u) + model_v.register_output_transform(transform.output_trans_v) + model_p.register_output_transform(transform.output_trans_p) + model = ppsci.arch.ModelList((model_u, model_v, model_p)) + + model_input_keys = ["x", "y", "nu"] + input_spec = { + key: InputSpec(shape=[None, 1], dtype="float32", name=key) for key in model_input_keys + } - print(f"Inference models have been exported to {cfg.output_dir}.") + solver = ppsci.solver.Solver(model, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + export_path = os.path.join(cfg.output_dir) + solver.export(input_spec, export_path) def inference(cfg: DictConfig): From d5c0e35d40e45fa27b25d9410f6b5810c209923a Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 11:34:47 +0800 Subject: [PATCH 09/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 770162ea60..be79e1038b 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -472,10 +472,13 @@ def output_trans_p(self, input, out): model_input_keys = ["x", "y", "nu"] input_spec = { - key: InputSpec(shape=[None, 1], dtype="float32", name=key) for key in model_input_keys - } + key: InputSpec(shape=[None, 1], dtype="float32", name=key) + for key in model_input_keys + } - solver = ppsci.solver.Solver(model, pretrained_model_path=cfg.EXPORT.pretrained_model_path) + solver = ppsci.solver.Solver( + model, pretrained_model_path=cfg.EXPORT.pretrained_model_path + ) export_path = os.path.join(cfg.output_dir) solver.export(input_spec, export_path) From 441fa3efa339424e5284b6f1a1cd801f1fb35769 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 13:49:39 +0800 Subject: [PATCH 10/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index be79e1038b..16c70531be 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -435,29 +435,30 @@ def export(cfg): model_u = ppsci.arch.MLP(**cfg.MODEL.u_net) model_v = ppsci.arch.MLP(**cfg.MODEL.v_net) model_p = ppsci.arch.MLP(**cfg.MODEL.p_net) + X_OUT = cfg.X_IN + cfg.L class Transform: def input_trans(self, input): self.input = input x, y = input["x"], input["y"] nu = input["nu"] - b = 2 * np.pi / (X_OUT - X_IN) - c = np.pi * (X_IN + X_OUT) / (X_IN - X_OUT) - sin_x = X_IN * paddle.sin(b * x + c) - cos_x = X_IN * paddle.cos(b * x + c) + b = 2 * np.pi / (X_OUT - cfg.X_IN) + c = np.pi * (cfg.X_IN + X_OUT) / (cfg.X_IN - X_OUT) + sin_x = cfg.X_IN * paddle.sin(b * x + c) + cos_x = cfg.X_IN * paddle.cos(b * x + c) return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} def output_trans_u(self, input, out): - return {"u": out["u"] * (R**2 - self.input["y"] ** 2)} + return {"u": out["u"] * (cfg.R**2 - self.input["y"] ** 2)} def output_trans_v(self, input, out): - return {"v": (R**2 - self.input["y"] ** 2) * out["v"]} + return {"v": (cfg.R**2 - self.input["y"] ** 2) * out["v"]} def output_trans_p(self, input, out): return { "p": ( - (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L - + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + (cfg.P_IN - cfg.P_OUT) * (X_OUT - self.input["x"]) / cfg.L + + (cfg.X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] ) } From 85b8f19d72979cdef95d9730c3ae458137521e1b Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 13:51:30 +0800 Subject: [PATCH 11/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 16c70531be..e216f171f7 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -540,7 +540,9 @@ def output_trans_p(self, input, out): return { "p": ( (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L - + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + + (X_IN - self.input["x"]) + * (X_OUT - self.input["x"]) + * out["p"] ) } From 0b4151476a80a2f485f8cd04de617d7283e4f56d Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 13:53:57 +0800 Subject: [PATCH 12/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index e216f171f7..c12dbcb75c 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -458,7 +458,9 @@ def output_trans_p(self, input, out): return { "p": ( (cfg.P_IN - cfg.P_OUT) * (X_OUT - self.input["x"]) / cfg.L - + (cfg.X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + + (cfg.X_IN - self.input["x"]) + * (X_OUT - self.input["x"]) + * out["p"] ) } @@ -539,10 +541,7 @@ def output_trans_v(self, input, out): def output_trans_p(self, input, out): return { "p": ( - (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L - + (X_IN - self.input["x"]) - * (X_OUT - self.input["x"]) - * out["p"] + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] ) } From e7ab1758014de0361970dc4d9e3fdd0a3bb39d12 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Tue, 3 Dec 2024 13:55:19 +0800 Subject: [PATCH 13/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index c12dbcb75c..d943e19900 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -541,7 +541,8 @@ def output_trans_v(self, input, out): def output_trans_p(self, input, out): return { "p": ( - (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L + + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] ) } From 888aa6749dbe4900e8a6a32736c799416ec3b7b2 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Wed, 4 Dec 2024 18:41:31 +0800 Subject: [PATCH 14/17] fix --- examples/pipe/conf/poiseuille_flow.yaml | 16 +-- examples/pipe/poiseuille_flow.py | 133 ++++++++---------------- 2 files changed, 51 insertions(+), 98 deletions(-) diff --git a/examples/pipe/conf/poiseuille_flow.yaml b/examples/pipe/conf/poiseuille_flow.yaml index 80ba16c0ba..c59d909297 100644 --- a/examples/pipe/conf/poiseuille_flow.yaml +++ b/examples/pipe/conf/poiseuille_flow.yaml @@ -26,6 +26,7 @@ hydra: mode: train # running mode: train/eval seed: 42 output_dir: ${hydra:run.dir} +log_freq: 20 # set working condition NU_MEAN: 0.001 @@ -60,6 +61,7 @@ MODEL: num_layers: 3 hidden_size: 50 activation: "swish" + output_keys: ["v", "u", "p"] # training settings TRAIN: @@ -77,20 +79,20 @@ EVAL: pretrained_model_path: null eval_with_no_grad: true -# export settings -EXPORT: - pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/poiseuille_flow_pretrained.pdparams" - # inference settings INFER: pretrained_model_path: "https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/poiseuille_flow_pretrained.pdparams" + export_path: ./inference/poiseuile_flow + pdmodel_path: ${INFER.export_path}.pdmodel + pdiparams_path: ${INFER.export_path}.pdiparams device: gpu engine: native precision: fp32 + onnx_path: ${INFER.export_path}.onnx ir_optim: true min_subgraph_size: 5 gpu_mem: 2000 gpu_id: 0 - max_batch_size: 8192 - num_cpu_threads: 10 - batch_size: 8192 + max_batch_size: 256 + num_cpu_threads: 4 + batch_size: 256 diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index d943e19900..4193a9e442 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -429,7 +429,7 @@ def forward(self, output_dict, label_dict): plt.savefig(osp.join(PLOT_DIR, "pipe_unformUQ.png"), bbox_inches="tight") -def export(cfg): +def export(cfg: DictConfig): from paddle.static import InputSpec model_u = ppsci.arch.MLP(**cfg.MODEL.u_net) @@ -458,9 +458,7 @@ def output_trans_p(self, input, out): return { "p": ( (cfg.P_IN - cfg.P_OUT) * (X_OUT - self.input["x"]) / cfg.L - + (cfg.X_IN - self.input["x"]) - * (X_OUT - self.input["x"]) - * out["p"] + + (cfg.X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] ) } @@ -473,17 +471,18 @@ def output_trans_p(self, input, out): model_p.register_output_transform(transform.output_trans_p) model = ppsci.arch.ModelList((model_u, model_v, model_p)) - model_input_keys = ["x", "y", "nu"] - input_spec = { - key: InputSpec(shape=[None, 1], dtype="float32", name=key) - for key in model_input_keys - } - solver = ppsci.solver.Solver( - model, pretrained_model_path=cfg.EXPORT.pretrained_model_path + model, + pretrained_model_path=cfg.INFER.pretrained_model_path, ) - export_path = os.path.join(cfg.output_dir) - solver.export(input_spec, export_path) + input_keys = ['x', 'y', 'nu'] + input_spec = [ + { + key: InputSpec([None, 1], "float32", name=key) + for key in input_keys + }, + ] + solver.export(input_spec, cfg.INFER.export_path) def inference(cfg: DictConfig): @@ -504,6 +503,7 @@ def inference(cfg: DictConfig): NU_START = NU_MEAN - NU_MEAN * NU_STD # 0.0001 NU_END = NU_MEAN + NU_MEAN * NU_STD # 0.1 + ## prepare data with (?, 2) data_1d_x = np.linspace( X_IN, X_OUT, N_x, endpoint=True, dtype=paddle.get_default_dtype() ) @@ -517,49 +517,17 @@ def inference(cfg: DictConfig): np.array(np.meshgrid(data_1d_x, data_1d_y, data_1d_nu)).reshape(3, -1).T ) - model_u = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("u",), 3, 50, "swish") - model_v = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("v",), 3, 50, "swish") - model_p = ppsci.arch.MLP(("sin(x)", "cos(x)", "y", "nu"), ("p",), 3, 50, "swish") - - class Transform: - def input_trans(self, input): - self.input = input - x, y = input["x"], input["y"] - nu = input["nu"] - b = 2 * np.pi / (X_OUT - X_IN) - c = np.pi * (X_IN + X_OUT) / (X_IN - X_OUT) - sin_x = X_IN * paddle.sin(b * x + c) - cos_x = X_IN * paddle.cos(b * x + c) - return {"sin(x)": sin_x, "cos(x)": cos_x, "y": y, "nu": nu} - - def output_trans_u(self, input, out): - return {"u": out["u"] * (R**2 - self.input["y"] ** 2)} - - def output_trans_v(self, input, out): - return {"v": (R**2 - self.input["y"] ** 2) * out["v"]} - - def output_trans_p(self, input, out): - return { - "p": ( - (P_IN - P_OUT) * (X_OUT - self.input["x"]) / L - + (X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] - ) - } - - transform = Transform() - model_u.register_input_transform(transform.input_trans) - model_v.register_input_transform(transform.input_trans) - model_p.register_input_transform(transform.input_trans) - model_u.register_output_transform(transform.output_trans_u) - model_v.register_output_transform(transform.output_trans_v) - model_p.register_output_transform(transform.output_trans_p) - model = ppsci.arch.ModelList((model_u, model_v, model_p)) + # Initialize your custom predictor + from deploy.python_infer import pinn_predictor + predictor = pinn_predictor.PINNPredictor(cfg) + # Prepare input data input_dict = { "x": data_2d_xy[:, 0:1], "y": data_2d_xy[:, 1:2], "nu": data_2d_xy[:, 2:3], } + u_analytical = np.zeros([N_y, N_x, N_p]) dP = P_IN - P_OUT @@ -567,9 +535,7 @@ def output_trans_p(self, input, out): uy = (R**2 - data_1d_y**2) * dP / (2 * L * data_1d_nu[i] * RHO) u_analytical[:, :, i] = np.tile(uy.reshape([N_y, 1]), N_x) - label_dict = {"u": np.ones_like(input_dict["x"])} - weight_dict = {"u": np.ones_like(input_dict["x"])} - + # Validator KL num_test = 500 data_1d_nu_distribution = np.random.normal(NU_MEAN, 0.2 * NU_MEAN, num_test) data_2d_xy_test = ( @@ -579,51 +545,22 @@ def output_trans_p(self, input, out): .reshape(3, -1) .T ) - input_dict_KL = { - "x": data_2d_xy_test[:, 0:1], - "y": data_2d_xy_test[:, 1:2], - "nu": data_2d_xy_test[:, 2:3], - } - u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) - label_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} - weight_dict_KL = {"u": np.ones_like(input_dict_KL["x"])} - dataset_vel = { - "name": "NamedArrayDataset", - "input": input_dict, - "label": label_dict, - "weight": weight_dict, - } - dataset_kl = { - "name": "NamedArrayDataset", - "input": input_dict_KL, - "label": label_dict_KL, - "weight": weight_dict_KL, + # Perform inference + output_dict = predictor.predict(input_dict, cfg.INFER.batch_size) + # mapping data to cfg.INFER.output_keys + output_dict = { + store_key: output_dict[infer_key] + for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict.keys()) } - infer_cfg = { - "sampler": { - "name": "BatchSampler", - "shuffle": False, - "drop_last": False, - }, - "batch_size": 2000, - } - infer_cfg["dataset"] = dataset_vel - infer_cfg["dataset"] = dataset_kl - - solver = ppsci.solver.Solver( - model, - output_dir=cfg.output_dir, - pretrained_model_path=cfg.INFER.pretrained_model_path, - ) - - output_dict = solver.predict(input_dict, return_numpy=True) + # Process and reshape output as needed u_pred = output_dict["u"].reshape(N_y, N_x, N_p) fontsize = 16 idx_X = int(round(N_x / 2)) # pipe velocity section at L/2 nu_index = [3, 6, 9, 12, 14, 20, 49] # pick 7 nu samples ytext = [0.55, 0.5, 0.4, 0.28, 0.1, 0.05, 0.001] # text y position + # Plot PLOT_DIR = osp.join(cfg.output_dir, "visu") os.makedirs(PLOT_DIR, exist_ok=True) plt.figure(1) @@ -663,13 +600,27 @@ def output_trans_p(self, input, out): plt.savefig(osp.join(PLOT_DIR, "pipe_uProfiles.png"), bbox_inches="tight") # Distribution of center velocity + num_test = 500 + data_1d_nu_distribution = np.random.normal(NU_MEAN, 0.2 * NU_MEAN, num_test) + data_2d_xy_test = ( + np.array( + np.meshgrid((X_IN - X_OUT) / 2.0, 0, data_1d_nu_distribution), np.float32 + ) + .reshape(3, -1) + .T + ) # Predicted result input_dict_test = { "x": data_2d_xy_test[:, 0:1], "y": data_2d_xy_test[:, 1:2], "nu": data_2d_xy_test[:, 2:3], } - output_dict_test = solver.predict(input_dict_test, return_numpy=True) + output_dict_test = predictor.predict(input_dict_test, cfg.INFER.batch_size) + # mapping data to cfg.INFER.output_keys + output_dict_test = { + store_key: output_dict_test[infer_key] + for store_key, infer_key in zip(cfg.MODEL.output_keys, output_dict_test.keys()) + } u_max_pred = output_dict_test["u"] u_max_a = (R**2) * dP / (2 * L * data_1d_nu_distribution * RHO) From f325f0b12a3ca6e2b1cdfffcd850d648352fe137 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Wed, 4 Dec 2024 18:44:50 +0800 Subject: [PATCH 15/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index 4193a9e442..d5e5e85bed 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -458,7 +458,9 @@ def output_trans_p(self, input, out): return { "p": ( (cfg.P_IN - cfg.P_OUT) * (X_OUT - self.input["x"]) / cfg.L - + (cfg.X_IN - self.input["x"]) * (X_OUT - self.input["x"]) * out["p"] + + (cfg.X_IN - self.input["x"]) + * (X_OUT - self.input["x"]) + * out["p"] ) } @@ -475,12 +477,9 @@ def output_trans_p(self, input, out): model, pretrained_model_path=cfg.INFER.pretrained_model_path, ) - input_keys = ['x', 'y', 'nu'] + input_keys = ["x", "y", "nu"] input_spec = [ - { - key: InputSpec([None, 1], "float32", name=key) - for key in input_keys - }, + {key: InputSpec([None, 1], "float32", name=key) for key in input_keys}, ] solver.export(input_spec, cfg.INFER.export_path) @@ -519,6 +518,7 @@ def inference(cfg: DictConfig): # Initialize your custom predictor from deploy.python_infer import pinn_predictor + predictor = pinn_predictor.PINNPredictor(cfg) # Prepare input data From ebb82d686d78636ec669d8f3567fd7485c33de50 Mon Sep 17 00:00:00 2001 From: Junjie Zhang <1356732652@qq.com> Date: Wed, 4 Dec 2024 18:48:58 +0800 Subject: [PATCH 16/17] fix codestyle --- examples/pipe/poiseuille_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pipe/poiseuille_flow.py b/examples/pipe/poiseuille_flow.py index d5e5e85bed..450a40f86f 100644 --- a/examples/pipe/poiseuille_flow.py +++ b/examples/pipe/poiseuille_flow.py @@ -518,7 +518,7 @@ def inference(cfg: DictConfig): # Initialize your custom predictor from deploy.python_infer import pinn_predictor - + predictor = pinn_predictor.PINNPredictor(cfg) # Prepare input data From 9bfde69dc6942b55d64be10b1811b8cd2ade3f16 Mon Sep 17 00:00:00 2001 From: HydrogenSulfate <490868991@qq.com> Date: Wed, 4 Dec 2024 19:15:55 +0800 Subject: [PATCH 17/17] Update docs/zh/examples/labelfree_DNN_surrogate.md --- docs/zh/examples/labelfree_DNN_surrogate.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/zh/examples/labelfree_DNN_surrogate.md b/docs/zh/examples/labelfree_DNN_surrogate.md index f5586b51b3..0a96abb352 100644 --- a/docs/zh/examples/labelfree_DNN_surrogate.md +++ b/docs/zh/examples/labelfree_DNN_surrogate.md @@ -34,21 +34,18 @@ python aneurysm_flow.py mode=eval EVAL.pretrained_model_path=https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/aneurysm_flow.pdparams ``` - === "模型导出命令" ``` sh python poiseuille_flow.py mode=export ``` - === "模型推理命令" ``` sh python poiseuille_flow.py mode=infer ``` - | 预训练模型 | 指标 | |:--| :--| |[aneurysm_flow.pdparams](https://paddle-org.bj.bcebos.com/paddlescience/models/LabelFree-DNN-Surrogate/aneurysm_flow.pdparams)| L-2 error u : 2.548e-4
L-2 error v : 7.169e-5 |