Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #327 from simphony/generic-interface-lattice
Browse files Browse the repository at this point in the history
Generic interface lattice
  • Loading branch information
mehdisadeghi committed Nov 7, 2016
2 parents 95afad3 + 956fc85 commit 7751937
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 155 deletions.
3 changes: 2 additions & 1 deletion simphony/cuds/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from .abc_particles import ABCParticles
from .mesh import Mesh
from .mesh_items import Point, Element, Edge, Face, Cell
from .lattice import Lattice, LatticeNode
from .lattice import Lattice
from .lattice_items import LatticeNode
from .particles import Particles
from .particles_items import Particle, Bond
from .model import CUDS
Expand Down
174 changes: 147 additions & 27 deletions simphony/cuds/abc_lattice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from abc import ABCMeta, abstractmethod
from abc import abstractmethod

from simphony.core.cuds_item import CUDSItem
from simphony.cuds.abc_dataset import ABCDataset
from simphony.cuds.utils import deprecated

class ABCLattice(object):

class ABCLattice(ABCDataset):
"""Abstract base class for a lattice.
Attributes
Expand All @@ -17,11 +21,128 @@ class ABCLattice(object):
data : DataContainer
high level CUBA data assigned to lattice
"""
__metaclass__ = ABCMeta

@abstractmethod
def get(self, index):
"""Returns a copy of the node with the given index
Parameters
----------
index : int[3]
node index coordinate
Raises
------
KeyError :
when the node is not in the container.
Returns
-------
object :
A copy of the internally stored info.
"""
return self._get_node(index)

def add(self, iterable):
"""Adds a set of objects from the provided iterable
to the dataset.
Currently not implemented.
"""
raise NotImplementedError()

def update(self, iterable):
"""Updates a set of nodes from the provided iterable.
Takes the indexes of the nodes and searches inside the dataset for
those nodes objects. If the node exists, they are replaced in the
dataset. If any node doesn't exist, it will raise an exception.
Parameters
----------
iterable : iterable of nodes
the nodes that will be replaced.
Raises
------
ValueError :
If any node inside the iterable does not exist.
"""
self._update_nodes(iterable)

def remove(self, index):
"""Removes a set of nodes with the provided indexes
from the dataset.
Currently not implemented.
"""
raise NotImplementedError()

def iter(self, indices=None, item_type=None):
"""Generator method for iterating over the objects of the container.
It can receive any kind of sequence of indices to iterate over
those concrete objects. If nothing is passed as parameter, it will
iterate over all the objects.
Parameters
----------
indices : list of int[3] or None
sequence containing the indices of the objects that will be
iterated. When the indices are provided, then the objects are
returned in the same order the indices are returned by the
iterable.
If indices is None, then all objects are returned by the iterable
and there is no restriction on the order that they are returned.
Yields
------
object : Node
The Node item.
Raises
------
KeyError :
if any of the indices passed as parameters are not in the dataset.
"""
if item_type is not None and item_type != CUDSItem.NODE:
raise ValueError("item_type must be CUDSItem.NODE")

return self._iter_nodes(indices)

def has(self, index):
"""iChecks if an object with the given index already exists
in the dataset.
Not implemented.
"""
raise NotImplementedError()

def has_type(self, item_type):
"""Checks if the specified CUDSItem type is present
in the dataset.
Not implemented
"""
raise NotImplementedError()

def __len__(self):
"""Returns the total number of items in the container.
Returns
-------
count : int
The number of items of item_type in the dataset.
"""
return self.count_of(CUDSItem.NODE)

@deprecated
def get_node(self, index): # pragma: no cover
"""Get the lattice node corresponding to the given index.
"""
Deprecated. Use get() instead.
Get the lattice node corresponding to the given index.
Parameters
----------
Expand All @@ -33,20 +154,28 @@ def get_node(self, index): # pragma: no cover
node : LatticeNode
"""
return self.get(index)

@abstractmethod
@deprecated
def update_nodes(self, nodes): # pragma: no cover
"""Update the corresponding lattice nodes.
"""
Deprecated. Use update() instead.
Update the corresponding lattice nodes.
Parameters
----------
nodes : iterator of LatticeNodes
"""
self.update(nodes)

@abstractmethod
@deprecated
def iter_nodes(self, indices=None): # pragma: no cover
"""Get an iterator over the LatticeNodes described by the indices.
"""
Deprecated. Use iter() instead.
Get an iterator over the LatticeNodes described by the indices.
Parameters
----------
Expand All @@ -62,6 +191,7 @@ def iter_nodes(self, indices=None): # pragma: no cover
An iterator over LatticeNode objects
"""
return self.iter(indices)

@property
def primitive_cell(self):
Expand All @@ -87,23 +217,13 @@ def get_coordinate(self, ind):
self.origin[2] + ind[0]*p1[2] + ind[1]*p2[2] + ind[2]*p3[2])

@abstractmethod
def count_of(self, item_type): # pragma: no cover
""" Return the count of item_type in the container.
def _get_node(self, index): # pragma: no cover
pass

Parameters
----------
item_type : CUDSItem
The CUDSItem enum of the type of the items to return the count of.
Returns
-------
count : int
The number of items of item_type in the container.
Raises
------
ValueError :
If the type of the item is not supported in the current
container.
@abstractmethod
def _update_nodes(self, nodes): # pragma: no cover
pass

"""
@abstractmethod
def _iter_nodes(self, indices=None): # pragma: no cover
pass
115 changes: 49 additions & 66 deletions simphony/cuds/lattice.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
import numpy as np
from simphony.cuds.abc_lattice import ABCLattice

from simphony.core.cuds_item import CUDSItem
from simphony.core.data_container import DataContainer
from simphony.cuds.abc_lattice import ABCLattice
from simphony.cuds.lattice_items import LatticeNode
from simphony.cuds.primitive_cell import PrimitiveCell


class LatticeNode(object):
"""A single node of a lattice.
Attributes
----------
index : tuple of int[3]
node index coordinate
data : DataContainer
"""
def __init__(self, index, data=None):
self.index = index[0], index[1], index[2]

if data is None:
self.data = DataContainer()
else:
self.data = DataContainer(data)


class Lattice(ABCLattice):
"""A Bravais lattice. Stores references to data
containers (node related data).
Expand Down Expand Up @@ -55,7 +38,50 @@ def __init__(self, name, primitive_cell, size, origin):
CUDSItem.NODE: lambda: self._size
}

def get_node(self, index):
def count_of(self, item_type):
""" Return the count of item_type in the container.
Parameters
----------
item_type : CUDSItem
The CUDSItem enum of the type of the items to return
the count of.
Returns
-------
count : int
The number of items of item_type in the container.
Raises
------
ValueError :
If the type of the item is not supported in the current
container.
"""
try:
return np.prod(self._items_count[item_type]())
except KeyError:
error_str = "Trying to obtain count a of non-supported item: {}"
raise ValueError(error_str.format(item_type))

@property
def size(self):
return self._size

@property
def origin(self):
return self._origin

@property
def data(self):
return DataContainer(self._data)

@data.setter
def data(self, value):
self._data = DataContainer(value)

def _get_node(self, index):
"""Get a copy of the node corresponding to the given index.
Parameters
Expand All @@ -73,7 +99,7 @@ def get_node(self, index):
raise IndexError('invalid index: {}'.format(tuple_index))
return LatticeNode(tuple_index, self._dcs[tuple_index])

def update_nodes(self, nodes):
def _update_nodes(self, nodes):
"""Update the corresponding lattice nodes (data copied).
Parameters
Expand All @@ -89,7 +115,7 @@ def update_nodes(self, nodes):
raise IndexError('invalid index: {}'.format(index))
self._dcs[index] = DataContainer(node.data)

def iter_nodes(self, indices=None):
def _iter_nodes(self, indices=None):
"""Get an iterator over the LatticeNodes described by the indices.
Parameters
Expand All @@ -112,49 +138,6 @@ def iter_nodes(self, indices=None):
for index in indices:
yield self.get_node(index)

def count_of(self, item_type):
""" Return the count of item_type in the container.
Parameters
----------
item_type : CUDSItem
The CUDSItem enum of the type of the items to return
the count of.
Returns
-------
count : int
The number of items of item_type in the container.
Raises
------
ValueError :
If the type of the item is not supported in the current
container.
"""
try:
return np.prod(self._items_count[item_type]())
except KeyError:
error_str = "Trying to obtain count a of non-supported item: {}"
raise ValueError(error_str.format(item_type))

@property
def size(self):
return self._size

@property
def origin(self):
return self._origin

@property
def data(self):
return DataContainer(self._data)

@data.setter
def data(self, value):
self._data = DataContainer(value)


def make_cubic_lattice(name, h, size, origin=(0, 0, 0)):
"""Create and return a 3D cubic lattice.
Expand Down

0 comments on commit 7751937

Please sign in to comment.