Thanks everyone for the great suggestions!

I think there are multiple ways I could have got this to work, but in the end, as I suspected, cctbx does already have a ready-made function. Us human crystallographers might not have been able to find it immediately, but some assistance from ChatGPT could unearth it. All credit to Peter Zwart for acting as "prompt engineer" here. It wasn't totally straightforward, as shown by the conversation at the link below. I think it's a good example of how to use these tools though. One has to be insistent in the face of confabulation. From Peter's email:

https://chat.openai.com/share/b0a3ca0b-01ab-4469-a965-b54b4a3fed3d
which essentially tells me to look at the source code
Computational Crystallography Toolbox / Code / [r25775] /trunk/cctbx/development/random_structure.py (sourceforge.net)
go to line 344, it uses space_group_info.any_compatible_unit_cell(volume=1000)

This is what my loop looks like now, following these pointers:

for space_group_symbol in sgtbx.space_group_symbol_iterator():
    space_group = sgtbx.space_group(space_group_symbol)
    unit_cell = space_group.info().any_compatible_unit_cell(volume=1000)
    print(space_group_symbol.universal_hermann_mauguin(), unit_cell.parameters())

Admittedly the cells do not explore the full random space within the constraints, but actually for my purposes that's fine.

For what it's worth, this was Bard's attempt: https://g.co/bard/share/7340bcdde4b3

This sort of looks promising, but fails because space_group.bravais_lattice does not exist. I got disillusioned at that point, but what Peter's conversation shows is that you've just got to keep pushing. At least, that's the case right now. I wonder how long before these tools just write the whole test I wanted correctly the first time.

Cheers
-- David



On Sat, 16 Sept 2023 at 09:33, Aaron Brewster <asbrewster@lbl.gov> wrote:
Hi David, there is a constraints object that tells you which elements of the metrical matrix can be varied:

>>> from cctbx.sgtbx import space_group_info
>>> sg = space_group_info('P4').group()
>>> c = sg.adp_constraints()
>>> c.independent_indices
(1, 2)

This means elements 1 and 2 of the metrical matrix are independent and can be varied.  So you could randomly vary those elements of the metrical matrix.  Say here we pick (25,100).  Then to convert those back to a unit cell:

>>> from cctbx.uctbx import unit_cell
>>> uc = unit_cell(metrical_matrix=c.all_params(independent_params=(25,100)))
>>> print(uc)
(5, 5, 10, 90, 90, 90)

This code is part of symmetrize_reduce_enlarge in ./rstbx/symmetry/constraints/parameter_reduction.py

I would presume this works for any space group.  Is this along the lines you were looking for?
-Aaron
 

On Fri, Sep 15, 2023 at 11:59 AM David Waterman <dgwaterman@gmail.com> wrote:
Hi folks,

I'm testing some code changes and would like to iterate over every space group and for each generate a random unit cell compatible with the Bravais symmetry. I've got the iteration sorted with space_group_symbol_iterator, but I'm not sure how best to generate some compatible unit cell from the space_group object. Is this something I could do easily with cctbx?

Cheers,
-- David
_______________________________________________
cctbxbb mailing list
cctbxbb@phenix-online.org
http://phenix-online.org/mailman/listinfo/cctbxbb
_______________________________________________
cctbxbb mailing list
cctbxbb@phenix-online.org
http://phenix-online.org/mailman/listinfo/cctbxbb