Skip to content
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

2982: Allow density to be blank in SLD Calculator #2986

Merged
merged 13 commits into from
Sep 10, 2024

Conversation

krzywon
Copy link
Contributor

@krzywon krzywon commented Jul 23, 2024

Description

This allows the density value to be blank in the SLD calcuator tool for cases where the density is specified in the formula.

Fixes #2982

How Has This Been Tested?

Tested locally that the result from the issue is the result when the formula and no density are entered.

Review Checklist:

[if using the editor, use [x] in place of [ ] to check a box]

Documentation (check at least one)

Installers

  • There is a chance this will affect the installers, if so
    • Windows installer (GH artifact) has been tested (installed and worked)
    • MacOSX installer (GH artifact) has been tested (installed and worked)

Licencing (untick if necessary)

  • The introduced changes comply with SasView license (BSD 3-Clause)

@krzywon krzywon requested a review from butlerpd July 23, 2024 20:40
@wpotrzebowski wpotrzebowski added SasView 6.0.0 Required for 6.0.0 release Discuss At The Call Issues to be discussed at the fortnightly call labels Jul 27, 2024
@wpotrzebowski
Copy link
Contributor

I am still getting 2.75 from 50%vol H2O@1 // D2O@1.1

@rozyczko
Copy link
Member

rozyczko commented Jul 30, 2024

Entering 50%vol H2O@1 // D2O@1.1 and removing the default density value results in the correct value being displayed.

However, clearing out the density field before the formula is entered (with only the default H2O) results in an assert:

11:20:06 - sas.qtgui.Perspectives.Fitting.FittingWidget - ERROR - Traceback (most recent call last):
  File "C:\projects\sas\sasview\src\sas\qtgui\Calculators\SldPanel.py", line 194, in recalculateSLD
    results = neutronSldAlgorithm(str(formula), density, float(neutronWavelength))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\projects\sas\sasview\src\sas\qtgui\Calculators\SldPanel.py", line 57, in neutronSldAlgorithm
    neutron_scattering(
  File "C:\Users\piotrrozyczko\.conda\envs\sasview\Lib\site-packages\periodictable\util.py", line 96, in _require_kwds
    return function(*args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\piotrrozyczko\.conda\envs\sasview\Lib\site-packages\periodictable\nsf.py", line 874, in neutron_scattering
    assert compound.density is not None, "scattering calculation needs density"
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: scattering calculation needs density

This worked properly before.

@krzywon
Copy link
Contributor Author

krzywon commented Jul 31, 2024

Check the updates I just added. This should be more robust in error catching before the information is sent for calculation.

@wpotrzebowski wpotrzebowski removed the Discuss At The Call Issues to be discussed at the fortnightly call label Aug 1, 2024
Copy link
Member

@rozyczko rozyczko left a comment

Choose a reason for hiding this comment

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

the code looks good and the fix addressed the original issue.

@wpotrzebowski
Copy link
Contributor

Ok, I didn't read the instructions properly... It works. I am wondering however how users should know about the blanking density field? I guess one option is to leave it blank by default but then we won't be able to start from the example formula. We probably can add warning or at least tooltip?

@wpotrzebowski wpotrzebowski added Discuss At The Call Issues to be discussed at the fortnightly call and removed Discuss At The Call Issues to be discussed at the fortnightly call labels Aug 12, 2024
@krzywon
Copy link
Contributor Author

krzywon commented Aug 16, 2024

I am wondering however how users should know about the blanking density field?

I think a good solution for this is, if the density is specified in the formula, we could disable and gray out the density field and ignore any value in the box. This would give a visual user cue something has happened, while keeping the calculation true to the user intent.

@krzywon krzywon linked an issue Aug 20, 2024 that may be closed by this pull request
@butlerpd
Copy link
Member

Actually, This issue will require some small changes to the documentation. I will open an issue around that.

Copy link
Member

@butlerpd butlerpd left a comment

Choose a reason for hiding this comment

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

The code changes look reasonable and already approved by @rozyczko. testing fairly thoroughly the parser seems to be doing a good job of robustly managing not only the new changes but the problem of re-computing while typing causing errors when one does not type too fast.

However, I do not believe the changes to the documentation are sufficient. Everything from Specifying Materials or Mixtures in the Molecular Formula Field down to Biomolecules should be read and edited appropriately. This could be done in a separate PR as originally suggested or done in this PR given the docs have already started to be edited in this PR. Assuming the latter, this still needs changes. However if we decouple then this can be merged IMO

@butlerpd
Copy link
Member

@krzywon it may be more expedient to open another issue after all and let me do the documentation edits? Up to you.

@krzywon
Copy link
Contributor Author

krzywon commented Aug 21, 2024

it may be more expedient to open another issue after all and let me do the documentation edits? Up to you.

Working on it now in this branch

@krzywon
Copy link
Contributor Author

krzywon commented Aug 21, 2024

@butlerpd - take another look when you get a chance. I have updated the documentation and made another fix related to the formula entry to ensure all formulas in the documentation are valid.

@butlerpd
Copy link
Member

OK @krzywon - will work on that next then

Copy link
Member

@butlerpd butlerpd left a comment

Choose a reason for hiding this comment

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

went deep in a rabbit hole on this. It is much more complicated that I realized and I see why it took @katieweigandt so long to get the docs sorted! I now see why greying out the box may not always be the desired behavior. individual densities are required for all vol% entries in order to get the total scattering length. However non-ideal mixing means it may not always give you the right materials density.

I know there was some discussion about this earlier between @wpotrzebowski and @krzywon which led to replacing the "leave blank" with "grey out." I will admit that sounded good to me too this morning, but now I think that was the wrong answer. As @katieweigandt kept emphasizing in the docs: the best is to actually measure the density of your solution if you can. It is just that this is often not easy to do. Thus I think the right answer would be to go back to the density box overriding but allowing it to be blank without it defaulting to 1.0. That is I think how it works now if we just remove the .setEnabled(False) line (but keep density = None). The error checking is much more robust now so that should work? It may require a bit more editing of the docs? At least making the point that whatever is in the density box will override anything else? There would be the earlier question by @wpotrzebowski about some better way of alerting the novice user I guess?

A couple of other points to note:

  • Entering @ , without any numbers following it, defaults to @1.0 - I will argue that is reasonable and not worth trying to capture. If the user stops there I'm not sure what they would expect other than 1?
  • Nested expressions may not grey out the density line even if the outer X components all have their densities reported. The density box always supersedes (see example in docs 20%vol (10%wt NaCl // H2O)@1.07 // D2O@1.11) - This should probably be the default behavior anyway if we accept what I said above.

@wpotrzebowski wpotrzebowski added the Discuss At The Call Issues to be discussed at the fortnightly call label Aug 27, 2024
Copy link
Member

@butlerpd butlerpd 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 good now. Looking at the code change I have not tried to do the full extensive testing again as only entries with multiple components will be affected (containing \\).

This nicely addresses the problem of not allowing for non-ideal mixing and makes the SasView SLD app completely compatible with the online version I think. Moreover the color coded and tool tip on the density override is quite a nice aid for the user. All real errors, which will raise an exception from the periodictables.py all get caught and make the offending box background yellow so not worried about any combination that is not valid.

One confusing point about periodictables.py that maybe should be noted in the documentation is that periodictables.py knows the density of the elements and will assume them if a mixture does not specify its density. But this is not related to this PR so I vote this can be merged and be called done for 6.0.0

@krzywon krzywon merged commit 79e22f3 into release_6.0.0 Sep 10, 2024
32 checks passed
@krzywon krzywon deleted the 2982-sld-calculator-density branch September 10, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discuss At The Call Issues to be discussed at the fortnightly call SasView 6.0.0 Required for 6.0.0 release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SLD calculator does not respect density in formula
4 participants