Skip to content

Commit

Permalink
Fix a bug where errors could happen in chromatic drawing when realize…
Browse files Browse the repository at this point in the history
…d flux is 0
  • Loading branch information
rmjarvis committed Dec 2, 2024
1 parent 2c9266b commit d821c2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions galsim/gsobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ def _calculate_nphotons(self, n_photons, poisson_flux, max_extra_noise, rng):

# Make n_photons an integer.
iN = int(n_photons + 0.5)
g = g if iN > 0 else 1. # g=0 can cause trouble in places, so avoid it.

return iN, g

Expand Down
23 changes: 23 additions & 0 deletions tests/test_chromatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,29 @@ def test_phot(run_slow):
with assert_raises(galsim.GalSimRangeError):
obj2.drawImage(bp2, image=im5, method='phot', rng=rng, n_photons=10)

@timer
def test_low_flux_phot():
""" Check that objects with 0 realized flux don't have problems.
"""

bandpass = galsim.Bandpass("LSST_r.dat", wave_type="nm")
sed = galsim.SED('vega.txt', 'nm', 'flambda').thin(rel_err=1.e-2)
sed = sed.withFlux(1.e-5, bandpass)

base_psf = galsim.Gaussian(fwhm=0.7)
psf = galsim.ChromaticAtmosphere(
base_psf,
700,
alpha=-0.3,
zenith_angle=0 * galsim.degrees,
parallactic_angle=0 * galsim.degrees,
)

observed = galsim.Convolve(psf, sed * galsim.Exponential(half_light_radius=0.3))
rng = galsim.BaseDeviate(1234)
image = observed.drawImage(nx=53, ny=53, bandpass=bandpass, method="phot", rng=rng)
np.testing.assert_array_equal(image.array, 0.)


@timer
def test_chromatic_fiducial_wavelength():
Expand Down

0 comments on commit d821c2f

Please sign in to comment.