Skip to content

Commit 843e258

Browse files
committed
Added the possibility to save the properties with save_bin and load_bin
1 parent 8a16ad2 commit 843e258

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

Modules/Ensemble.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import print_function
33
import sys, os
4+
import warnings
45
import numpy as np
56
import time
67

@@ -33,6 +34,7 @@
3334

3435
import sscha.Parallel as Parallel
3536
from sscha.Parallel import pprint as print
37+
from sscha.Tools import NumpyEncoder
3638

3739
import json
3840

@@ -133,7 +135,6 @@ def __init__(self, dyn0, T0, supercell = None, **kwargs):
133135
self.forces = []
134136
self.stresses = []
135137
self.xats = []
136-
self.all_properties = []
137138
self.units = UNITS_DEFAULT
138139

139140
self.sscha_energies = []
@@ -186,7 +187,7 @@ def __init__(self, dyn0, T0, supercell = None, **kwargs):
186187
self.stress_computed = None
187188

188189
# Get the extra quantities
189-
self.extra_quantities = {}
190+
self.all_properties = []
190191

191192

192193
# Setup the attribute control
@@ -770,9 +771,9 @@ def save_bin(self, data_dir, population_id = 1):
770771

771772
self.dyn_0.save_qe("%s/dyn_gen_pop%d_" % (data_dir, population_id))
772773

773-
if len(self.all_properties):
774-
with open(os.path.join(data_dir, "all_properties_pop%d.json" % population_id), "w") as fp:
775-
json.dump({"properties" : self.all_properties}, fp)
774+
if len(self.all_properties):
775+
with open(os.path.join(data_dir, "all_properties_pop%d.json" % population_id), "w") as fp:
776+
json.dump({"properties" : self.all_properties}, fp, cls=NumpyEncoder)
776777

777778
def save_enhanced_xyz(self, filename, append_mode = True, stress_key = "virial", forces_key = "force", energy_key = "energy"):
778779
"""
@@ -960,6 +961,16 @@ def load_bin(self, data_dir, population_id = 1, avoid_loading_dyn = False):
960961
# Setup that both forces and stresses are not computed
961962
self.stress_computed = np.ones(self.N, dtype = bool)
962963
self.force_computed = np.ones(self.N, dtype = bool)
964+
965+
all_prop_fname = os.path.join(data_dir, "all_properties_pop%d.json" % population_id)
966+
if os.path.exists(all_prop_fname):
967+
with open(os.path.join(data_dir, "all_properties_pop%d.json" % population_id), "w") as fp:
968+
props= json.load(fp)
969+
if "properties" in props:
970+
self.all_properties = props["properties"]
971+
else:
972+
warnings.warn("WARNING: found file {} but not able to load the properties keyword.".format(all_prop_fname))
973+
963974

964975

965976
def init_from_structures(self, structures):

Modules/Tools.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
import scipy, scipy.linalg
2828
import numpy as np
2929
import os
30+
import json
31+
class NumpyEncoder(json.JSONEncoder):
32+
def default(self, obj):
33+
if isinstance(obj, np.ndarray):
34+
return obj.tolist()
35+
return json.JSONEncoder.default(self, obj)
3036

3137

3238
# Here some usefull functions to solve linear systems

0 commit comments

Comments
 (0)