Skip to content

Allow pure python Fq for beta approximation.#570

Merged
pkienzle merged 7 commits intomasterfrom
pr-python-fq
Mar 6, 2026
Merged

Allow pure python Fq for beta approximation.#570
pkienzle merged 7 commits intomasterfrom
pr-python-fq

Conversation

@pkienzle
Copy link
Copy Markdown
Contributor

Allow pure python F(q) functions such as the following:

def Fq(q, sld, sld_solvent, radius):
    """Calculate F(q), F^2(q) for sphere"""
    qr = q * radius
    sn, cn = sin(qr), cos(qr)
    with np.errstate(all='ignore'):
        bes = 3 * (sn - qr * cn) / qr ** 3 # may be 0/0 but we fix that next line
        bes[qr == 0] = 1
    fq = bes * (1e-2 * (sld - sld_solvent) * form_volume(radius))
    return fq, fq**2
Fq.vectorized = vectorized  # Fq accepts an array of q value

Test using:

python -m sasmodels.compare -pars -midq sphere@hardsphere,_spherepy@hardsphere radius_pd=0.1 structure_factor_mode=1

@butlerpd
Copy link
Copy Markdown
Member

@yunliu01 and @smalex-z to speak with @pkienzle about this

@butlerpd butlerpd requested review from butlerpd and lucas-wilkins and removed request for smalex-z August 15, 2023 13:49
@butlerpd
Copy link
Copy Markdown
Member

Looks like it needs some unit tests.

@wpotrzebowski
Copy link
Copy Markdown
Contributor

Not a priority for 6.0 but will be good to have

Copy link
Copy Markdown
Contributor

@lucas-wilkins lucas-wilkins left a comment

Choose a reason for hiding this comment

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

This looks OK and it looks like it will do what it is supposed to do, but there does seem to be a lot of mordernisation to be done on these files. If assessed it more consistency with existing code, rather than something approximating best practice.

Comment thread sasmodels/kernelpy.py
Comment thread sasmodels/kernelpy.py
Comment thread sasmodels/modelinfo.py
#: functions by reference), then set this to false.
opencl = None # type: bool
#: True if the model is compiled with C or OpenCL
compiled = None # type: bool
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might make sense to make this a dataclass and avoid having variables initialised to values outside their type

Comment thread sasmodels/models/_spherepy.py Outdated
@lucas-wilkins
Copy link
Copy Markdown
Contributor

I think @butlerpd said he would try writing some python models to check with.

@butlerpd
Copy link
Copy Markdown
Member

yes @lucas-wilkins that bit is now on me.

Copy link
Copy Markdown
Collaborator

@krzywon krzywon left a comment

Choose a reason for hiding this comment

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

A couple of suggestions, but overall, this will be good for model writers.

Comment thread sasmodels/models/_spherepy.py Outdated
Comment thread sasmodels/kernelpy.py
@pkienzle pkienzle merged commit 459b9d4 into master Mar 6, 2026
26 checks passed
@pkienzle pkienzle deleted the pr-python-fq branch March 6, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants