Skip to content

Commit 6d51bc9

Browse files
committed
A superbig error while reading
1 parent 64bd91d commit 6d51bc9

2 files changed

Lines changed: 86 additions & 8 deletions

File tree

Modules/AdvancedCalculations.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading, copy
44

55
import numpy as np
6+
import scipy, scipy.interpolate
67

78
import sys, os
89

@@ -267,4 +268,75 @@ def prepare_input_file(self, structures, calc, labels):
267268
print('THREAD: {} inputs: {} outputs: {}'.format(threading.get_native_id(), list_of_inputs, list_of_outputs))
268269

269270

270-
return list_of_inputs, list_of_outputs
271+
return list_of_inputs, list_of_outputs
272+
273+
274+
275+
def get_optical_spectrum(ensemble, w_array = None):
276+
"""
277+
COMPUTE THE OPTICAL SPECTRUM
278+
============================
279+
280+
By averaging the dielectric properties of phonon-displaced configurations,
281+
we can get the phonon-renormalized optical spectrum.
282+
283+
284+
Parameters
285+
----------
286+
ensemble : sscha.Ensemble.Ensemble
287+
The ensemble. It should contain the optical data.
288+
To do it, use the cluster calculator OpticalQECluster of
289+
the AdvancedCalculation module.
290+
w_array : ndarray, Optional
291+
The frequencies at which to interpolate the results.
292+
If not passed, the full dataset of frequencies will be returned.
293+
294+
Results
295+
-------
296+
w : ndarray, dtype = float
297+
The frequency (eV)
298+
n : ndarray, dtype = complex
299+
The (complex) refractive index
300+
"""
301+
302+
w_data = None
303+
for i in range(ensemble.N):
304+
if not 'epsilon' in ensemble.all_properties[i]:
305+
ERR = """
306+
Error, the configuration {} has no 'epsilon' data.
307+
""".format(i)
308+
raise ValueError(ERR)
309+
310+
data = ensemble.all_properties[i]['epsilon']
311+
312+
if w_data is None:
313+
w_data = data[:,0]
314+
eps_real = np.zeros_like(w_data)
315+
eps_imag = np.zeros_like(w_data)
316+
317+
318+
eps_real += data[:,1]
319+
eps_imag += data[:,2]
320+
321+
eps_real /= ensemble.N
322+
eps_imag /= ensemble.N
323+
324+
# Interpolate the data if requested
325+
if w_array is not None:
326+
f_real = scipy.interpolate.interp1d(w_data, eps_real,
327+
kind = 'cubic', bounds_error = False, fill_value = 'extrapolate')
328+
eps_real = f_real(w_array)
329+
f_imag = scipy.interpolate.interp1d(w_data, eps_imag,
330+
kind = 'cubic', bounds_error = False, fill_value = 'extrapolate')
331+
eps_imag = f_imag(w_array)
332+
w_data = w_array
333+
334+
# Build the complex epsilon
335+
epsilon = eps_real + 1j * eps_imag
336+
337+
# Build the refractive index
338+
n = np.sqrt(epsilon)
339+
340+
return w_data, n
341+
342+

Modules/Ensemble.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -977,15 +977,21 @@ def load_bin(self, data_dir, population_id = 1, avoid_loading_dyn = False):
977977
self.force_computed = np.ones(self.N, dtype = bool)
978978

979979
all_prop_fname = os.path.join(data_dir, "all_properties_pop%d.json" % population_id)
980+
self.all_properties = [{}] * self.N
980981
if os.path.exists(all_prop_fname):
981-
with open(os.path.join(data_dir, "all_properties_pop%d.json" % population_id), "w") as fp:
982-
props= json.load(fp)
983-
if "properties" in props:
984-
self.all_properties = props["properties"]
985-
else:
982+
with open(os.path.join(data_dir, "all_properties_pop%d.json" % population_id), "r") as fp:
983+
try:
984+
props= json.load(fp)
985+
reading = True
986+
if "properties" in props:
987+
self.all_properties = props["properties"]
988+
else:
989+
reading = False
990+
except:
991+
reading = False
992+
993+
if not reading:
986994
warnings.warn("WARNING: found file {} but not able to load the properties keyword.".format(all_prop_fname))
987-
else:
988-
self.all_properties = [{}] * self.N
989995

990996

991997
def init_from_structures(self, structures):

0 commit comments

Comments
 (0)