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

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

 

import numpy as np 

import matplotlib 

import matplotlib.axes 

 

from chemreac import ReactionDiffusion 

from chemreac.integrate import run 

from chemreac.util.plotting import ( 

    coloured_spy, plot_jacobian, plot_per_reaction_contribution, 

    plot_C_vs_t_in_bin, plot_C_vs_x, plot_C_vs_t_and_x, plot_fields, 

    plot_solver_linear_error, plot_solver_linear_excess_error, 

) 

from chemreac.util.testing import slow 

 

matplotlib.use('Agg')  # travis-ci has no DISPLAY env var. 

 

 

def _get_decay_rd(N): 

    return ReactionDiffusion(2, [[0]], [[1]], [3.14], N, D=[0.0, 0.0]) 

 

 

def _get_decay_Cref(N, y0, tout): 

    y0 = np.asarray(y0) 

    tout = tout.reshape((tout.size, 1)) 

    factor = np.exp(-(tout-tout[0])*3.14) 

    Cref = np.hstack([ 

        np.hstack((y0[i*2]*factor, y0[i*2]*(1 - factor) + y0[i*2+1])) 

        for i in range(N)]) 

    return Cref.reshape((tout.size, N, 2)) 

 

 

def test_get_decay_Cref(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    Cref = _get_decay_Cref(N, y0, tout) 

    assert np.allclose(Cref, integr.yout) 

 

 

def test_plot_jacobian(): 

    # A -> B 

    rd = _get_decay_rd(1) 

    y0 = [3.0, 1.0] 

    tout = np.linspace(0, 3.0, 7) 

    integr = run(rd, y0, tout) 

    axes = plot_jacobian(rd, integr.tout, integr.yout, [0, 1]) 

    for ax in axes: 

        assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_per_reaction_contribution(): 

    rd = _get_decay_rd(1) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0] 

    integr = run(rd, y0, tout) 

    axes = plot_per_reaction_contribution(integr, [0, 1]) 

    for ax in axes: 

        assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_C_vs_t_in_bin(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    ax = plot_C_vs_t_in_bin(rd, tout, integr.yout, 0) 

    assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_C_vs_x(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    ax = plot_C_vs_x(rd, tout, integr.yout, [0, 1], 6) 

    assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_C_vs_t_and_x(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    ax = plot_C_vs_t_and_x(rd, tout, integr.yout, 0) 

    import mpl_toolkits.mplot3d 

    assert isinstance(ax, mpl_toolkits.mplot3d.Axes3D) 

 

 

def test_plot_fields(): 

    # modulation in x means x_center 

    # A -> B # mod1 (x**2) 

    # C -> D # mod1 (x**2) 

    # E -> F # mod2 (sqrt(x)) 

    # G -> H # no modulation 

    k = np.array([3.0, 7.0, 13.0, 22.0]) 

    N = 5 

    n = 8 

    D = np.zeros(n) 

    x = np.linspace(3, 7, N+1) 

    xc = x[:-1] + np.diff(x)/2 

    fields = [ 

        [xc[i]*xc[i] for i in range(N)], 

        [xc[i]*xc[i] for i in range(N)], 

        [xc[i]**0.5 for i in range(N)] 

    ] 

    g_values = [ 

        [-k[0], k[0], 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

        [0.0, 0.0, -k[1], k[1], 0.0, 0.0, 0.0, 0.0], 

        [0.0, 0.0, 0.0, 0.0, -k[2], k[2], 0.0, 0.0], 

    ] 

    rd = ReactionDiffusion( 

        n, 

        [[6]], 

        [[7]], 

        k=k[3:], N=N, D=D, x=x, fields=fields, 

        g_values=g_values 

    ) 

    ax = plot_fields(rd, indices=[0, 2]) 

    assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_solver_linear_error(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    Cref = _get_decay_Cref(N, y0, tout) 

    ax = plot_solver_linear_error(integr, Cref) 

    assert isinstance(ax, matplotlib.axes.Axes) 

 

 

def test_plot_solver_linear_excess_error(): 

    N = 3 

    rd = _get_decay_rd(N) 

    tout = np.linspace(0, 3.0, 7) 

    y0 = [3.0, 1.0]*N 

    integr = run(rd, y0, tout) 

    Cref = _get_decay_Cref(N, y0, tout) 

    ax = plot_solver_linear_excess_error(integr, Cref) 

    assert isinstance(ax, matplotlib.axes.Axes) 

 

 

@slow 

def test_coloured_spy(): 

    from matplotlib.axes import Axes 

    A = np.arange(9).reshape((3, 3)) 

    for log in (False, True, -5): 

        ax_im, ax_cb = coloured_spy(A, log=log) 

        assert isinstance(ax_im, Axes) 

        assert isinstance(ax_cb, Axes)