pde_dataset.py 5.43 KB
Newer Older
Boris Bonev's avatar
Boris Bonev committed
1
2
3
4
# coding=utf-8

# SPDX-FileCopyrightText: Copyright (c) 2022 The torch-harmonics Authors. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
5
#
Boris Bonev's avatar
Boris Bonev committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import torch

from math import ceil

Boris Bonev's avatar
Boris Bonev committed
36
from .shallow_water_equations import ShallowWaterSolver
Boris Bonev's avatar
Boris Bonev committed
37

38

Boris Bonev's avatar
Boris Bonev committed
39
class PdeDataset(torch.utils.data.Dataset):
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    """Custom Dataset class for PDE training data

    Parameters
    ----------
    dt : float
        Time step
    nsteps : int
        Number of solver steps
    dims : tuple, optional
        Number of latitude and longitude points, by default (384, 768)
    grid : str, optional
        Grid type, by default "equiangular"
    pde : str, optional
        PDE type, by default "shallow water equations"
    initial_condition : str, optional
        Initial condition type, by default "random"
    num_examples : int, optional
        Number of examples, by default 32
    device : torch.device, optional
        Device to use, by default torch.device("cpu")
    normalize : bool, optional
        Whether to normalize the input and target, by default True
    stream : torch.cuda.Stream, optional
        CUDA stream to use, by default None

    Returns
    -------
    inp : torch.Tensor
        Input tensor
    tar : torch.Tensor
        Target tensor
    """
72
73
74
75
76
77
78
79
80
81
82
83
84
85

    def __init__(
        self,
        dt,
        nsteps,
        dims=(384, 768),
        grid="equiangular",
        pde="shallow water equations",
        initial_condition="random",
        num_examples=32,
        device=torch.device("cpu"),
        normalize=True,
        stream=None,
    ):
Boris Bonev's avatar
Boris Bonev committed
86
87
88
89
90
91
92
93
94
95
96
        self.num_examples = num_examples
        self.device = device
        self.stream = stream

        self.nlat = dims[0]
        self.nlon = dims[1]

        # number of solver steps used to compute the target
        self.nsteps = nsteps
        self.normalize = normalize

97
98
        if pde == "shallow water equations":
            lmax = ceil(self.nlat / 3)
Boris Bonev's avatar
Boris Bonev committed
99
100
            mmax = lmax
            dt_solver = dt / float(self.nsteps)
101
            self.solver = ShallowWaterSolver(self.nlat, self.nlon, dt_solver, lmax=lmax, mmax=mmax, grid=grid).to(self.device).float()
Boris Bonev's avatar
Boris Bonev committed
102
103
104
105
106
107
108
109
110
111
112
        else:
            raise NotImplementedError

        self.set_initial_condition(ictype=initial_condition)

        if self.normalize:
            inp0, _ = self._get_sample()
            self.inp_mean = torch.mean(inp0, dim=(-1, -2)).reshape(-1, 1, 1)
            self.inp_var = torch.var(inp0, dim=(-1, -2)).reshape(-1, 1, 1)

    def __len__(self):
113
        length = self.num_examples if self.ictype == "random" else 1
Boris Bonev's avatar
Boris Bonev committed
114
115
        return length

116
    def set_initial_condition(self, ictype="random"):
Boris Bonev's avatar
Boris Bonev committed
117
        self.ictype = ictype
118

Boris Bonev's avatar
Boris Bonev committed
119
120
121
122
    def set_num_examples(self, num_examples=32):
        self.num_examples = num_examples

    def _get_sample(self):
123
        if self.ictype == "random":
Boris Bonev's avatar
Boris Bonev committed
124
            inp = self.solver.random_initial_condition(mach=0.2)
125
        elif self.ictype == "galewsky":
Boris Bonev's avatar
Boris Bonev committed
126
            inp = self.solver.galewsky_initial_condition()
127

Boris Bonev's avatar
Boris Bonev committed
128
129
130
        # solve pde for n steps to return the target
        tar = self.solver.timestep(inp, self.nsteps)
        inp = self.solver.spec2grid(inp)
131
        tar = self.solver.spec2grid(tar)
Boris Bonev's avatar
Boris Bonev committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

        return inp, tar

    def __getitem__(self, index):

        # if self.stream is None:
        #     self.stream = torch.cuda.Stream()

        # with torch.cuda.stream(self.stream):
        #     with torch.inference_mode():
        #         with torch.no_grad():
        #             inp, tar = self._get_sample()

        #             if self.normalize:
        #                 inp = (inp - self.inp_mean) / torch.sqrt(self.inp_var)
        #                 tar = (tar - self.inp_mean) / torch.sqrt(self.inp_var)

        # self.stream.synchronize()

        with torch.inference_mode():
            with torch.no_grad():
                inp, tar = self._get_sample()

                if self.normalize:
                    inp = (inp - self.inp_mean) / torch.sqrt(self.inp_var)
                    tar = (tar - self.inp_mean) / torch.sqrt(self.inp_var)

        return inp.clone(), tar.clone()