DanPorter / Dans_Diffraction

Reads crystallographic cif files and simulates diffraction
Apache License 2.0
45 stars 13 forks source link

Suggestion on scattering intensity calculation #3

Closed hepesu closed 5 years ago

hepesu commented 5 years ago

When deal with CIF with large HKL range(or just large HKL list), calculate all intensities may use huge amount of mermory. This could be solved by splitting the HKL array into parts, calculating for each parts then merging back.

I use code below to solve the memory problem.

HKL = xtl.Cell.all_hkl(energy_kev, xtl.Scatter._scattering_max_twotheta)
intensity = []
HKL_split = np.array_split(HKL, len(HKL) // 1000)
for _HKL in HKL_split:
     intensity += xtl.Scatter.intensity(_HKL).tolist()
DanPorter commented 5 years ago

Hi Hepesu,

Thanks for your message, apologies for taking so long to get back to you!

Yes you are correct that when the lattice parameters are large and you want to calculate the whole pattern this may eventually run into the memory size limit for arrays, and cause errors or very slow behaviour. I haven't had this problem yet but it's certainly possible.

Your solution is perfect - I hadn't actually seen np.array_split before and this appears to behave in the desired way.

I will include this functionality in the next version I upload!

For now, you can always reduce the size of the arrays significantly by reducing the max_twotheta argument, which is usually 180 but in reality most measurements don't go this high, probably 100 or 120 degrees may be fine - it depends on your use case.

hepesu commented 5 years ago

Thank you!

DanPorter commented 5 years ago

Hi Hepesu, the latest version now has your idea implemented in the function Crystal.Scatter.intensity. This is the umbrella function for all intensity calculations so should mean memory errors no longer come up. Thanks again for your great suggestion!

hepesu commented 5 years ago

That is great!:tada:

Thank you!