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

Improve DISF+DCSF summation #619

Open
wants to merge 8 commits into
base: protos
Choose a base branch
from
4 changes: 2 additions & 2 deletions MDANSE/Src/MDANSE/Core/Platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def __new__(cls, *args, **kwargs):
"""
Create a new instance of Platform class.

:param cls: the class to instanciate.
:param cls: the class to instantiate.
:type cls: class
"""

# Case of the first instanciation.
# Case of the first instantiation.
if cls.__instance is None:
cls.__instance = super(Platform, cls).__new__(cls, *args, **kwargs)

Expand Down
2 changes: 1 addition & 1 deletion MDANSE/Src/MDANSE/Core/Singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Singleton(type):

def __call__(self, *args, **kwargs):
"""
Creates (or returns if it has already been instanciated) an instance of the class.
Creates (or returns if it has already been instantiated) an instance of the class.
"""

if self.__name__ not in self.__instances:
Expand Down
22 changes: 13 additions & 9 deletions MDANSE/Src/MDANSE/Framework/Configurators/WeightsConfigurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,24 @@ def configure(self, value):
self.error_status = "OK"

def get_weights(self):
ascfg = self._configurable[self._dependencies["atom_selection"]]
atom_selection_configurator = self._configurable[
self._dependencies["atom_selection"]
]

weights = {}
for i in range(ascfg["selection_length"]):
name = ascfg["names"][i]
for el in ascfg["elements"][i]:
p = ATOMS_DATABASE.get_atom_property(el, self["property"])
for i in range(atom_selection_configurator["selection_length"]):
name = atom_selection_configurator["names"][i]
for element in atom_selection_configurator["elements"][i]:
property = ATOMS_DATABASE.get_atom_property(element, self["property"])
if name in weights:
weights[name] += p
weights[name] += property
else:
weights[name] = p
weights[name] = property

for k, v in list(ascfg.get_natoms().items()):
weights[k] /= v
for element, num_atoms in list(
atom_selection_configurator.get_natoms().items()
):
weights[element] /= num_atoms

return weights

Expand Down
32 changes: 15 additions & 17 deletions MDANSE/Src/MDANSE/Framework/Jobs/CurrentCorrelationFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from scipy.signal import correlate

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import (
differentiate,
get_spectrum,
Expand Down Expand Up @@ -391,36 +391,34 @@ def finalize(self):
fft="rfft",
)

jqtLongTotal = weight(
self.configuration["weights"].get_weights(),
weights = self.configuration["weights"].get_weights()
weight_dict = get_weights(weights, nAtomsPerElement, 2)
assign_weights(self._outputData, weight_dict, "j(q,t)_long_%s%s")
assign_weights(self._outputData, weight_dict, "j(q,t)_trans_%s%s")
assign_weights(self._outputData, weight_dict, "J(q,f)_long_%s%s")
assign_weights(self._outputData, weight_dict, "J(q,f)_trans_%s%s")
jqtLongTotal = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"j(q,t)_long_%s%s",
)
self._outputData["j(q,t)_long_total"][:] = jqtLongTotal
jqtTransTotal = weight(
self.configuration["weights"].get_weights(),
jqtTransTotal = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"j(q,t)_trans_%s%s",
)
self._outputData["j(q,t)_trans_total"][:] = jqtTransTotal

sqfLongTotal = weight(
self.configuration["weights"].get_weights(),
sqfLongTotal = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"J(q,f)_long_%s%s",
)
self._outputData["J(q,f)_long_total"][:] = sqfLongTotal
sqfTransTotal = weight(
self.configuration["weights"].get_weights(),
sqfTransTotal = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"J(q,f)_trans_%s%s",
)
self._outputData["J(q,f)_trans_total"][:] = sqfTransTotal
Expand Down
21 changes: 10 additions & 11 deletions MDANSE/Src/MDANSE/Framework/Jobs/DensityOfStates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from scipy.signal import correlate

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import differentiate, get_spectrum
from MDANSE.MolecularDynamics.TrajectoryUtils import sorted_atoms
from MDANSE.MLogging import LOG
Expand Down Expand Up @@ -238,21 +238,20 @@ def finalize(self):
)

weights = self.configuration["weights"].get_weights()
self._outputData["vacf_total"][:] = weight(
weights,
weight_dict = get_weights(weights, nAtomsPerElement, 1)
assign_weights(self._outputData, weight_dict, "vacf_%s")
assign_weights(self._outputData, weight_dict, "dos_%s")
self._outputData["vacf_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"vacf_%s",
update_partials=True,
update_partials=False,
)
self._outputData["dos_total"][:] = weight(
weights,
self._outputData["dos_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"dos_%s",
update_partials=True,
update_partials=False,
)

self._outputData.write(
Expand Down
11 changes: 6 additions & 5 deletions MDANSE/Src/MDANSE/Framework/Jobs/DensityProfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from MDANSE.Core.Error import Error
from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum


class DensityProfileError(Error):
Expand Down Expand Up @@ -176,11 +176,12 @@ def finalize(self):
for element in n_atoms_per_element.keys():
self._outputData["dp_%s" % element] += self.numberOfSteps

dp_total = weight(
self.configuration["weights"].get_weights(),
weights = self.configuration["weights"].get_weights()
weight_dict = get_weights(weights, n_atoms_per_element, 1)
assign_weights(self._outputData, weight_dict, "dp_%s")
dp_total = weighted_sum(
self._outputData,
n_atoms_per_element,
1,
weight_dict,
"dp_%s",
)

Expand Down
27 changes: 15 additions & 12 deletions MDANSE/Src/MDANSE/Framework/Jobs/DynamicCoherentStructureFactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from MDANSE.Core.Error import Error
from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import get_spectrum


Expand Down Expand Up @@ -249,33 +249,36 @@ def finalize(self):
"""

nAtomsPerElement = self.configuration["atom_selection"].get_natoms()

weights = self.configuration["weights"].get_weights()
weight_dict = get_weights(weights, nAtomsPerElement, 2)
assign_weights(self._outputData, weight_dict, "f(q,t)_%s%s")
assign_weights(self._outputData, weight_dict, "s(q,f)_%s%s")
for pair in self._elementsPairs:
ni = nAtomsPerElement[pair[0]]
nj = nAtomsPerElement[pair[1]]
self._outputData["f(q,t)_%s%s" % pair][:] /= np.sqrt(ni * nj)
extra_scaling = 1.0 / np.sqrt(ni * nj)
self._outputData["f(q,t)_%s%s" % pair].scaling_factor *= extra_scaling
self._outputData["s(q,f)_%s%s" % pair][:] = get_spectrum(
self._outputData["f(q,t)_%s%s" % pair],
self.configuration["instrument_resolution"]["time_window"],
self.configuration["instrument_resolution"]["time_step"],
axis=1,
)
self._outputData["s(q,f)_%s%s" % pair].scaling_factor *= extra_scaling

self._outputData["f(q,t)_total"][:] = weight(
self.configuration["weights"].get_weights(),
self._outputData["f(q,t)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"f(q,t)_%s%s",
update_partials=True,
update_partials=False,
)

self._outputData["s(q,f)_total"][:] = weight(
self.configuration["weights"].get_weights(),
self._outputData["s(q,f)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
2,
weight_dict,
"s(q,f)_%s%s",
update_partials=True,
update_partials=False,
)

self._outputData.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from scipy.signal import correlate

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import get_spectrum
from MDANSE.MolecularDynamics.TrajectoryUtils import sorted_atoms

Expand Down Expand Up @@ -255,33 +255,33 @@ def finalize(self):
"""

nAtomsPerElement = self.configuration["atom_selection"].get_natoms()
weights = self.configuration["weights"].get_weights()
weight_dict = get_weights(weights, nAtomsPerElement, 1)
assign_weights(self._outputData, weight_dict, "f(q,t)_%s")
assign_weights(self._outputData, weight_dict, "s(q,f)_%s")
for element, number in list(nAtomsPerElement.items()):
self._outputData["f(q,t)_%s" % element][:] /= number
extra_scaling = 1.0 / number
self._outputData["f(q,t)_%s" % element].scaling_factor *= extra_scaling
self._outputData["s(q,f)_%s" % element][:] = get_spectrum(
self._outputData["f(q,t)_%s" % element],
self.configuration["instrument_resolution"]["time_window"],
self.configuration["instrument_resolution"]["time_step"],
axis=1,
)
self._outputData["s(q,f)_%s" % element].scaling_factor *= extra_scaling

weights = self.configuration["weights"].get_weights()

self._outputData["f(q,t)_total"][:] = weight(
weights,
self._outputData["f(q,t)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"f(q,t)_%s",
update_partials=True,
update_partials=False,
)

self._outputData["s(q,f)_total"][:] = weight(
weights,
self._outputData["s(q,f)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"s(q,f)_%s",
update_partials=True,
update_partials=False,
)

self._outputData.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.MolecularDynamics.TrajectoryUtils import sorted_atoms


Expand Down Expand Up @@ -198,13 +198,13 @@ def finalize(self):
self._outputData["eisf_%s" % element][:] /= number

weights = self.configuration["weights"].get_weights()
self._outputData["eisf_total"][:] = weight(
weights,
weight_dict = get_weights(weights, nAtomsPerElement, 1)
assign_weights(self._outputData, weight_dict, "eisf_%s")
self._outputData["eisf_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"eisf_%s",
update_partials=True,
update_partials=False,
)

self._outputData.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import get_spectrum
from MDANSE.MolecularDynamics.Analysis import mean_square_displacement
from MDANSE.MolecularDynamics.TrajectoryUtils import sorted_atoms
Expand Down Expand Up @@ -250,23 +250,21 @@ def finalize(self):
axis=1,
)
weights = self.configuration["weights"].get_weights()

self._outputData["f(q,t)_total"][:] = weight(
weights,
weight_dict = get_weights(weights, nAtomsPerElement, 1)
assign_weights(self._outputData, weight_dict, "f(q,t)_%s")
assign_weights(self._outputData, weight_dict, "s(q,f)_%s")
self._outputData["f(q,t)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"f(q,t)_%s",
update_partials=True,
update_partials=False,
)

self._outputData["s(q,f)_total"][:] = weight(
weights,
self._outputData["s(q,f)_total"][:] = weighted_sum(
self._outputData,
nAtomsPerElement,
1,
weight_dict,
"s(q,f)_%s",
update_partials=True,
update_partials=False,
)

self._outputData.write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import collections

from MDANSE.Framework.Jobs.IJob import IJob
from MDANSE.Mathematics.Arithmetic import weight
from MDANSE.Mathematics.Arithmetic import assign_weights, get_weights, weighted_sum
from MDANSE.Mathematics.Signal import correlation, normalize


Expand Down Expand Up @@ -163,12 +163,14 @@ def finalize(self):
if self._outputData["gacf_{}}".format(element)][0] == 0:
raise ValueError("The normalization factor is equal to zero")
else:
self._outputData["gacf_{}".format(element)] = normalize(
self._outputData["gacf_{}".format(element)][:] = normalize(
self._outputData["gacf_%s" % element], axis=0
)

weights = self.configuration["weights"].get_weights()
gacfTotal = weight(weights, self._outputData, nAtomsPerElement, 1, "gacf_%s")
weight_dict = get_weights(weights, nAtomsPerElement, 1)
assign_weights(self._outputData, weight_dict, "gacf_%s")
gacfTotal = weighted_sum(self._outputData, weight_dict, "gacf_%s")

self._outputData["gacf_total"][:] = gacfTotal

Expand Down
Loading
Loading