FNO for 2D Navier-Stokes

DownloadNotebookDownloadCodeViewSource

Overview

Computational fluid dynamics is one of the most important techniques in the field of fluid mechanics in the 21st century. The flow analysis, prediction and control can be realized by solving the governing equations of fluid mechanics by numerical method. Traditional finite element method (FEM) and finite difference method (FDM) are inefficient because of the complex simulation process (physical modeling, meshing, numerical discretization, iterative solution, etc.) and high computing costs. Therefore, it is necessary to improve the efficiency of fluid simulation with AI.

Machine learning methods provide a new paradigm for scientific computing by providing a fast solver similar to traditional methods. Classical neural networks learn mappings between finite dimensional spaces and can only learn solutions related to specific discretizations. Different from traditional neural networks, Fourier Neural Operator (FNO) is a new deep learning architecture that can learn mappings between infinite-dimensional function spaces. It directly learns mappings from arbitrary function parameters to solutions to solve a class of partial differential equations. Therefore, it has a stronger generalization capability. More information can be found in the paper, Fourier Neural Operator for Parametric Partial Differential Equations.

This tutorial describes how to solve the Navier-Stokes equation using Fourier neural operator.

Problem Description

We aim to solve two-dimensional incompressible N-S equation by learning the operator mapping from each time step to the next time step:

\[w_t \mapsto w(\cdot, t+1)\]

Technology Path

MindSpore Flow solves the problem as follows:

  1. Training Dataset Construction.

  2. Model Construction.

  3. Optimizer and Loss Function.

  4. Model Training.

Fourier Neural Operator

The following figure shows the architecture of the Fourier Neural Operator model. In the figure, \(w_0(x)\) represents the initial vorticity. The input vector is lifted to higher dimension channel space by the lifting layer. Then the mapping result is used as the input of the Fourier layer to perform nonlinear transformation of the frequency domain information. Finally, the decoding layer maps the transformation result to the final prediction result \(w_1(x)\).

The Fourier Neural Operator consists of the lifting Layer, Fourier Layers, and the decoding Layer.

Fourier Neural Operator model structure

Fourier layers: Start from input V. On top: apply the Fourier transform \(\mathcal{F}\); a linear transform R on the lower Fourier modes and filters out the higher modes; then apply the inverse Fourier transform \(\mathcal{F}^{-1}\). On the bottom: apply a local linear transform W. Finally, the Fourier Layer output vector is obtained through the activation function.

Fourier Layer structure

[1]:
import os
import time
import numpy as np

import mindspore
from mindspore import nn, ops, Tensor, jit, set_seed

The following src pacakage can be downloaded in applications/data_driven/navier_stokes_fno/src. Parameters can be modified in config.

[2]:
from mindflow.cell import FNO2D
from mindflow.common import get_warmup_cosine_annealing_lr
from mindflow.loss import RelativeRMSELoss
from mindflow.utils import load_yaml_config
from mindflow.pde import UnsteadyFlowWithLoss
from src import calculate_l2_error, create_training_dataset

set_seed(0)
np.random.seed(0)
[3]:
# set context for training: using graph mode for high performance training with GPU acceleration
mindspore.set_context(mode=mindspore.GRAPH_MODE, device_target='GPU', device_id=2)
use_ascend = mindspore.get_context(attr_key='device_target') == "Ascend"
config = load_yaml_config('navier_stokes_2d.yaml')
data_params = config["data"]
model_params = config["model"]
optimizer_params = config["optimizer"]

Training Dataset Construction

Download the training and test dataset: data_driven/navier_stokes/dataset .

In this case, training data sets and test data sets are generated according to Zongyi Li’s data set in Fourier Neural Operator for Parametric Partial Differential Equations . The settings are as follows:

The initial condition \(w_0(x)\) is generated according to periodic boundary conditions:

\[w_0 \sim \mu, \mu=\mathcal{N}\left(0,7^{3 / 2}(-\Delta+49 I)^{-2.5}\right)\]

The forcing function is defined as:

\[f(x)=0.1\left(\sin \left(2 \pi\left(x_1+x_2\right)\right)+\right.\cos(2 \pi(x_1+x_2)))\]

We use a time-step of 1e-4 for the Crank–Nicolson scheme in the data-generated process where we record the solution every t = 1 time units. All data are generated on a 256 × 256 grid and are downsampled to 64 × 64. In this case, the viscosity coefficient \(\nu=1e-5\), the number of samples in the training set is 19000, and the number of samples in the test set is 3800.

[4]:
train_dataset = create_training_dataset(data_params, input_resolution=model_params["input_resolution"], shuffle=True)
test_input = np.load(os.path.join(data_params["path"], "test/inputs.npy"))
test_label = np.load(os.path.join(data_params["path"], "test/label.npy"))
Data preparation finished

Model Construction

The network is composed of 1 lifting layer, multiple Fourier layers and 1 decoding layer:

  • The Lifting layer corresponds to the FNO2D.fc0 in the case, and maps the output data \(x\) to the high dimension;

  • Multi-layer Fourier Layer corresponds to the FNO2D.fno_seq in the case. Discrete Fourier transform is used to realize the conversion between time domain and frequency domain;

  • The Decoding layer corresponds to FNO2D.fc1 and FNO2D.fc2 in the case to obtain the final predictive value.

[5]:
model = FNO2D(in_channels=model_params["in_channels"],
              out_channels=model_params["out_channels"],
              resolution=model_params["input_resolution"],
              modes=model_params["modes"],
              channels=model_params["width"],
              depths=model_params["depth"]
              )

model_params_list = []
for k, v in model_params.items():
    model_params_list.append(f"{k}-{v}")
model_name = "_".join(model_params_list)

Optimizer and Loss Function

[6]:
steps_per_epoch = train_dataset.get_dataset_size()
lr = get_warmup_cosine_annealing_lr(lr_init=optimizer_params["initial_lr"],
                                    last_epoch=optimizer_params["train_epochs"],
                                    steps_per_epoch=steps_per_epoch,
                                    warmup_epochs=optimizer_params["warmup_epochs"])

optimizer = nn.Adam(model.trainable_params(), learning_rate=Tensor(lr))

problem = UnsteadyFlowWithLoss(model, loss_fn=RelativeRMSELoss(), data_format="NHWC")

Model Training

With MindSpore version >= 2.0.0, we can use the functional programming for training neural networks.

[7]:
def train():
    if use_ascend:
        from mindspore.amp import DynamicLossScaler, auto_mixed_precision, all_finite
        loss_scaler = DynamicLossScaler(1024, 2, 100)
        auto_mixed_precision(model, 'O3')

    def forward_fn(train_inputs, train_label):
        loss = problem.get_loss(train_inputs, train_label)
        if use_ascend:
            loss = loss_scaler.scale(loss)
        return loss

    grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=False)

    @jit
    def train_step(train_inputs, train_label):
        loss, grads = grad_fn(train_inputs, train_label)
        if use_ascend:
            loss = loss_scaler.unscale(loss)
            if all_finite(grads):
                grads = loss_scaler.unscale(grads)
                loss = ops.depend(loss, optimizer(grads))
        else:
            loss = ops.depend(loss, optimizer(grads))
        return loss

    sink_process = mindspore.data_sink(train_step, train_dataset, sink_size=1)
    summary_dir = os.path.join(config["summary_dir"], model_name)

    for cur_epoch in range(optimizer_params["train_epochs"]):
        local_time_beg = time.time()
        model.set_train()
        cur_loss = 0.0
        for _ in range(steps_per_epoch):
            cur_loss = sink_process()

        print("epoch: %s, loss is %s" % (cur_epoch + 1, cur_loss), flush=True)
        local_time_end = time.time()
        epoch_seconds = (local_time_end - local_time_beg) * 1000
        step_seconds = epoch_seconds / steps_per_epoch
        print("Train epoch time: {:5.3f} ms, per step time: {:5.3f} ms".format
              (epoch_seconds, step_seconds), flush=True)

        if (cur_epoch + 1) % config["save_checkpoint_epoches"] == 0:
            ckpt_dir = os.path.join(summary_dir, "ckpt")
            if not os.path.exists(ckpt_dir):
                os.makedirs(ckpt_dir)
            mindspore.save_checkpoint(model, os.path.join(ckpt_dir, model_params["name"]))

        if (cur_epoch + 1) % config['eval_interval'] == 0:
            calculate_l2_error(model, test_input, test_label, config["test_batch_size"])

[8]:
train()
epoch: 1, loss is 1.7631323
Train epoch time: 50405.954 ms, per step time: 50.406 ms
epoch: 2, loss is 1.9283392
Train epoch time: 36591.429 ms, per step time: 36.591 ms
epoch: 3, loss is 1.4265916
Train epoch time: 35085.079 ms, per step time: 35.085 ms
epoch: 4, loss is 1.8609437
Train epoch time: 34407.280 ms, per step time: 34.407 ms
epoch: 5, loss is 1.5222052
Train epoch time: 34596.965 ms, per step time: 34.597 ms
epoch: 6, loss is 1.3424721
Train epoch time: 33847.209 ms, per step time: 33.847 ms
epoch: 7, loss is 1.607729
Train epoch time: 33106.981 ms, per step time: 33.107 ms
epoch: 8, loss is 1.3308442
Train epoch time: 33051.339 ms, per step time: 33.051 ms
epoch: 9, loss is 1.3169765
Train epoch time: 33901.816 ms, per step time: 33.902 ms
epoch: 10, loss is 1.4149593
Train epoch time: 33908.748 ms, per step time: 33.909 ms
================================Start Evaluation================================
mean rel_rmse_error: 0.15500953359901906
=================================End Evaluation=================================
...
epoch: 141, loss is 0.777328
Train epoch time: 32549.911 ms, per step time: 32.550 ms
epoch: 142, loss is 0.7008966
Train epoch time: 32522.572 ms, per step time: 32.523 ms
epoch: 143, loss is 0.72377646
Train epoch time: 32566.685 ms, per step time: 32.567 ms
epoch: 144, loss is 0.72175145
Train epoch time: 32435.932 ms, per step time: 32.436 ms
epoch: 145, loss is 0.6235678
Train epoch time: 32463.707 ms, per step time: 32.464 ms
epoch: 146, loss is 0.9351083
Train epoch time: 32448.413 ms, per step time: 32.448 ms
epoch: 147, loss is 0.9283789
Train epoch time: 32472.401 ms, per step time: 32.472 ms
epoch: 148, loss is 0.7655642
Train epoch time: 32604.642 ms, per step time: 32.605 ms
epoch: 149, loss is 0.7233772
Train epoch time: 32649.832 ms, per step time: 32.650 ms
epoch: 150, loss is 0.86825275
Train epoch time: 32589.243 ms, per step time: 32.589 ms
================================Start Evaluation================================
mean rel_rmse_error: 0.07437102290522307
=================================End Evaluation=================================
predict total time: 15.212349653244019 s