Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

174

175

176

177

178

179

# -*- coding: utf-8 -*- 

# This file is part of the pyMOR project (http://www.pymor.org). 

# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler 

# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) 

 

from __future__ import absolute_import, division, print_function 

 

import numpy as np 

 

from pymor.analyticalproblems.advection import InstationaryAdvectionProblem 

from pymor.core import Unpicklable, inject_sid 

from pymor.domaindescriptions import LineDomain, RectDomain, TorusDomain, CircleDomain 

from pymor.functions import ConstantFunction, GenericFunction 

from pymor.parameters.spaces import CubicParameterSpace 

 

 

class BurgersProblem(InstationaryAdvectionProblem, Unpicklable): 

    '''One-dimensional Burgers-type problem. 

 

    The problem is to solve :: 

 

        ∂_t u(x, t, μ)  +  ∂_x (v * u(x, t, μ)^μ) = 0 

                                       u(x, 0, μ) = u_0(x) 

 

    for u with t in [0, 0.3], x in [0, 2]. 

 

    Parameters 

    ---------- 

    v 

        The velocity v. 

    circle 

        If `True` impose periodic boundary conditions. Otherwise Dirichlet left, 

        outflow right. 

    initial_data_type 

        Type of initial data (`'sin'` or `'bump'`). 

    parameter_range 

        The interval in which μ is allowed to vary. 

    ''' 

 

    def __init__(self, v=1., circle=True, initial_data_type='sin', parameter_range=(1., 2.)): 

 

        assert initial_data_type in ('sin', 'bump') 

 

        def burgers_flux(U, mu): 

            U_exp = np.sign(U) * np.power(np.abs(U), mu['exponent']) 

            R = U_exp * v 

            return R 

        inject_sid(burgers_flux, str(BurgersProblem) + '.burgers_flux', v) 

 

        def burgers_flux_derivative(U, mu): 

            U_exp = mu['exponent'] * (np.sign(U) * np.power(np.abs(U), mu['exponent']-1)) 

            R = U_exp * v 

            return R 

        inject_sid(burgers_flux_derivative, str(BurgersProblem) + '.burgers_flux_derivative', v) 

 

        flux_function = GenericFunction(burgers_flux, dim_domain=1, shape_range=(1,), 

                                        parameter_type={'exponent': 0}, 

                                        name='burgers_flux') 

 

        flux_function_derivative = GenericFunction(burgers_flux_derivative, dim_domain=1, shape_range=(1,), 

                                                   parameter_type={'exponent': 0}, 

                                                   name='burgers_flux') 

 

        if initial_data_type == 'sin': 

            def initial_data(x): 

                return 0.5 * (np.sin(2 * np.pi * x[..., 0]) + 1.) 

            inject_sid(initial_data, str(BurgersProblem) + '.initial_data_sin') 

            dirichlet_data = ConstantFunction(dim_domain=1, value=0.5) 

        else: 

            def initial_data(x): 

                return (x[..., 0] >= 0.5) * (x[..., 0] <= 1) * 1 

            inject_sid(initial_data, str(BurgersProblem) + '.initial_data_bump') 

            dirichlet_data = ConstantFunction(dim_domain=1, value=0) 

 

        initial_data = GenericFunction(initial_data, dim_domain=1) 

 

        if circle: 

            domain = CircleDomain([0, 2]) 

        else: 

            domain = LineDomain([0, 2], right=None) 

 

        super(BurgersProblem, self).__init__(domain=domain, 

                                             rhs=None, 

                                             flux_function=flux_function, 

                                             flux_function_derivative=flux_function_derivative, 

                                             initial_data=initial_data, 

                                             dirichlet_data=dirichlet_data, 

                                             T=0.3, name='BurgersProblem') 

 

        self.parameter_space = CubicParameterSpace({'exponent': 0}, *parameter_range) 

        self.parameter_range = parameter_range 

        self.initial_data_type = initial_data_type 

        self.circle = circle 

        self.v = v 

 

 

class Burgers2DProblem(InstationaryAdvectionProblem, Unpicklable): 

    '''Two-dimensional Burgers-type problem. 

 

    The problem is to solve :: 

 

        ∂_t u(x, t, μ)  +  ∇ ⋅ (v * u(x, t, μ)^μ) = 0 

                                       u(x, 0, μ) = u_0(x) 

 

    for u with t in [0, 0.3], x in [0, 2] x [0, 1]. 

 

    Parameters 

    ---------- 

    vx 

        The x component of the velocity vector v. 

    vy 

        The y component of the velocity vector v. 

    torus 

        If `True` impose periodic boundary conditions. Otherwise Dirichlet left and bottom, 

        outflow top and right. 

    initial_data_type 

        Type of initial data (`'sin'` or `'bump'`). 

    parameter_range 

        The interval in which μ is allowed to vary. 

    ''' 

    def __init__(self, vx=1., vy=1., torus=True, initial_data_type='sin', parameter_range=(1., 2.)): 

 

        assert initial_data_type in ('sin', 'bump') 

 

        def burgers_flux(U, mu): 

            U = U.reshape(U.shape[:-1]) 

            U_exp = np.sign(U) * np.power(np.abs(U), mu['exponent']) 

            R = np.empty(U.shape + (2,)) 

            R[..., 0] = U_exp * vx 

            R[..., 1] = U_exp * vy 

            return R 

        inject_sid(burgers_flux, str(Burgers2DProblem) + '.burgers_flux', vx, vy) 

 

        def burgers_flux_derivative(U, mu): 

            U = U.reshape(U.shape[:-1]) 

            U_exp = mu['exponent'] * (np.sign(U) * np.power(np.abs(U), mu['exponent']-1)) 

            R = np.empty(U.shape + (2,)) 

            R[..., 0] = U_exp * vx 

            R[..., 1] = U_exp * vy 

            return R 

        inject_sid(burgers_flux_derivative, str(Burgers2DProblem) + '.burgers_flux_derivative', vx, vy) 

 

        flux_function = GenericFunction(burgers_flux, dim_domain=1, shape_range=(2,), 

                                        parameter_type={'exponent': 0}, 

                                        name='burgers_flux') 

 

        flux_function_derivative = GenericFunction(burgers_flux_derivative, dim_domain=1, shape_range=(2,), 

                                                   parameter_type={'exponent': 0}, 

                                                   name='burgers_flux') 

 

        if initial_data_type == 'sin': 

            def initial_data(x): 

                return 0.5 * (np.sin(2 * np.pi * x[..., 0]) * np.sin(2 * np.pi * x[..., 1]) + 1.) 

            inject_sid(initial_data, str(Burgers2DProblem) + '.initial_data_sin') 

            dirichlet_data = ConstantFunction(dim_domain=2, value=0.5) 

        else: 

            def initial_data(x): 

                return (x[..., 0] >= 0.5) * (x[..., 0] <= 1) * 1 

            inject_sid(initial_data, str(Burgers2DProblem) + '.initial_data_bump') 

            dirichlet_data = ConstantFunction(dim_domain=2, value=0) 

 

        initial_data = GenericFunction(initial_data, dim_domain=2) 

 

        domain = TorusDomain([[0, 0], [2, 1]]) if torus else RectDomain([[0, 0], [2, 1]], right=None, top=None) 

 

        super(Burgers2DProblem, self).__init__(domain=domain, 

                                               rhs=None, 

                                               flux_function=flux_function, 

                                               flux_function_derivative=flux_function_derivative, 

                                               initial_data=initial_data, 

                                               dirichlet_data=dirichlet_data, 

                                               T=0.3, name='Burgers2DProblem') 

 

        self.parameter_space = CubicParameterSpace({'exponent': 0}, *parameter_range) 

        self.parameter_range = parameter_range 

        self.initial_data_type = initial_data_type 

        self.torus = torus 

        self.vx = vx 

        self.vy = vy