Commit 6fd08918 authored by Theo Steininger's avatar Theo Steininger

Fixed random seed in MagneticField.

parent 251f1f7a
# -*- coding: utf-8 -*-
from mpi4py import MPI
import simplejson as json
import numpy as np
from nifty import Field, FieldArray, RGSpace
from d2o import distributed_data_object
size = comm.size
rank = comm.rank
class MagneticField(Field):
......@@ -27,12 +34,18 @@ class MagneticField(Field):
for p in self.parameter_list:
self._parameters[p] = np.float(parameters[p])
casted_random_seed = np.empty(self.shape[0],
casted_random_seed = distributed_data_object(
if random_seed is None:
random_seed = np.random.randint(np.uint32(-1)/3,
random_seed = comm.bcast(random_seed, root=0)
casted_random_seed[:] = random_seed
self.random_seed = tuple(casted_random_seed)
self.random_seed = casted_random_seed
def parameter_list(self):
......@@ -50,13 +63,14 @@ class MagneticField(Field):
def _to_hdf5(self, hdf5_group):
hdf5_group.attrs['_parameters'] = json.dumps(self._parameters)
hdf5_group.create_dataset('random_seed', data=self.random_seed)
return super(MagneticField, self)._to_hdf5(hdf5_group=hdf5_group)
ret_dict = super(MagneticField, self)._to_hdf5(hdf5_group=hdf5_group)
ret_dict['random_seed'] = self.random_seed
return ret_dict
def _from_hdf5(cls, hdf5_group, repository):
new_field = super(MagneticField, cls)._from_hdf5(hdf5_group=hdf5_group,
new_field._parameters = json.loads(hdf5_group.attrs['_parameters'])
new_field.random_seed = tuple(hdf5_group['random_seed'])
new_field.random_seed = repository.get('random_seed', hdf5_group)
return new_field
......@@ -113,7 +113,10 @@ class Hammurapy(Observer):
lx, ly, lz = np.array(grid_space.shape)*np.array(grid_space.distances)
nx, ny, nz = grid_space.shape
if local_ensemble_index is not None:
random_seed = magnetic_field.random_seed[local_ensemble_index]
# access the magnetic-field's random-seed d2o directly, since we
# know that the distribution strategy is the same for the
# randam samples and the magnetic field itself
random_seed =[local_ensemble_index]
parameter_dict.update({'B_field_seed': random_seed})
parameter_dict.update({'B_field_lx': lx,
......@@ -4,4 +4,4 @@
# 1) we don't load dependencies by storing it in
# 2) we can import it in for the same reason
# 3) we can import it into your module module
__version__ = '0.0.1a1'
__version__ = '0.1.0'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment