validation_test.py 6.7 KB
Newer Older
mashun1's avatar
jax-cfd  
mashun1 committed
1
2
3
4
5
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
36
37
38
39
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Validation tests for JAX-CFD."""

import functools

from absl.testing import absltest
from absl.testing import parameterized

import jax.numpy as jnp
from jax_cfd.base import advection
from jax_cfd.base import diffusion
from jax_cfd.base import equations
from jax_cfd.base import funcutils
from jax_cfd.base import test_util
from jax_cfd.base import time_stepping
from jax_cfd.base import validation_problems


class ValidationTests(test_util.TestCase):

  @parameterized.named_parameters(
      dict(
          testcase_name='_TaylorGreen_SemiImplicitNavierStokes',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=1e-3),
          solver=functools.partial(
              equations.semi_implicit_navier_stokes,
              convect=advection.convect_linear),
          implicit_diffusion=False,
          max_courant_number=.1,
          time=10.,
          atol=1e-5),
      dict(
          testcase_name='_TaylorGreen_SemiImplicitNavierStokes_rk1',
          problem=validation_problems.TaylorGreen(
              shape=(512, 512), density=1., viscosity=1e-2),
          solver=functools.partial(
              equations.semi_implicit_navier_stokes,
              convect=advection.convect_linear,
              time_stepper=time_stepping.forward_euler,
          ),
          implicit_diffusion=False,
          max_courant_number=.1,
          time=40.,
          atol=6e-6),
      dict(
          testcase_name='_TaylorGreen_SemiImplicitNavierStokes_rk4',
          problem=validation_problems.TaylorGreen(
              shape=(512, 512), density=1., viscosity=1e-2),
          solver=functools.partial(
              equations.semi_implicit_navier_stokes,
              convect=advection.convect_linear,
              time_stepper=time_stepping.classic_rk4,
          ),
          implicit_diffusion=False,
          max_courant_number=.1,
          time=40.,
          atol=8e-7),
      dict(
          testcase_name='_TaylorGreen_ImplicitDiffusionNavierStokes_matmul',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=1e-3),
          solver=functools.partial(
              equations.implicit_diffusion_navier_stokes,
              convect=advection.convect_linear,
              diffusion_solve=functools.partial(
                  diffusion.solve_fast_diag, implementation='matmul'),
          ),
          implicit_diffusion=True,
          max_courant_number=.1,
          time=10.,
          atol=3e-5),
      dict(
          testcase_name='_TaylorGreen_ImplicitDiffusionNavierStokes_fft',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=1e-3),
          solver=functools.partial(
              equations.implicit_diffusion_navier_stokes,
              convect=advection.convect_linear,
              diffusion_solve=functools.partial(
                  diffusion.solve_fast_diag, implementation='fft'),
          ),
          implicit_diffusion=True,
          max_courant_number=.1,
          time=10.,
          atol=4e-5),
      dict(
          testcase_name='_TaylorGreen_ImplicitDiffusionNavierStokes_rfft',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=1e-3),
          solver=functools.partial(
              equations.implicit_diffusion_navier_stokes,
              convect=advection.convect_linear,
              diffusion_solve=functools.partial(
                  diffusion.solve_fast_diag, implementation='rfft'),
          ),
          implicit_diffusion=True,
          max_courant_number=.1,
          time=10.,
          atol=4e-5),
      dict(
          testcase_name='_TaylorGreen_ImplicitDiffusionNavierStokes_cg',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=1e-3),
          solver=functools.partial(
              equations.implicit_diffusion_navier_stokes,
              convect=advection.convect_linear,
              diffusion_solve=functools.partial(
                  diffusion.solve_cg, atol=1e-6, maxiter=512)),
          implicit_diffusion=True,
          max_courant_number=.1,
          time=10.,
          atol=3e-5),
      dict(
          testcase_name='_TaylorGreen_ImplicitDiffusionNavierStokes_viscous',
          problem=validation_problems.TaylorGreen(
              shape=(1024, 1024), density=1., viscosity=0.5),
          solver=functools.partial(
              equations.implicit_diffusion_navier_stokes,
              convect=advection.convect_linear),
          implicit_diffusion=True,
          max_courant_number=.5,
          time=1.0,
          atol=6e-4,
      ),
  )
  def test_accuracy(self, problem, solver, implicit_diffusion,
                    max_courant_number, time, atol):
    """Test the accuracy of `solver` on the given `problem`.

    Args:
      problem: an instance of `validation_problems.Problem`.
      solver: a callable that takes `density`, `viscosity`, `dt`, `grid`, and
        `steps`. It returns a callable that takes `velocity`,
        `pressure_correction` and `force` and returns updated versions of these
        values at the next time step.
      implicit_diffusion: whether or not the solver models diffusion implicitly.
      max_courant_number: a float used to choose the size of the time step `dt`
        according to the Courant-Friedrichs-Lewy condition. See
        https://en.wikipedia.org/wiki/Courant-Friedrichs-Lewy_condition.
      time: the amount of time to run the simulation for.
      atol: absolute error tolerance per entry.
    """
    v = problem.velocity(0.)
    dt = equations.dynamic_time_step(
        v, max_courant_number, problem.viscosity, problem.grid,
        implicit_diffusion)
    steps = int(jnp.ceil(time / dt))
    navier_stokes = solver(density=problem.density,
                           viscosity=problem.viscosity,
                           dt=dt,
                           grid=problem.grid)
    v_computed = funcutils.repeated(navier_stokes, steps)(v)
    v_analytic = problem.velocity(time)
    for u_c, u_a in zip(v_computed, v_analytic):
      self.assertAllClose(u_c, u_a, atol=atol)


if __name__ == '__main__':
  absltest.main()