Commit 61459d3d authored by eric pellegrini's avatar eric pellegrini

advances in MDANSE framework

parent f84243a0
......@@ -51,15 +51,16 @@ class ClassRegistry(abc.ABCMeta):
return
metaClass = namespace.get("__metaclass__", None)
if metaClass is ClassRegistry:
if (ClassRegistry._registry.has_key(typ)):
return
ClassRegistry.__interfaces.append(self)
ClassRegistry._registry[typ] = {}
else:
for interface in ClassRegistry.__interfaces:
if issubclass(self, interface):
ClassRegistry._registry[interface.type][typ] = self
break
......
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE.Framework.InputData.IInputData import IInputData
class EmptyData(IInputData):
type = "empty_data"
extension = None
count = 0
def __init__(self, filename, *args, **kwargs):
IInputData.__init__(self, filename, *args, **kwargs)
self._data = None
if filename is None:
self._filename = self._basename = 'empty_' + str(EmptyData.count)
EmptyData.count += 1
else:
self._filename = self._basename = filename
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE import REGISTRY
from MDANSE.Core.Error import Error
class InputDataError(Error):
pass
class IInputData(object):
__metaclass__ = REGISTRY
type = "input_data"
def __init__(self, *args, **kwargs):
self._data = None
self._filename = None
@property
def filename(self):
return self._filename
@property
def basename(self):
return self._basename
@property
def data(self):
return self._data
def info(self):
return "No information available"
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
import abc
import os
from MDANSE.Framework.InputData.IInputData import IInputData
class InputFileData(IInputData):
def __init__(self, filename, *args, **kwargs):
super(InputFileData, self).__init__()
self._filename = filename
self._dirname = os.path.dirname(self._filename)
self._basename = os.path.basename(self._filename)
self._data = None
self.load()
@property
def dirname(self):
return self._dirname
@abc.abstractmethod
def load(self):
pass
@abc.abstractmethod
def close(self):
pass
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
import numpy
from MDANSE.Framework.InputData.IInputData import InputDataError
from MDANSE.Framework.InputData.InputFileData import InputFileData
from MDANSE.MolecularDynamics.Trajectory import get_chemical_objects_size, get_chemical_objects_number, MMTKTrajectory
class MMTKTrajectoryInputData(InputFileData):
type = "mmtk_trajectory"
extension = "nc"
def load(self):
try:
traj = MMTKTrajectory(None, self._filename, "r")
except IOError:
raise InputDataError("The MMTK trajectory %r could not be loaded property." % self._filename)
self._data = traj
def close(self):
self._data.close()
def info(self):
val = []
val.append("Path:")
val.append("%s\n" % self._filename)
val.append("Number of steps:")
val.append("%s\n" % len(self._data))
val.append("Universe:")
val.append("%s\n" % self._data.universe)
if self._data.universe.is_periodic:
val.append("Direct cell:")
val.append("%s\n" % str(numpy.round(self._data.universe.basisVectors(),4)))
val.append("Reciprocal cell:")
val.append("%s\n" % str(numpy.round(self._data.universe.reciprocalBasisVectors(),4)))
val.append("Molecular types found:")
molSize = get_chemical_objects_size(self._data.universe)
molNumber = get_chemical_objects_number(self._data.universe)
for k,v in molSize.items():
val.append('\t- %d molecule(s) of %s (%d atoms)' % (molNumber[k],k,v))
val.append('\n')
val = "\n".join(val)
return val
@property
def trajectory(self):
return self._data
@property
def universe(self):
return self._data.universe
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE.Framework.InputData.InputFileData import InputFileData
class MviTraceInputData(InputFileData):
type = "mvi_trace"
extension = "mvi"
def load(self):
pass
def close(self):
pass
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
import collections
from Scientific.IO.NetCDF import NetCDFFile
from MDANSE.Framework.InputData.IInputData import InputDataError
from MDANSE.Framework.InputData.InputFileData import InputFileData
class NetCDFInputData(InputFileData):
type = "netcdf_data"
extension = "nc"
def load(self):
try:
self._netcdf = NetCDFFile(self._filename,"r")
except IOError:
raise InputDataError("The data stored in %r filename could not be loaded property." % self._filename)
else:
self._data = collections.OrderedDict()
variables = self._netcdf.variables
for k in variables:
self._data[k]={}
try :
if vars[k].axis:
self._data[k]['axis'] = variables[k].axis.split('|')
else:
self._data[k]['axis'] = []
except:
self._data[k]['axis'] = []
self._data[k]['data'] = variables[k].getValue()
self._data[k]['units'] = getattr(variables[k], 'units', 'au')
def close(self):
self._netcdf.close()
@property
def netcdf(self):
return self._netcdf
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE.Framework.InputData.IInputData import IInputData
class PeriodicTableData(IInputData):
type = "periodic_table"
extension = None
def load(self):
pass
def close(self):
pass
\ No newline at end of file
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
import collections
import numpy
from MDANSE import REGISTRY
from MDANSE.Core.Error import Error
class OutputVariableError(Error):
pass
class OutputData(collections.OrderedDict):
def add(self, dataName, dataType, data, **kwargs):
self[dataName] = REGISTRY["output_variable"][dataType](data, dataName, **kwargs)
def write(self, basename, formats, header=None):
for fmt in formats:
REGISTRY["format"][fmt].write(basename, self, header)
class IOutputVariable(numpy.ndarray):
'''
Defines a nmoldyn output variable.
A nmoldyn output variable is defined as s subclass of Numpy array that stores additional attributes.
Those extra attributes will be contain information necessary for the the nmoldyn plotter.
'''
__metaclass__ = REGISTRY
type = "output_variable"
def __new__(cls, value, name, axis=(), units="unitless"):
'''
Instanciate a new nmoldyn output variable.
@param cls: the class to instantiate.
@type cls: an OutputVariable object
@param name: the name of the output variable.
@type name: string
@param value: the input numpy array.
@type value: numpy array
@note: This is the standard implementation for subclassing a numpy array.
Please look at http://docs.scipy.org/doc/numpy/user/basics.subclassing.html for more information.
'''
if isinstance(value, tuple):
value = numpy.zeros(value)
value = numpy.array(value, dtype=numpy.float64)
if value.ndim != cls._nDimensions:
raise OutputVariableError("Invalid number of dimensions for an output variable of type %r" % cls.name)
# Input array is an already formed ndarray instance
# We first cast to be our class type
obj = numpy.asarray(value).view(cls)
# The name of the output variable.
obj.name = name
obj.units = units
obj.axis = axis
return obj
def __array_finalize__(self, obj):
if obj is None:
return
self.name = getattr(obj, 'name', None)
self.axis = getattr(obj, 'axis',())
self.units = getattr(obj, 'units','unitless')
def __array_wrap__(self, out_arr, context=None):
return numpy.ndarray.__array_wrap__(self, out_arr, context)
def info(self):
info = []
info.append("# variable name: %s" % self.name)
info.append("# \ttype: %s" % self.type)
info.append("# \taxis: %s" % str(self.axis))
info.append("# \tunits: %s" % self.units)
info = "\n".join(info)
return info
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE.Framework.OutputVariables.IOutputVariable import IOutputVariable
class LineOutputVariable(IOutputVariable):
type = "line"
_nDimensions = 1
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
'''
Created on Mar 27, 2015
@author: pellegrini
'''
from MDANSE.Framework.OutputVariables.IOutputVariable import IOutputVariable
class SurfaceOutputVariable(IOutputVariable):
type = "surface"
_nDimensions = 2
#MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
#------------------------------------------------------------------------------------------
#Copyright (C)
#2015- Eric C. Pellegrini Institut Laue-Langevin
#BP 156
#6, rue Jules Horowitz
#38042 Grenoble Cedex 9
#France
#pellegrini[at]ill.fr
#goret[at]ill.fr
#aoun[at]ill.fr
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software