forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
abinsalgorithm.py
922 lines (757 loc) · 44.2 KB
/
abinsalgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2020 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source
# & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
# Supporting functions for the Abins Algorithm that don't belong in
# another part of AbinsModules.
import os
import re
from typing import Dict, Iterable, List, Optional, Tuple, Union
import numpy as np
from mantid.api import mtd, FileAction, FileProperty, WorkspaceGroup, WorkspaceProperty
from mantid.kernel import Atom, Direction, StringListValidator, StringArrayProperty, logger
from mantid.simpleapi import CloneWorkspace, SaveAscii, Scale
import abins
from abins.constants import AB_INITIO_FILE_EXTENSIONS, ALL_INSTRUMENTS, ATOM_PREFIX
from abins.instruments import get_instrument, Instrument
class AbinsAlgorithm:
"""Class providing shared utility for multiple inheritence by 1D, 2D implementations"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) # i.e. forward everything to PythonAlgorithm
# User input private properties
self._instrument_name = None
self._vibrational_or_phonon_data_file = None
self._ab_initio_program = None
self._out_ws_name = None
self._temperature = None
self._atoms = None
self._sum_contributions = None
self._save_ascii = None
self._scale_by_cross_section = None
self._num_quantum_order_events = None
self._autoconvolution = None
self._energy_units = None
# Interally-used private properties
self._max_event_order = None
self._bin_width = None
def get_common_properties(self) -> None:
"""From user input, set properties common to Abins 1D and 2D versions"""
self._ab_initio_program = self.getProperty("AbInitioProgram").value
self._vibrational_or_phonon_data_file = self.getProperty("VibrationalOrPhononFile").value
self._out_ws_name = self.getPropertyValue('OutputWorkspace')
self._temperature = self.getProperty("TemperatureInKelvin").value
self._atoms = self.getProperty("Atoms").value
self._sum_contributions = self.getProperty("SumContributions").value
self._save_ascii = self.getProperty("SaveAscii").value
self._scale_by_cross_section = self.getPropertyValue('ScaleByCrossSection')
self._energy_units = self.getProperty("EnergyUnits").value
# conversion from str to int
self._num_quantum_order_events = int(self.getProperty("QuantumOrderEventsNumber").value)
self._max_event_order = self._num_quantum_order_events # This default can be replaced in child class
def set_instrument(self) -> None:
"""Instantiate self._instrument using name and self._instrument_kwargs"""
instrument_name = self.getProperty("Instrument").value
if instrument_name in ALL_INSTRUMENTS:
self._instrument_name = instrument_name
self._instrument = get_instrument(self._instrument_name,
**self._instrument_kwargs)
else:
raise ValueError("Unknown instrument %s" % instrument_name)
def get_instrument(self) -> Union[Instrument, None]:
return self._instrument
def declare_common_properties(self) -> None:
"""Declare properties common to Abins 1D and 2D versions"""
self.declareProperty(FileProperty("VibrationalOrPhononFile", "",
action=FileAction.Load,
direction=Direction.Input,
extensions=AB_INITIO_FILE_EXTENSIONS),
doc="File with the data from a vibrational or phonon calculation.")
self.declareProperty(name="AbInitioProgram",
direction=Direction.Input,
defaultValue="CASTEP",
validator=StringListValidator(["CASTEP", "CRYSTAL", "DMOL3",
"FORCECONSTANTS", "GAUSSIAN", "VASP"]),
doc="An ab initio program which was used for vibrational or phonon calculation.")
self.declareProperty(WorkspaceProperty("OutputWorkspace", '', Direction.Output),
doc="Name to give the output workspace.")
self.declareProperty(name="TemperatureInKelvin",
direction=Direction.Input,
defaultValue=10.0,
doc="Temperature in K for which dynamical structure factor S should be calculated.")
self.declareProperty(StringArrayProperty("Atoms", Direction.Input),
doc="List of atoms to use to calculate partial S."
"If left blank, workspaces with S for all types of atoms will be calculated. "
"Element symbols will be interpreted as a sum of all atoms of that element in the "
"cell. 'atomN' or 'atom_N' (where N is a positive integer) will be interpreted as "
"individual atoms, indexing from 1 following the order of the input data.")
self.declareProperty(name="SumContributions", defaultValue=False,
doc="Sum the partial dynamical structure factors into a single workspace.")
self.declareProperty(name="SaveAscii", defaultValue=False,
doc="Write workspaces to .ascii files after computing them.")
self.declareProperty(name="ScaleByCrossSection", defaultValue='Incoherent',
validator=StringListValidator(['Total', 'Incoherent', 'Coherent']),
doc="Scale the partial dynamical structure factors by the scattering cross section.")
# Abins is supposed to support excitations up to fourth-order. Order 3 and 4 are currently disabled while the
# weighting is being investigated; these intensities were unreasonably large in hydrogenous test cases
self.declareProperty(name="QuantumOrderEventsNumber", defaultValue='1',
validator=StringListValidator(['1', '2']),
doc="Number of quantum order effects included in the calculation "
"(1 -> FUNDAMENTALS, 2-> first overtone + FUNDAMENTALS + 2nd order combinations")
self.declareProperty(name="Autoconvolution", defaultValue=False,
doc="Estimate higher quantum orders by convolution with fundamental spectrum.")
self.declareProperty(name="EnergyUnits",
defaultValue="cm-1",
direction=Direction.Input,
validator=StringListValidator(["cm-1", "meV"]),
doc="Energy units for output workspace and experimental file")
def declare_instrument_properties(
self, default: str = "TOSCA",
choices: Iterable = ALL_INSTRUMENTS,
multiple_choice_settings: List[Tuple[str, str, str]] = [],
freeform_settings: List[Tuple[str, str, str]] = []):
"""Declare properties related to instrument
Args:
default: default instrument
choices: Iterable of available instruments for "Instrument" combo box
multiple_choice_settings:
List of field names, corresponding parameter and tooltip for additional instrument settings, e.g.::
[('Setting', 'settings', 'Setting choice for this instrument (e.g. monochromator)'), ...]
This should correspond to a dict in abins.parameters.instruments[instrument]
freeform_settings:
List of field names, defaults and tooltips for additional instrument settings, e.g.::
[('IncidentEnergy', '4100', 'Incident energy in wavenumber'), ...]
"""
self.declareProperty(name="Instrument",
direction=Direction.Input,
defaultValue=default,
validator=StringListValidator(choices),
doc="Name of an instrument for which analysis should be performed.")
for property_name, parameter_name, doc in multiple_choice_settings:
# Populate list of possible instrument settings
valid_choices = ['']
for instrument in choices:
if (
(instrument in abins.parameters.instruments)
and (parameter_name in abins.parameters.instruments[instrument])):
valid_choices += list(abins.parameters.instruments[instrument][parameter_name])
valid_choices = sorted(list(set(valid_choices)))
self.declareProperty(name=property_name,
direction=Direction.Input,
defaultValue="",
validator=StringListValidator(valid_choices),
doc=doc)
for property_name, default, doc in freeform_settings:
self.declareProperty(name=property_name,
direction=Direction.Input,
defaultValue=default,
doc=doc)
def validate_common_inputs(self, issues: dict = None) -> Dict[str, str]:
"""Validate inputs common to Abins 1D and 2D versions
Args:
abins: Algorithm instance for validation
issues: Collection of validation issues to append to. (This will be mutated without copying.)
Returns:
issues dict including any new issues
"""
if issues is None:
issues = {}
input_file_validators = {"CASTEP": self._validate_castep_input_file,
"CRYSTAL": self._validate_crystal_input_file,
"DMOL3": self._validate_dmol3_input_file,
"FORCECONSTANTS": self._validate_euphonic_input_file,
"GAUSSIAN": self._validate_gaussian_input_file,
"VASP": self._validate_vasp_input_file}
ab_initio_program = self.getProperty("AbInitioProgram").value
vibrational_or_phonon_data_filename = self.getProperty("VibrationalOrPhononFile").value
output = input_file_validators[ab_initio_program](vibrational_or_phonon_data_filename)
if output["Invalid"]:
issues["VibrationalOrPhononFile"] = output["Comment"]
workspace_name = self.getPropertyValue("OutputWorkspace")
# list of special keywords which cannot be used in the name of workspace
forbidden_keywords = {"total",}
if workspace_name in mtd:
issues["OutputWorkspace"] = "Workspace with name " + workspace_name + " already in use; please give " \
"a different name for workspace."
elif workspace_name == "":
issues["OutputWorkspace"] = "Please specify name of workspace."
for word in forbidden_keywords:
if word in workspace_name:
issues["OutputWorkspace"] = "Keyword: " + word + " cannot be used in the name of workspace."
break
temperature = self.getProperty("TemperatureInKelvin").value
if temperature < 0:
issues["TemperatureInKelvin"] = "Temperature must be positive."
return issues
def _validate_instrument_settings(self, name='Setting', parameter='settings') -> Dict[str, str]:
"""Check that multiple-choice parameter is compatible with selected Instrument"""
instrument_name = self.getProperty("Instrument").value
setting = self.getProperty(name).value
if instrument_name not in abins.parameters.instruments:
# If an instrument lacks an entry in abins.parameters, we cannot
# reason about how appropriate the setting is; assume good.
return {}
parameters = abins.parameters.instruments.get(instrument_name)
if setting == '':
if parameter + '_default' in parameters:
return {}
else:
return {"Setting":
f'Instrument "{instrument_name}" does not have a default '
+ 'setting, and no setting was specified. Accepted settings: '
+ ', '.join(parameters[parameter].keys())}
downcased_settings = {s.lower(): s for s in parameters[parameter]}
if setting.lower() in downcased_settings:
return {}
return {name:
f'{name}: "{setting}" is unknown for instrument '
+ f'{instrument_name}. Supported values: '
+ ', '.join(sorted(parameters[parameter].keys()))}
@staticmethod
def get_atom_selection(*, atoms_data: abins.AtomsData, selection: list) -> Tuple[list, list]:
"""Interpret the user 'Atoms' input as a set of elements and atom indices
(These atom indices match the user-facing convention and begin at 1.)"""
num_atoms = len(atoms_data)
all_atms_smbls = list(set([atoms_data[atom_index]["symbol"]
for atom_index in range(num_atoms)]))
all_atms_smbls.sort()
if len(selection) == 0: # case: all atoms
atom_symbols = all_atms_smbls
atom_numbers = []
else: # case selected atoms
# Specific atoms are identified with prefix and integer index, e.g 'atom_5'. Other items are element symbols
# A regular expression match is used to make the underscore separator optional and check the index format
atom_symbols = [item for item in selection if item[:len(ATOM_PREFIX)] != ATOM_PREFIX]
if len(atom_symbols) != len(set(atom_symbols)): # only different types
raise ValueError("User atom selection (by symbol) contains repeated species. This is not permitted as "
"Abins cannot create multiple workspaces with the same name.")
numbered_atom_test = re.compile('^' + ATOM_PREFIX + r'_?(\d+)$')
atom_numbers = [numbered_atom_test.findall(item) for item in selection] # Matches will be lists of str
atom_numbers = [int(match[0]) for match in atom_numbers if match] # Remove empty matches, cast rest to int
if len(atom_numbers) != len(set(atom_numbers)):
raise ValueError("User atom selection (by number) contains repeated atom. This is not permitted as Abins"
" cannot create multiple workspaces with the same name.")
for atom_symbol in atom_symbols:
if atom_symbol not in all_atms_smbls:
raise ValueError("User defined atom selection (by element) '%s': not present in the system." %
atom_symbol)
for atom_number in atom_numbers:
if atom_number < 1 or atom_number > num_atoms:
raise ValueError("Invalid user atom selection (by number) '%s%s': out of range (%s - %s)" %
(ATOM_PREFIX, atom_number, 1, num_atoms))
# Final sanity check that everything in "atoms" field was understood
if len(atom_symbols) + len(atom_numbers) < len(selection):
elements_report = " Symbols: " + ", ".join(atom_symbols) if len(atom_symbols) else ""
numbers_report = " Numbers: " + ", ".join(atom_numbers) if len(atom_numbers) else ""
raise ValueError("Not all user atom selections ('atoms' option) were understood."
+ elements_report + numbers_report)
return atom_numbers, atom_symbols
@staticmethod
def get_masses_table(atoms_data):
"""
Collect masses associated with each element in atoms_data
:param num_atoms: Number of atoms in the system. (Saves time working out iteration.)
:type num_atoms: int
:returns: Mass data in form ``{el1: [m1, ...], ... }``
"""
masses = {}
for atom in atoms_data:
symbol = atom["symbol"]
mass = atom["mass"]
if symbol not in masses:
masses[symbol] = set()
masses[symbol].add(mass)
# convert set to list to fix order
for s in masses:
masses[s] = sorted(list(set(masses[s])))
return masses
def create_workspaces(self, atoms_symbols=None, atom_numbers=None, *, s_data, atoms_data, max_quantum_order):
"""
Creates workspaces for all types of atoms. Creates both partial and total workspaces for given types of atoms.
:param atoms_symbols: atom types (i.e. element symbols) for which S should be created.
:type iterable of str:
:param atom_numbers:
indices of individual atoms for which S should be created. (One-based numbering; 1 <= I <= NUM_ATOMS)
:type iterable of int:
:param s_data: dynamical factor data
:type abins.SData
:param atoms_data: atom positions/masses
:type abins.AtomsData:
:param max_quantum_order: maximum quantum order to include
:type int:
:returns: workspaces for list of atoms types, S for the particular type of atom
"""
from abins.constants import FLOAT_TYPE, MASS_EPS, ONLY_ONE_MASS
# Create appropriately-shaped arrays to be used in-place by _atom_type_s - avoid repeated slow instantiation
shape = [max_quantum_order]
shape.extend(list(s_data[0]["order_1"].shape))
s_atom_data = np.zeros(shape=tuple(shape), dtype=FLOAT_TYPE)
temp_s_atom_data = np.copy(s_atom_data)
num_atoms = len(s_data)
masses = self.get_masses_table(atoms_data)
result = []
if atoms_symbols is not None:
for symbol in atoms_symbols:
sub = (len(masses[symbol]) > ONLY_ONE_MASS
or abs(Atom(symbol=symbol).mass - masses[symbol][0]) > MASS_EPS)
for m in masses[symbol]:
result.extend(self._atom_type_s(num_atoms=num_atoms, mass=m, s_data=s_data, atoms_data=atoms_data,
element_symbol=symbol, temp_s_atom_data=temp_s_atom_data,
s_atom_data=s_atom_data, substitution=sub))
if atom_numbers is not None:
for atom_number in atom_numbers:
result.extend(self._atom_number_s(atom_number=atom_number, s_data=s_data,
s_atom_data=s_atom_data, atoms_data=atoms_data))
return result
def _create_workspace(self, atom_name=None, s_points=None, optional_name="", protons_number=None,
nucleons_number=None):
"""
Creates workspace for the given frequencies and s_points with S data. After workspace is created it is rebined,
scaled by cross-section factor and optionally multiplied by the user defined scaling factor.
:param atom_name: symbol of atom for which workspace should be created
:param s_points: S(Q, omega)
:param optional_name: optional part of workspace name
:returns: workspace for the given frequency and S data
:param protons_number: number of protons in the given type fo atom
:param nucleons_number: number of nucleons in the given type of atom
"""
ws_name = self._out_ws_name + "_" + atom_name + optional_name
self._fill_s_workspace(s_points=s_points, workspace=ws_name, protons_number=protons_number,
nucleons_number=nucleons_number)
return ws_name
def _atom_number_s(self, *, atom_number, s_data, s_atom_data, atoms_data):
"""
Helper function for calculating S for the given atomic index
:param atom_number: One-based index of atom in s_data e.g. 1 to select first element 'atom_1'
:type atom_number: int
:param s_data: Precalculated S for all atoms and quantum orders
:type s_data: abins.SData
:param s_atom_data: helper array to accumulate S (outer loop over atoms); does not transport
information but is used in-place to save on time instantiating large arrays. First dimension is quantum
order; following dimensions should match arrays in s_data.
:type s_atom_data: numpy.ndarray
:param
:returns: mantid workspaces of S for atom (total) and individual quantum orders
:returntype: list of Workspace2D
"""
from abins.constants import ATOM_PREFIX, FUNDAMENTALS
atom_workspaces = []
s_atom_data.fill(0.0)
output_atom_label = "%s_%d" % (ATOM_PREFIX, atom_number)
symbol = atoms_data[atom_number - 1]["symbol"]
z_number = Atom(symbol=symbol).z_number
for i, order in enumerate(range(FUNDAMENTALS, self._max_event_order + 1)):
s_atom_data[i] = s_data[atom_number - 1]["order_%s" % order]
total_s_atom_data = np.sum(s_atom_data, axis=0)
atom_workspaces = []
atom_workspaces.append(self._create_workspace(atom_name=output_atom_label,
s_points=np.copy(total_s_atom_data),
optional_name="_total", protons_number=z_number))
atom_workspaces.append(self._create_workspace(atom_name=output_atom_label,
s_points=np.copy(s_atom_data),
protons_number=z_number))
return atom_workspaces
def _atom_type_s(self, num_atoms=None, mass=None, s_data=None, atoms_data=None,
element_symbol=None, temp_s_atom_data=None, s_atom_data=None, substitution=None):
"""
Helper function for calculating S for the given type of atom
:param num_atoms: number of atoms in the system
:param s_data: Precalculated S for all atoms and quantum orders
:type s_data: abins.SData
:param atoms_data: Atomic position/mass data
:type atoms_data: abins.AtomsData
:param element_symbol: label for the type of atom
:param temp_s_atom_data: helper array to accumulate S (inner loop over quantum order); does not transport
information but is used in-place to save on time instantiating large arrays.
:param s_atom_data: helper array to accumulate S (outer loop over atoms); does not transport
information but is used in-place to save on time instantiating large arrays.
:param substitution: True if isotope substitution and False otherwise
"""
from abins.constants import MASS_EPS
atom_workspaces = []
s_atom_data.fill(0.0)
element = Atom(symbol=element_symbol)
for atom_index in range(num_atoms):
if (atoms_data[atom_index]["symbol"] == element_symbol
and abs(atoms_data[atom_index]["mass"] - mass) < MASS_EPS):
temp_s_atom_data.fill(0.0)
for order in range(1, self._max_event_order + 1):
order_indx = order - 1
temp_s_order = s_data[atom_index]["order_%s" % order]
temp_s_atom_data[order_indx] = temp_s_order
s_atom_data += temp_s_atom_data # sum S over the atoms of the same type
total_s_atom_data = np.sum(s_atom_data, axis=0)
nucleons_number = int(round(mass))
if substitution:
atom_workspaces.append(self._create_workspace(atom_name=str(nucleons_number) + element_symbol,
s_points=np.copy(total_s_atom_data),
optional_name="_total", protons_number=element.z_number,
nucleons_number=nucleons_number))
atom_workspaces.append(self._create_workspace(atom_name=str(nucleons_number) + element_symbol,
s_points=np.copy(s_atom_data),
protons_number=element.z_number,
nucleons_number=nucleons_number))
else:
atom_workspaces.append(self._create_workspace(atom_name=element_symbol,
s_points=np.copy(total_s_atom_data),
optional_name="_total", protons_number=element.z_number))
atom_workspaces.append(self._create_workspace(atom_name=element_symbol,
s_points=np.copy(s_atom_data),
protons_number=element.z_number))
return atom_workspaces
def create_total_workspace(self, workspaces: list) -> None:
"""Sum together elemental totals to make an additional Total workspace"""
total_atom_workspaces = []
for ws in workspaces:
if "total" in ws:
total_atom_workspaces.append(ws)
total_workspace = self._create_total_workspace(partial_workspaces=total_atom_workspaces)
workspaces.insert(0, total_workspace)
def _create_total_workspace(self, partial_workspaces=None):
"""
Sets workspace with total S.
:param partial_workspaces: list of workspaces which should be summed up to obtain total workspace
:returns: workspace with total S from partial_workspaces
"""
from abins.constants import ONE_DIMENSIONAL_INSTRUMENTS, TWO_DIMENSIONAL_INSTRUMENTS
total_workspace = self._out_ws_name + "_total"
if isinstance(mtd[partial_workspaces[0]], WorkspaceGroup):
local_partial_workspaces = mtd[partial_workspaces[0]].names()
else:
local_partial_workspaces = partial_workspaces
if len(local_partial_workspaces) > 1:
# get frequencies
ws = mtd[local_partial_workspaces[0]]
# initialize S
if self._instrument.get_name() in ONE_DIMENSIONAL_INSTRUMENTS:
s_atoms = np.zeros_like(ws.dataY(0))
if self._instrument.get_name() in TWO_DIMENSIONAL_INSTRUMENTS:
n_q = abins.parameters.instruments[self._instrument.get_name()]['q_size']
n_energy_bins = ws.getDimension(1).getNBins()
s_atoms = np.zeros([n_q, n_energy_bins])
# collect all S
for partial_ws in local_partial_workspaces:
if self._instrument.get_name() in ONE_DIMENSIONAL_INSTRUMENTS:
s_atoms += mtd[partial_ws].dataY(0)
elif self._instrument.get_name() in TWO_DIMENSIONAL_INSTRUMENTS:
for i in range(n_energy_bins):
s_atoms[:, i] += mtd[partial_ws].dataY(i)
# create workspace with S
self._fill_s_workspace(s_atoms, total_workspace)
# # Otherwise just repackage the workspace we have as the total
else:
CloneWorkspace(InputWorkspace=local_partial_workspaces[0], OutputWorkspace=total_workspace)
return total_workspace
@staticmethod
def write_workspaces_to_ascii(scale: float = 1., *, ws_name: str) -> None:
"""Write all with given root name to ascii files
:param ws_name: Workspace name (to be searched for in Mantid context)
:param scale: Scale factor to apply to data (typically 1 / bin_width)
"""
num_workspaces = mtd[ws_name].getNumberOfEntries()
for wrk_num in range(num_workspaces):
wrk = mtd[ws_name].getItem(wrk_num)
SaveAscii(InputWorkspace=Scale(wrk, scale, "Multiply"),
Filename=wrk.name() + ".dat", Separator="Space", WriteSpectrumID=False)
@staticmethod
def get_cross_section(scattering: str = 'Total',
nucleons_number: Optional[int] = None,
*,
protons_number: int) -> float:
"""
Calculates cross section for the given element.
:param scattering: Type of cross-section: 'Incoherent', 'Coherent' or 'Total'
:param protons_number: number of protons in the given type fo atom
:param nucleons_number: number of nucleons in the given type of atom
:returns: cross section for that element
"""
if nucleons_number is not None:
try:
atom = Atom(a_number=nucleons_number, z_number=protons_number)
# isotopes are not implemented for all elements so use different constructor in that cases
except RuntimeError:
logger.warning(f"Could not find data for isotope {nucleons_number}, "
f"using default values for {protons_number} protons.")
atom = Atom(z_number=protons_number)
else:
atom = Atom(z_number=protons_number)
scattering_keys = {'Incoherent': 'inc_scatt_xs',
'Coherent': 'coh_scatt_xs',
'Total': 'tot_scatt_xs'}
return atom.neutron()[scattering_keys[scattering]]
@staticmethod
def set_workspace_units(wrk, layout='1D', energy_units='cm-1'):
"""
Sets x and y units for a workspace.
:param wrk: workspace which units should be set
:type Workspace2D:
:param layout: layout of data in Workspace2D.
- '1D' is a typical indirect spectrum, with energy transfer on Axis
0 (X), S on Axis 1 (Y)
- '2D' is a 2D S(q,omega) map with momentum transfer on Axis 0 (X),
S on Axis 1 and energy transfer on Axis 2
:type str:
:param energy_units:
Preferred units in user-friendly notation. ('cm-1' or 'meV')
:type str:
"""
energy_units_map = {'cm-1': 'DeltaE_inWavenumber',
'mev': 'DeltaE'}
energy_units_str = energy_units_map.get(energy_units.lower(), '')
if not energy_units_str:
raise ValueError(f"Energy unit {energy_units} not recognised.")
if layout == '1D':
mtd[wrk].getAxis(0).setUnit(energy_units_str)
mtd[wrk].setYUnitLabel("S / Arbitrary Units")
mtd[wrk].setYUnit("Arbitrary Units")
elif layout == '2D':
mtd[wrk].getAxis(0).setUnit("MomentumTransfer")
mtd[wrk].setYUnitLabel("S / Arbitrary Units")
mtd[wrk].setYUnit("Arbitrary Units")
mtd[wrk].getAxis(1).setUnit(energy_units_str)
else:
raise ValueError('Unknown data/units layout "{}"'.format(layout))
@staticmethod
def _validate_ab_initio_file_extension(*,
ab_initio_program: str,
filename_full_path: str,
expected_file_extension: str) -> dict:
"""
Checks consistency between name of ab initio program and extension.
:param expected_file_extension: file extension
:returns: dictionary with error message
"""
msg_err = "Invalid %s file. " % filename_full_path
msg_rename = "Please rename your file and try again."
# check extension of a file
found_filename_ext = os.path.splitext(filename_full_path)[1]
if found_filename_ext.lower() != expected_file_extension:
comment = "{}Output from ab initio program {} is expected." \
" The expected extension of file is .{}. Found: {}.{}".format(
msg_err, ab_initio_program, expected_file_extension, found_filename_ext, msg_rename)
return dict(Invalid=True, Comment=comment)
else:
return dict(Invalid=False, Comment="")
@classmethod
def _validate_dmol3_input_file(cls, filename_full_path: str) -> dict:
"""
Method to validate input file for DMOL3 ab initio program.
:param filename_full_path: full path of a file to check.
:returns: True if file is valid otherwise false.
"""
logger.information("Validate DMOL3 file with vibrational data.")
return cls._validate_ab_initio_file_extension(ab_initio_program="DMOL3",
filename_full_path=filename_full_path,
expected_file_extension=".outmol")
@classmethod
def _validate_gaussian_input_file(cls, filename_full_path: str) -> dict:
"""
Method to validate input file for GAUSSIAN ab initio program.
:param filename_full_path: full path of a file to check.
:returns: True if file is valid otherwise false.
"""
logger.information("Validate GAUSSIAN file with vibration data.")
return cls._validate_ab_initio_file_extension(ab_initio_program="GAUSSIAN",
filename_full_path=filename_full_path,
expected_file_extension=".log")
@classmethod
def _validate_crystal_input_file(cls,
filename_full_path: str) -> dict:
"""
Method to validate input file for CRYSTAL ab initio program.
:param filename_full_path: full path of a file to check.
:returns: True if file is valid otherwise false.
"""
logger.information("Validate CRYSTAL file with vibrational or phonon data.")
return cls._validate_ab_initio_file_extension(ab_initio_program="CRYSTAL",
filename_full_path=filename_full_path,
expected_file_extension=".out")
@classmethod
def _validate_castep_input_file(cls, filename_full_path: str) -> dict:
"""
Check if ab initio input vibrational or phonon file has been produced by CASTEP. Currently the crucial
keywords in the first few lines are checked (to be modified if a better validation is found...)
:param filename_full_path: full path of a file to check
:returns: Dictionary with two entries "Invalid", "Comment". Valid key can have two values: True/ False. As it
comes to "Comment" it is an empty string if Valid:True, otherwise stores description of the problem.
"""
logger.information("Validate CASTEP file with vibrational or phonon data.")
msg_err = "Invalid %s file. " % filename_full_path
output = cls._validate_ab_initio_file_extension(ab_initio_program="CASTEP",
filename_full_path=filename_full_path,
expected_file_extension=".phonon")
if output["Invalid"]:
return output
else:
# check a structure of the header part of file.
# Here fortran convention is followed: case of letter does not matter
with open(filename_full_path) as castep_file:
line = cls._get_one_line(castep_file)
if not cls._compare_one_line(line, "beginheader"): # first line is BEGIN header
return dict(Invalid=True, Comment=msg_err + "The first line should be 'BEGIN header'.")
line = cls._get_one_line(castep_file)
if not cls._compare_one_line(one_line=line, pattern="numberofions"):
return dict(Invalid=True, Comment=msg_err + "The second line should include 'Number of ions'.")
line = cls._get_one_line(castep_file)
if not cls._compare_one_line(one_line=line, pattern="numberofbranches"):
return dict(Invalid=True, Comment=msg_err + "The third line should include 'Number of branches'.")
line = cls._get_one_line(castep_file)
if not cls._compare_one_line(one_line=line, pattern="numberofwavevectors"):
return dict(Invalid=True, Comment=msg_err + "The fourth line should include 'Number of wavevectors'.")
line = cls._get_one_line(castep_file)
if not cls._compare_one_line(one_line=line,
pattern="frequenciesin"):
return dict(Invalid=True, Comment=msg_err + "The fifth line should be 'Frequencies in'.")
return dict(Invalid=False, Comment="")
@classmethod
def _validate_euphonic_input_file(cls, filename_full_path: str) -> dict:
logger.information("Validate force constants file for interpolation.")
from dos.load_euphonic import euphonic_available
if euphonic_available():
try:
from euphonic.cli.utils import force_constants_from_file
force_constants_from_file(filename_full_path)
return dict(Invalid=False, Comment="")
except Exception as error:
if hasattr(error, 'message'):
message = error.message
else:
message = str(error)
return dict(Invalid=True,
Comment=f"Problem opening force constants file with Euphonic.: {message}")
else:
return dict(Invalid=True,
Comment=("Could not import Euphonic module. "
"Try running user/AdamJackson/install_euphonic.py from the Script Repository."))
@classmethod
def _validate_vasp_input_file(cls, filename_full_path: str) -> dict:
logger.information("Validate VASP file with vibrational or phonon data.")
if 'OUTCAR' in os.path.basename(filename_full_path):
return dict(Invalid=False, Comment="")
else:
output = cls._validate_ab_initio_file_extension(ab_initio_program="VASP",
filename_full_path=filename_full_path,
expected_file_extension=".xml")
if output["Invalid"]:
output["Comment"] = ("Invalid filename {}. Expected OUTCAR, *.OUTCAR or"
" *.xml for VASP calculation output. Please rename your file and try again. "
.format(filename_full_path))
return output
@staticmethod
def _get_one_line(file_obj=None):
"""
:param file_obj: file object from which reading is done
:returns: string containing one non empty line
"""
line = file_obj.readline().replace(" ", "").lower()
while line and line == "":
line = file_obj.readline().replace(" ", "").lower()
return line
@staticmethod
def _compare_one_line(one_line, pattern):
"""
compares line in the the form of string with a pattern.
:param one_line: line in the for mof string to be compared
:param pattern: string which should be present in the line after removing white spaces and setting all
letters to lower case
:returns: True is pattern present in the line, otherwise False
"""
return one_line and pattern in one_line.replace(" ", "")
@classmethod
def _check_common_advanced_parameters(cls, message=""):
cls._check_general_resolution(message)
cls._check_folder_names(message)
cls._check_rebinning(message)
cls._check_threshold(message)
cls._check_chunk_size(message)
cls._check_threads(message)
def _check_general_resolution(self, message_end=None):
"""
Checks general parameters used in construction resolution functions.
:param message_end: closing part of the error message.
"""
# check fwhm
fwhm = abins.parameters.instruments['fwhm']
if not (isinstance(fwhm, float) and 0.0 < fwhm < 10.0):
raise RuntimeError("Invalid value of fwhm" + message_end)
@staticmethod
def _check_folder_names(message_end=None):
"""
Checks folders names.
:param message_end: closing part of the error message.
"""
folder_names = []
ab_initio_group = abins.parameters.hdf_groups['ab_initio_data']
if not isinstance(ab_initio_group, str) or ab_initio_group == "":
raise RuntimeError("Invalid name for folder in which the ab initio data should be stored.")
folder_names.append(ab_initio_group)
powder_data_group = abins.parameters.hdf_groups['powder_data']
if not isinstance(powder_data_group, str) or powder_data_group == "":
raise RuntimeError("Invalid value of powder_data_group" + message_end)
elif powder_data_group in folder_names:
raise RuntimeError("Name for powder_data_group already used by as name of another folder.")
folder_names.append(powder_data_group)
crystal_data_group = abins.parameters.hdf_groups['crystal_data']
if not isinstance(crystal_data_group, str) or crystal_data_group == "":
raise RuntimeError("Invalid value of crystal_data_group" + message_end)
elif crystal_data_group in folder_names:
raise RuntimeError("Name for crystal_data_group already used as a name of another folder.")
s_data_group = abins.parameters.hdf_groups['s_data']
if not isinstance(s_data_group, str) or s_data_group == "":
raise RuntimeError("Invalid value of s_data_group" + message_end)
elif s_data_group in folder_names:
raise RuntimeError("Name for s_data_group already used as a name of another folder.")
@staticmethod
def _check_rebinning(message_end=None):
"""
Checks rebinning parameters.
:param message_end: closing part of the error message.
"""
min_wavenumber = abins.parameters.sampling['min_wavenumber']
if not (isinstance(min_wavenumber, float) and min_wavenumber >= 0.0):
raise RuntimeError("Invalid value of min_wavenumber" + message_end)
max_wavenumber = abins.parameters.sampling['max_wavenumber']
if not (isinstance(max_wavenumber, float) and max_wavenumber > 0.0):
raise RuntimeError("Invalid number of max_wavenumber" + message_end)
if min_wavenumber > max_wavenumber:
raise RuntimeError("Invalid energy window for rebinning.")
@staticmethod
def _check_threshold(message_end=None):
"""
Checks threshold for frequencies.
:param message_end: closing part of the error message.
"""
from abins.parameters import sampling
freq_threshold = sampling['frequencies_threshold']
if not (isinstance(freq_threshold, float) and freq_threshold >= 0.0):
raise RuntimeError("Invalid value of frequencies_threshold" + message_end)
# check s threshold
s_absolute_threshold = sampling['s_absolute_threshold']
if not (isinstance(s_absolute_threshold, float) and s_absolute_threshold > 0.0):
raise RuntimeError("Invalid value of s_absolute_threshold" + message_end)
s_relative_threshold = sampling['s_relative_threshold']
if not (isinstance(s_relative_threshold, float) and s_relative_threshold > 0.0):
raise RuntimeError("Invalid value of s_relative_threshold" + message_end)
@staticmethod
def _check_chunk_size(message_end=None):
"""
Check optimal size of chunk
:param message_end: closing part of the error message.
"""
optimal_size = abins.parameters.performance['optimal_size']
if not (isinstance(optimal_size, int) and optimal_size > 0):
raise RuntimeError("Invalid value of optimal_size" + message_end)
@staticmethod
def _check_threads(message_end=None):
"""
Checks number of threads
:param message_end: closing part of the error message.
"""
try:
import pathos.multiprocessing as mp
threads = abins.parameters.performance['threads']
if not (isinstance(threads, int) and 1 <= threads <= mp.cpu_count()):
raise RuntimeError("Invalid number of threads for parallelisation over atoms" + message_end)
except ImportError:
pass