|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import discmodel |
| 4 | + |
| 5 | +def test_discmodel_initialization(): |
| 6 | + """Test initialization of DiscGalaxy class.""" |
| 7 | + N = 1000 |
| 8 | + a = 3.0 |
| 9 | + M = 1.0 |
| 10 | + vcirc = 200.0 |
| 11 | + rmax = 30.0 |
| 12 | + |
| 13 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 14 | + |
| 15 | + assert disc.N == N |
| 16 | + assert disc.a == a |
| 17 | + assert disc.M == M |
| 18 | + assert disc.vcirc == vcirc |
| 19 | + assert disc.rmax == rmax * a |
| 20 | + |
| 21 | + assert len(disc.x) == N |
| 22 | + assert len(disc.y) == N |
| 23 | + assert len(disc.z) == N |
| 24 | + assert len(disc.u) == N |
| 25 | + assert len(disc.v) == N |
| 26 | + assert len(disc.w) == N |
| 27 | + |
| 28 | + # Check that mass is correctly assigned |
| 29 | + expected_mass = M / N |
| 30 | + assert np.all(disc.mass == expected_mass) |
| 31 | + |
| 32 | +def test_discmodel_rotation(): |
| 33 | + """Test the rotation angles in DiscModel.""" |
| 34 | + N = 1000 |
| 35 | + a = 3.0 |
| 36 | + M = 1.0 |
| 37 | + vcirc = 200.0 |
| 38 | + rmax = 30.0 |
| 39 | + |
| 40 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 41 | + |
| 42 | + R1 = disc.make_rotation_matrix(45.0,0.,30.,False) |
| 43 | + # check R1 is 3x3 |
| 44 | + assert R1.shape == (3,3) |
| 45 | + |
| 46 | + R2 = disc.make_rotation_matrix(45.0,0.,30.,True) |
| 47 | + # check R2 is 3x3 |
| 48 | + assert R2.shape == (3,3) |
| 49 | + |
| 50 | + # check R1 and R2 are different |
| 51 | + assert not np.allclose(R1,R2) |
| 52 | + |
| 53 | + # apply the rotation |
| 54 | + disc.rotate_disc(45.0,0.,30.,False) |
| 55 | + # check lengths are unchanged |
| 56 | + assert len(disc.x) == N |
| 57 | + assert len(disc.y) == N |
| 58 | + assert len(disc.z) == N |
| 59 | + |
| 60 | + disc.rotate_disc(45.0,0.,30.,True) |
| 61 | + # check lengths are unchanged |
| 62 | + assert len(disc.x) == N |
| 63 | + assert len(disc.y) == N |
| 64 | + assert len(disc.z) == N |
| 65 | + |
| 66 | + # try special case of N=1 |
| 67 | + N = 1 |
| 68 | + |
| 69 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 70 | + |
| 71 | + # apply the rotation |
| 72 | + disc.rotate_disc(45.0,0.,30.,False) |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +def test_discmodel_phasespace_input(): |
| 77 | + """Test initialization of DiscGalaxy class with phasespace input.""" |
| 78 | + N = 1000 |
| 79 | + a = 3.0 |
| 80 | + M = 1.0 |
| 81 | + vcirc = 200.0 |
| 82 | + rmax = 30.0 |
| 83 | + |
| 84 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 85 | + |
| 86 | + newdisc = discmodel.DiscGalaxy(phasespace=(disc.x,disc.y,disc.z,disc.u,disc.v,disc.w)) |
| 87 | + |
| 88 | + |
| 89 | +def test_discmodel_version(): |
| 90 | + """Test that the version string is correctly set.""" |
| 91 | + import discmodel |
| 92 | + assert isinstance(discmodel.__version__, str) |
| 93 | + |
| 94 | +def test_discmodel_image(): |
| 95 | + N = 1000 |
| 96 | + a = 3.0 |
| 97 | + M = 1.0 |
| 98 | + vcirc = 200.0 |
| 99 | + rmax = 30.0 |
| 100 | + noiselevel = -100.0 |
| 101 | + nbins = 50 |
| 102 | + |
| 103 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 104 | + disc.generate_image(rmax,nbins,noiselevel=noiselevel) |
| 105 | + |
| 106 | + # add some noise |
| 107 | + noiselevel = 0.1 |
| 108 | + disc.generate_image(rmax,nbins,noiselevel=noiselevel) |
| 109 | + |
| 110 | + # check that r and p are set |
| 111 | + assert hasattr(disc,'r') |
| 112 | + assert hasattr(disc,'p') |
| 113 | + |
| 114 | + |
| 115 | +def test_discmodel_expansion(): |
| 116 | + N = 1000 |
| 117 | + a = 3.0 |
| 118 | + M = 1.0 |
| 119 | + vcirc = 200.0 |
| 120 | + rmax = 30.0 |
| 121 | + noiselevel = -100.0 |
| 122 | + nbins = 50 |
| 123 | + |
| 124 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 125 | + disc.generate_image(rmax,nbins,noiselevel=noiselevel) |
| 126 | + |
| 127 | + # add some noise |
| 128 | + noiselevel = 0.1 |
| 129 | + disc.generate_image(rmax,nbins,noiselevel=noiselevel) |
| 130 | + |
| 131 | + |
| 132 | + # compute the expansion |
| 133 | + E1 = disc.make_expansion(mmax=4,nmax=4,rscl=1.0,xmax=rmax,noisy=False) |
| 134 | + |
| 135 | + # compute the expansion from the particles |
| 136 | + E2 = disc.make_particle_expansion(mmax=4,nmax=4,rscl=1.0) |
| 137 | + |
| 138 | + # compute A1 |
| 139 | + a1 = disc.compute_a1(E1) |
| 140 | + |
| 141 | + # check it is a float |
| 142 | + assert isinstance(a1,float) |
| 143 | + |
| 144 | + |
| 145 | +def test_discmodel_resampling(): |
| 146 | + N = 1000 |
| 147 | + a = 3.0 |
| 148 | + M = 1.0 |
| 149 | + vcirc = 200.0 |
| 150 | + rmax = 30.0 |
| 151 | + noiselevel = -100.0 |
| 152 | + nbins = 50 |
| 153 | + |
| 154 | + disc = discmodel.DiscGalaxy(N=N, a=a, M=M, vcirc=vcirc, rmax=rmax) |
| 155 | + disc.generate_image(rmax,nbins,noiselevel=noiselevel) |
| 156 | + |
| 157 | + # compute the expansion |
| 158 | + E1 = disc.make_expansion(mmax=4,nmax=4,rscl=1.0,xmax=rmax,noisy=False) |
| 159 | + |
| 160 | + newdisc = disc.resample_expansion(E1) |
| 161 | + |
| 162 | + # check newdisc is Nx2 (sampled from 2d image only) |
| 163 | + assert newdisc.shape == (N, 2) |
0 commit comments