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

Fixed random seed in MagneticField.

parent 251f1f7a
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from mpi4py import MPI
import simplejson as json import simplejson as json
import numpy as np import numpy as np
from nifty import Field, FieldArray, RGSpace from nifty import Field, FieldArray, RGSpace
from d2o import distributed_data_object
comm = MPI.COMM_WORLD
size = comm.size
rank = comm.rank
class MagneticField(Field): class MagneticField(Field):
...@@ -27,12 +34,18 @@ class MagneticField(Field): ...@@ -27,12 +34,18 @@ class MagneticField(Field):
for p in self.parameter_list: for p in self.parameter_list:
self._parameters[p] = np.float(parameters[p]) self._parameters[p] = np.float(parameters[p])
casted_random_seed = np.empty(self.shape[0], dtype=np.int) casted_random_seed = distributed_data_object(
global_shape=(self.shape[0],),
dtype=np.int,
distribution_strategy=distribution_strategy)
if random_seed is None: if random_seed is None:
random_seed = np.random.randint(np.uint32(-1)/3, random_seed = np.random.randint(np.uint32(-1)/3,
size=self.shape[0]) size=self.shape[0])
random_seed = comm.bcast(random_seed, root=0)
casted_random_seed[:] = random_seed casted_random_seed[:] = random_seed
self.random_seed = tuple(casted_random_seed) self.random_seed = casted_random_seed
@property @property
def parameter_list(self): def parameter_list(self):
...@@ -50,13 +63,14 @@ class MagneticField(Field): ...@@ -50,13 +63,14 @@ class MagneticField(Field):
def _to_hdf5(self, hdf5_group): def _to_hdf5(self, hdf5_group):
hdf5_group.attrs['_parameters'] = json.dumps(self._parameters) hdf5_group.attrs['_parameters'] = json.dumps(self._parameters)
hdf5_group.create_dataset('random_seed', data=self.random_seed) ret_dict = super(MagneticField, self)._to_hdf5(hdf5_group=hdf5_group)
return super(MagneticField, self)._to_hdf5(hdf5_group=hdf5_group) ret_dict['random_seed'] = self.random_seed
return ret_dict
@classmethod @classmethod
def _from_hdf5(cls, hdf5_group, repository): def _from_hdf5(cls, hdf5_group, repository):
new_field = super(MagneticField, cls)._from_hdf5(hdf5_group=hdf5_group, new_field = super(MagneticField, cls)._from_hdf5(hdf5_group=hdf5_group,
repository=repository) repository=repository)
new_field._parameters = json.loads(hdf5_group.attrs['_parameters']) 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 return new_field
...@@ -113,7 +113,10 @@ class Hammurapy(Observer): ...@@ -113,7 +113,10 @@ class Hammurapy(Observer):
lx, ly, lz = np.array(grid_space.shape)*np.array(grid_space.distances) lx, ly, lz = np.array(grid_space.shape)*np.array(grid_space.distances)
nx, ny, nz = grid_space.shape nx, ny, nz = grid_space.shape
if local_ensemble_index is not None: 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 = magnetic_field.random_seed.data[local_ensemble_index]
parameter_dict.update({'B_field_seed': random_seed}) parameter_dict.update({'B_field_seed': random_seed})
parameter_dict.update({'B_field_lx': lx, parameter_dict.update({'B_field_lx': lx,
......
...@@ -4,4 +4,4 @@ ...@@ -4,4 +4,4 @@
# 1) we don't load dependencies by storing it in __init__.py # 1) we don't load dependencies by storing it in __init__.py
# 2) we can import it in setup.py for the same reason # 2) we can import it in setup.py for the same reason
# 3) we can import it into your module module # 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