lsst-sims / legacy_sims_GalSimInterface

code to seamlessly generate simulated images using GalSim based off of simulated catalogs generated by CatSim
5 stars 7 forks source link

Switch to FFT rendering for bright (saturated) objects #74

Closed rmjarvis closed 5 years ago

rmjarvis commented 5 years ago

This PR implements ImSim issue 157 to transition to using FFT rendering when the object is very bright.

It's not really efficient to do this with the full atmospheric screen PSF, but if the object is saturated, we don't really care about the central turbulent part being right. Rather, we mostly just care that the wings are more or less correct. So this implements transitioning to a Kolmogorov convolved with an OpticalPSF taking all the relevant parameters from the currently active PhaseScreenPSF. So it matches the regular PSF as much as possible.

The FFT rendering time maxes out at around 1 second (on my laptop) for any flux, and the transition seems to be around 1e6 photons. Since 1e6 photons is not typically saturated, I set it up to keep using the regular photon shooting rendering until the object would be saturated and switch over at that point.

The recommended way to use this is to set fft_sb_thresh to the saturation value, 1.e5, or possibly a bit higher to be conservative. The default value is None, which means don't ever switch.

As usual, I didn't try to run this, since I don't have an easy way to do so. But I did run some tests of the salient bits of code on my laptop to try it out. So apologies in advance if there are any stupid coding errors here.

rmjarvis commented 5 years ago

Here is a comparison of the photon shooting (left) and fft (right) for 1.e7 photons.

image

Not identical, but close enough I think for the purpose of having interesting wings interfering with the objects we really care about.

jchiang87 commented 5 years ago

Here's a comparison for a star with 2.1e8 photons (magnorm=12.5 for this star): fft_switch_comparison The image on the left has the fft_switch enabled with fft_sb_thresh=2e5, and the image on the right has it disabled using fft_sb_thresh=None. Here are profile plots of a vertical slice through the center of each image: fft_switch_profile cProfile outputs attached: fft_switch_off.prof.txt fft_switch_on.prof.txt Based on the profile info, using the fft rendering saves about ~100s, so for a magnorm=10 object (10x more photons), this is about ~1000s savings.

cwwalter commented 5 years ago

Since we are doing a real FFT I'm a bit confused by the profile difference. Is this the difference between sampling over infinite time and 30 seconds?

rmjarvis commented 5 years ago

Great! Thanks Jim!

I'm a bit confused by the profile difference.

Partly, it's that the profiles aren't actually identical. A VonKarman profile would be closer to what you get from real phase screens, but it was showing weird artifacts in the FFT rendering that I couldn't figure out. So Josh and I decided that, for this purpose, switching to Kolmogorov (which doesn't show any artifacts) was probably fine.

And additionally, this is down in the regime where both photon shooting and ffts are making approximations. They make different approximations of course, and it's not actually clear which one of them is more accurate there. If I had to guess, I'd say the FFT is probably more accurate, since this is in the part of the profile that comes from the Airy wings, which the SecondKick is supposed to emulate for photon shooting. But there are a lot of fiddly bits in that second kick approximation, which may not be completely capturing this bit perfectly. Also, the FFT shape looks more natural to me, while the photon shooting looks to have a slightly abrupt drop off where they start to diverge.

cwwalter commented 5 years ago

Thanks Mike. I was remembering the FFT vs ray-tracing studies that Josh did when he was tuning the critical cutoff, and I thought I remembered better agreement but I didn't appreciate it was actually a different function. I wonder why wasn't Josh seeing the artifacts then?

jchiang87 commented 5 years ago

Unless there are objections, I will merge this at 2pm PT today.