|
| 1 | +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import numpy as np |
| 16 | + |
| 17 | +import ppsci |
| 18 | +from ppsci.utils import config |
| 19 | +from ppsci.utils import logger |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + args = config.parse_args() |
| 23 | + # set random seed for reproducibility |
| 24 | + ppsci.utils.misc.set_random_seed(42) |
| 25 | + # set output directory |
| 26 | + output_dir = "./output_darcy2d" if not args.output_dir else args.output_dir |
| 27 | + # initialize logger |
| 28 | + logger.init_logger("ppsci", f"{output_dir}/train.log", "info") |
| 29 | + |
| 30 | + # set model |
| 31 | + model = ppsci.arch.MLP(("x", "y"), ("p",), 5, 20, "tanh", False, False) |
| 32 | + |
| 33 | + # set equation |
| 34 | + equation = {"Poisson": ppsci.equation.Poisson(2)} |
| 35 | + |
| 36 | + # set geometry |
| 37 | + geom = {"rect": ppsci.geometry.Rectangle((0.0, 0.0), (1.0, 1.0))} |
| 38 | + |
| 39 | + # set dataloader config |
| 40 | + ITERS_PER_EPOCH = 1 |
| 41 | + train_dataloader_cfg = { |
| 42 | + "dataset": "IterableNamedArrayDataset", |
| 43 | + "iters_per_epoch": ITERS_PER_EPOCH, |
| 44 | + } |
| 45 | + |
| 46 | + NPOINT_PDE = 99**2 |
| 47 | + NPOINT_TOP = 101 |
| 48 | + NPOINT_BOTTOM = 101 |
| 49 | + NPOINT_LEFT = 99 |
| 50 | + NPOINT_RIGHT = 99 |
| 51 | + |
| 52 | + # set constraint |
| 53 | + def poisson_ref_compute_func(_in): |
| 54 | + return ( |
| 55 | + -8.0 |
| 56 | + * (np.pi**2) |
| 57 | + * np.sin(2.0 * np.pi * _in["x"]) |
| 58 | + * np.cos(2.0 * np.pi * _in["y"]) |
| 59 | + ) |
| 60 | + |
| 61 | + pde_constraint = ppsci.constraint.InteriorConstraint( |
| 62 | + equation["Poisson"].equations, |
| 63 | + {"poisson": poisson_ref_compute_func}, |
| 64 | + geom["rect"], |
| 65 | + {**train_dataloader_cfg, "batch_size": NPOINT_PDE}, |
| 66 | + ppsci.loss.MSELoss("sum"), |
| 67 | + evenly=True, |
| 68 | + name="EQ", |
| 69 | + ) |
| 70 | + bc_top = ppsci.constraint.BoundaryConstraint( |
| 71 | + {"p": lambda out: out["p"]}, |
| 72 | + { |
| 73 | + "p": lambda _in: np.sin(2.0 * np.pi * _in["x"]) |
| 74 | + * np.cos(2.0 * np.pi * _in["y"]) |
| 75 | + }, |
| 76 | + geom["rect"], |
| 77 | + {**train_dataloader_cfg, "batch_size": NPOINT_TOP}, |
| 78 | + ppsci.loss.MSELoss("sum"), |
| 79 | + criteria=lambda x, y: np.isclose(y, 1.0), |
| 80 | + name="BC_top", |
| 81 | + ) |
| 82 | + bc_bottom = ppsci.constraint.BoundaryConstraint( |
| 83 | + {"p": lambda out: out["p"]}, |
| 84 | + { |
| 85 | + "p": lambda _in: np.sin(2.0 * np.pi * _in["x"]) |
| 86 | + * np.cos(2.0 * np.pi * _in["y"]) |
| 87 | + }, |
| 88 | + geom["rect"], |
| 89 | + {**train_dataloader_cfg, "batch_size": NPOINT_BOTTOM}, |
| 90 | + ppsci.loss.MSELoss("sum"), |
| 91 | + criteria=lambda x, y: np.isclose(y, 0.0), |
| 92 | + name="BC_bottom", |
| 93 | + ) |
| 94 | + bc_left = ppsci.constraint.BoundaryConstraint( |
| 95 | + {"p": lambda out: out["p"]}, |
| 96 | + { |
| 97 | + "p": lambda _in: np.sin(2.0 * np.pi * _in["x"]) |
| 98 | + * np.cos(2.0 * np.pi * _in["y"]) |
| 99 | + }, |
| 100 | + geom["rect"], |
| 101 | + {**train_dataloader_cfg, "batch_size": NPOINT_LEFT}, |
| 102 | + ppsci.loss.MSELoss("sum"), |
| 103 | + criteria=lambda x, y: np.isclose(x, 0.0), |
| 104 | + name="BC_left", |
| 105 | + ) |
| 106 | + bc_right = ppsci.constraint.BoundaryConstraint( |
| 107 | + {"p": lambda out: out["p"]}, |
| 108 | + { |
| 109 | + "p": lambda _in: np.sin(2.0 * np.pi * _in["x"]) |
| 110 | + * np.cos(2.0 * np.pi * _in["y"]) |
| 111 | + }, |
| 112 | + geom["rect"], |
| 113 | + {**train_dataloader_cfg, "batch_size": NPOINT_RIGHT}, |
| 114 | + ppsci.loss.MSELoss("sum"), |
| 115 | + criteria=lambda x, y: np.isclose(x, 1.0), |
| 116 | + name="BC_right", |
| 117 | + ) |
| 118 | + # wrap constraints together |
| 119 | + constraint = { |
| 120 | + pde_constraint.name: pde_constraint, |
| 121 | + bc_top.name: bc_top, |
| 122 | + bc_bottom.name: bc_bottom, |
| 123 | + bc_left.name: bc_left, |
| 124 | + bc_right.name: bc_right, |
| 125 | + } |
| 126 | + |
| 127 | + # set training hyper-parameters |
| 128 | + epochs = 10000 if not args.epochs else args.epochs |
| 129 | + |
| 130 | + # set optimizer |
| 131 | + optimizer = ppsci.optimizer.Adam(1e-3)((model,)) |
| 132 | + |
| 133 | + # set validator |
| 134 | + NPOINTS_EVAL = NPOINT_PDE |
| 135 | + residual_validator = ppsci.validate.GeometryValidator( |
| 136 | + equation["Poisson"].equations, |
| 137 | + {"poisson": poisson_ref_compute_func}, |
| 138 | + geom["rect"], |
| 139 | + { |
| 140 | + "dataset": "NamedArrayDataset", |
| 141 | + "total_size": NPOINTS_EVAL, |
| 142 | + "batch_size": 8192, |
| 143 | + "sampler": {"name": "BatchSampler"}, |
| 144 | + }, |
| 145 | + ppsci.loss.MSELoss("sum"), |
| 146 | + evenly=True, |
| 147 | + metric={"MSE": ppsci.metric.MSE()}, |
| 148 | + name="Residual", |
| 149 | + ) |
| 150 | + validator = {residual_validator.name: residual_validator} |
| 151 | + |
| 152 | + # set visualizer(optional) |
| 153 | + # manually collate input data for visualization, |
| 154 | + NPOINT_BC = NPOINT_TOP + NPOINT_BOTTOM + NPOINT_LEFT + NPOINT_RIGHT |
| 155 | + vis_points = geom["rect"].sample_interior(NPOINT_PDE + NPOINT_BC, evenly=True) |
| 156 | + visualizer = { |
| 157 | + "visulzie_u_v": ppsci.visualize.VisualizerVtu( |
| 158 | + vis_points, |
| 159 | + {"p": lambda d: d["p"]}, |
| 160 | + prefix="result_u_v", |
| 161 | + ) |
| 162 | + } |
| 163 | + |
| 164 | + # initialize solver |
| 165 | + solver = ppsci.solver.Solver( |
| 166 | + model, |
| 167 | + constraint, |
| 168 | + output_dir, |
| 169 | + optimizer, |
| 170 | + None, |
| 171 | + epochs, |
| 172 | + ITERS_PER_EPOCH, |
| 173 | + eval_during_train=True, |
| 174 | + eval_freq=200, |
| 175 | + equation=equation, |
| 176 | + geom=geom, |
| 177 | + validator=validator, |
| 178 | + visualizer=visualizer, |
| 179 | + ) |
| 180 | + # train model |
| 181 | + solver.train() |
| 182 | + # evaluate after finished training |
| 183 | + solver.eval() |
| 184 | + # visualize prediction after finished training |
| 185 | + solver.visualize() |
| 186 | + |
| 187 | + # directly evaluate pretrained model(optional) |
| 188 | + logger.init_logger("ppsci", f"{output_dir}/eval.log", "info") |
| 189 | + solver = ppsci.solver.Solver( |
| 190 | + model, |
| 191 | + constraint, |
| 192 | + output_dir, |
| 193 | + equation=equation, |
| 194 | + geom=geom, |
| 195 | + validator=validator, |
| 196 | + visualizer=visualizer, |
| 197 | + pretrained_model_path=f"{output_dir}/checkpoints/latest", |
| 198 | + ) |
| 199 | + solver.eval() |
| 200 | + # visualize prediction for pretrained model(optional) |
| 201 | + solver.visualize() |
0 commit comments