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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/sas/qtgui/Calculators/SldPanel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# global
import numpy as np
import logging
from pyparsing.exceptions import ParseException
from PySide6 import QtCore
from PySide6 import QtGui
from PySide6 import QtWidgets
Expand Down Expand Up @@ -179,19 +180,36 @@ def dataChanged(self, top, bottom):
self.recalculateSLD()

def recalculateSLD(self):
self.ui.editMolecularFormula.setStyleSheet("background-color: white")
self.ui.editMassDensity.setStyleSheet("background-color: white; color: black")
formula = self.ui.editMolecularFormula.text()
density = self.ui.editMassDensity.text()
density = float(self.ui.editMassDensity.text()) if self.ui.editMassDensity.text() else None
self.ui.editMassDensity.setToolTip("The density can either be specified here or will be calculated if all "
"component densities are included in the formula.")
neutronWavelength = self.ui.editNeutronWavelength.text()
xrayWavelength = self.ui.editXrayWavelength.text()

if not formula or not density:
if not formula:
return
if not density and '@' not in formula:
self.ui.editMassDensity.setStyleSheet("background-color: yellow")
return
if density and '//' in formula and '@' in formula:
# Ignore density input when all individual densities are specified
self.ui.editMassDensity.setStyleSheet("color: orange")
self.ui.editMassDensity.setToolTip("The input density is overriding the density calculated from the "
"individual components. Clear the density field if you want the "
"calculation to take precedence.")

def format(value):
return ("%-5.3g" % value).strip()

if neutronWavelength and float(neutronWavelength) > np.finfo(float).eps:
results = neutronSldAlgorithm(str(formula), float(density), float(neutronWavelength))
try:
results = neutronSldAlgorithm(str(formula), density, float(neutronWavelength))
except (ValueError, ParseException, AssertionError, KeyError):
self.ui.editMolecularFormula.setStyleSheet("background-color: yellow")
return

self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real))
self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag))
Expand All @@ -217,7 +235,11 @@ def format(value):
self.ui.editNeutronAbsXs.setEnabled(False)

if xrayWavelength and float(xrayWavelength) > np.finfo(float).eps:
results = xraySldAlgorithm(str(formula), float(density), float(xrayWavelength))
try:
results = xraySldAlgorithm(str(formula), density, float(xrayWavelength))
except (ValueError, ParseException, AssertionError, KeyError):
self.ui.editMolecularFormula.setStyleSheet("background-color: yellow")
return

self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real))
self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag))
Expand Down
8 changes: 4 additions & 4 deletions src/sas/qtgui/Calculators/media/sld_calculator_help.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ User Inputs
This field defines the material for which you are calculating the SLD. The section "`Specifying Materials or Mixtures in the Molecular Formula Field`_" offers further guidance on how to enter molecules, biomolecules, and more complex mixtures.

**Mass Density** (|g/cm^3|)
This field defines the density of the material for which you are calculating the SLD. Density uncertainty is likely the largest source of error in the SLD calculator.
This field defines the density of the material for which you are calculating the SLD. Density uncertainty is likely the largest source of error in the SLD calculator. This field may be excluded in cases where the mass density of each individual component is provided in the formula field.

**Neutron Wavelength** (|Ang|)
Wavelength is used to calculate the neutron scattering cross-section and 1/e length. It is required for neutron calculations and entered in units of |Ang|.
Expand Down Expand Up @@ -73,13 +73,13 @@ Specifying Materials or Mixtures in the Molecular Formula Field

*For isotopic substitution you must also adjust the density for your mixture.* There is a density calculator in SASview that may be useful, but measurement with a density meter is recommended.

**Mass Fraction** can be entered with each component of the mixture written as XX%wt "component" and each component separated with //. The mass fraction of the last component does not need to be specified as the sum of the fractions must add to 100. The mass density field must be updated to specify the density of the mixture. For example:
**Mass Fraction** can be entered with each component of the mixture written as XX%wt *or* XXwt% "component" and each component separated with //. The mass fraction of the last component does not need to be specified as the sum of the fractions must add to 100. The mass density field must be updated to specify the density of the mixture. For example:

50%wt Co // Ti

1%wtNaCl // 50%wtD2O // H2O

**Volume Fraction** can be entered in the same way by substituting *%vol* for *%wt*. The density of each component must be specified using an @density notation, where density is in units of |g/cm^3|. The mass density field must also be updated to specify the density of the mixture. (Note: vol% and wt% will lead to syntax errors).
**Volume Fraction** can be entered in the same way by substituting *%vol or vol%* for *%wt*. The density of each component must be specified using an @density notation, where density is in units of |g/cm^3|. The mass density field will be calculated from the molecular formula when all individual densities are provided and the density field will be greyed out and ignored.

50%vol H2O@1 // D2O\@1.1

Expand All @@ -99,7 +99,7 @@ For even more complicated solutions, parenthesis can be nested and the number of

*Reminder the solution density is the largest source of error for these calculations.* Measure the solution density for an accurate result. For example:

A brine solution containing 10.44 g Al(NO\ :sub:`3`\)\ :sub:`3`\ |cdot| 9H\ :sub:`2`\O and 27.51 g D\ :sub:`2`\O was prepared for a wormlike micelle experiments. The solution density was measured with a density meter and found to be 1.22 (|g/cm^3|). Using the scattering lenth density calculator we enter:
A brine solution containing 10.44 g Al(NO\ :sub:`3`\)\ :sub:`3`\ |cdot| 9H\ :sub:`2`\O and 27.51 g D\ :sub:`2`\O was prepared for a wormlike micelle experiments. The solution density was measured with a density meter and found to be 1.22 (|g/cm^3|). Using the scattering length density calculator we enter:

10.44g Al(NO3)3(H2O)9 // 27.5126g D2O

Expand Down
Loading