Skip to content

generate lookup table for space groups #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

elindgren
Copy link
Collaborator

@elindgren elindgren commented Apr 14, 2025

Fixes #26

TODO

  • Add unit test to ensure that keys always matches it's corresponding values

@elindgren
Copy link
Collaborator Author

I wrote a quick profiling script that just creates a SampleModel and applies the symmetry constraints. This operation takes from 0.1s -> 0.007s. It seems like the next biggest bottleneck is the call to sympify.

Profiling results on develop

image

Profiling results on the feature branch

image

Profiling script

from easydiffraction import SampleModel
import cProfile
import pstats

def main():
    model = SampleModel('hs')
    model.space_group.name_h_m = 'R -3 m'
    model.space_group.it_coordinate_system_code = 'h'
    model.cell.length_a = 6.9
    model.cell.length_c = 14.1
    model.atom_sites.add('Zn', 'Zn', 0, 0, 0.5, wyckoff_letter='b', b_iso=0.5)
    model.atom_sites.add('Cu', 'Cu', 0.5, 0, 0, wyckoff_letter='e', b_iso=0.5)
    model.atom_sites.add('O', 'O', 0.21, -0.21, 0.06, wyckoff_letter='h', b_iso=0.5)
    model.atom_sites.add('Cl', 'Cl', 0, 0, 0.197, wyckoff_letter='c', b_iso=0.5)
    model.atom_sites.add('H', '2H', 0.13, -0.13, 0.08, wyckoff_letter='h', b_iso=0.5)
    model.apply_symmetry_constraints()

if __name__ == "__main__":
    with cProfile.Profile() as pr:
        main()
    stats = pstats.Stats(pr)
    stats.dump_stats("profile_output.prof")

@elindgren
Copy link
Collaborator Author

I also benchmarked a more thorough example, which actually performs a fit. The tabulated values helps a lot, the fit takes 42s on develop but only ~8s on the feature branch. On develop, a whopping 35s is taken up by cryspy computing the symmetry operations. On the feature branch, symmetry operations still take up 2s of the runtime, because of sympy.

Profiling results on develop

image

Profiling results on the feature branch

image

Benchmarking script

import cProfile
import pstats
from easydiffraction import (
    Project,
    SampleModel,
    Experiment
)

def prebench():
    # Create and configure sample model
    model = SampleModel('hs')
    model.space_group.name_h_m = 'R -3 m'
    model.space_group.it_coordinate_system_code = 'h'
    model.cell.length_a = 6.9
    model.cell.length_c = 14.1
    model.atom_sites.add('Zn', 'Zn', 0, 0, 0.5, wyckoff_letter='b', b_iso=0.5)
    model.atom_sites.add('Cu', 'Cu', 0.5, 0, 0, wyckoff_letter='e', b_iso=0.5)
    model.atom_sites.add('O', 'O', 0.21, -0.21, 0.06, wyckoff_letter='h', b_iso=0.5)
    model.atom_sites.add('Cl', 'Cl', 0, 0, 0.197, wyckoff_letter='c', b_iso=0.5)
    model.atom_sites.add('H', '2H', 0.13, -0.13, 0.08, wyckoff_letter='h', b_iso=0.5)
    model.show_as_cif()
    model.apply_symmetry_constraints()
    model.show_as_cif()

    # Create and configure experiment
    expt = Experiment('hrpt', data_path='examples/data/hrpt_hs.xye')
    expt.instrument.setup_wavelength = 1.89
    expt.instrument.calib_twotheta_offset = 0.0
    expt.peak.broad_gauss_u = 0.1
    expt.peak.broad_gauss_v = -0.2
    expt.peak.broad_gauss_w = 0.2
    expt.peak.broad_lorentz_x = 0.0
    expt.peak.broad_lorentz_y = 0
    expt.linked_phases.add('hs', scale=0.5)
    expt.background.add(x=4.4196, y=500)
    expt.background.add(x=6.6207, y=500)
    expt.background.add(x=10.4918, y=500)
    expt.background.add(x=15.4634, y=500)
    expt.background.add(x=45.6041, y=500)
    expt.background.add(x=74.6844, y=500)
    expt.background.add(x=103.4187, y=500)
    expt.background.add(x=121.6311, y=500)
    expt.background.add(x=159.4116, y=500)
    expt.show_as_cif()

    # Create project and add sample model and experiments
    project = Project()
    project.sample_models.add(model)
    project.experiments.add(expt)

    # Set calculator, minimizer and fit mode
    project.analysis.current_calculator = 'cryspy'
    project.analysis.current_minimizer = 'lmfit (leastsq)'

    # Compare measured and calculated patterns
    project.analysis.show_meas_vs_calc_chart('hrpt', 48, 51)

    # ------------ 1st fitting ------------

    # Define free parameters
    model.cell.length_a.free = True
    model.cell.length_c.free = True
    expt.linked_phases['hs'].scale.free = True
    expt.instrument.calib_twotheta_offset.free = True
    project.analysis.show_free_params()
    return project

def main(project):
    # Start fitting
    project.analysis.fit()

if __name__ == "__main__":
    project = prebench()
    with cProfile.Profile() as pr:
        main(project)
    stats = pstats.Stats(pr)
    stats.dump_stats("profile_output.prof")

@elindgren elindgren linked an issue Apr 15, 2025 that may be closed by this pull request
@elindgren elindgren changed the title DRAFT: generate lookup table for space groups generate lookup table for space groups Apr 15, 2025
@elindgren elindgren changed the title generate lookup table for space groups draft: generate lookup table for space groups Apr 15, 2025
@elindgren elindgren changed the title draft: generate lookup table for space groups generate lookup table for space groups Apr 16, 2025
@elindgren
Copy link
Collaborator Author

@AndrewSazonov this is ready to go

@elindgren elindgren requested a review from AndrewSazonov April 16, 2025 13:29
Copy link
Member

@AndrewSazonov AndrewSazonov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@elindgren elindgren merged commit 94941c1 into develop Apr 23, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Performance of Symmetry Constraints via Lookup Table
3 participants