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 #237 from simphony/feature-material
Browse files Browse the repository at this point in the history
Feature material
  • Loading branch information
nathanfranklin committed Dec 10, 2015
2 parents a1bf079 + 5820655 commit 6f455b7
Show file tree
Hide file tree
Showing 26 changed files with 380 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ uml:
java -jar plantuml.jar "source/uml/" "source/images"
mv source/uml/*.png source/images
@echo
@echo "Coversion finished the uml files are in source/images."
@echo "Conversion finished the uml files are in source/images."
10 changes: 10 additions & 0 deletions doc/source/api/cuds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Pure Python implementation
~mesh.Edge
~mesh.Face
~mesh.Cell
~material.Material
~materials.Materials

.. rubric:: Functions

Expand Down Expand Up @@ -89,3 +91,11 @@ Pure Python implementation
.. automodule:: simphony.cuds.particles
:members:
:undoc-members:

.. automodule:: simphony.cuds.material
:members:
:undoc-members:

.. automodule:: simphony.cuds.materials
:members:
:undoc-members:
1 change: 1 addition & 0 deletions doc/source/cuba.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ PoissonRatio Poisson Ratio
LnOfRestitutionCoefficient Natural Logarithm of the Restitution Coefficient ['DEM'] LN_OF_RESTITUTION_COEFFICIENT 65 [1] double
RollingFriction Rolling Friction coefficient ['DEM'] ROLLING_FRICTION 66 [1] double
VolumeFraction Volume fraction ['FEM', 'FVM'] VOLUME_FRACTION 67 [1] double
Material Material ['ATM', 'DEM', 'FEM', 'FVM', 'LBM', 'SPH', 'VIS'] MATERIAL 68 [1] uuid
========================== ==================================================================== ================================================= ============================= ======== ======= =======
11 changes: 11 additions & 0 deletions doc/source/cuds_definition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ Core items
The CUBA attribute container part of the SimPhoNy core. A dictionary
like object maps :class:`~.CUBA` enum keys to they values. In its
native python implementation it can support all CUBA attributes.

Materials
----------

.. figure:: ./images/material_materials.png

**Figure 4:** UML diagram of Materials and Material.

In the SimPhoNy modeling framework, :class:`~.Materials` and :class:`~.Material`
are used to describe materials and to define common material properties. Each
low-level object (e.g. Point, Particle) can be of a certain material.
Binary file modified doc/source/images/engine.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/source/images/material_materials.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions doc/source/uml/engine.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ abstract class ABCModelingEngine {
BC : DataContainer
CM : DataContainer
SP : DataContainer
materials : Materials
--
{abstract} run()
..
Expand Down
9 changes: 9 additions & 0 deletions doc/source/uml/material.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@startuml
class Material {
uid: uuid.UUID
data: DataContainer
description: str
-- classmethods --
from_material(material: Material): Material {copy}
}
@enduml
4 changes: 4 additions & 0 deletions doc/source/uml/material_materials.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@startuml
!include materials.txt
!include material.txt
@enduml
8 changes: 8 additions & 0 deletions doc/source/uml/materials.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@startuml
class Materials {
add_material(Material)
get_material(uid: uuid.UUID): Materials
update_material(material: Material)
iter_materials(uids: iterable of uuid.UUID {optional}): iterator of Materials {unordered}
}
@enduml
1 change: 1 addition & 0 deletions simphony/core/cuba.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ class CUBA(IntEnum):
LN_OF_RESTITUTION_COEFFICIENT = 65
ROLLING_FRICTION = 66
VOLUME_FRACTION = 67
MATERIAL = 68
9 changes: 8 additions & 1 deletion simphony/core/cuba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,11 @@
name: VolumeFraction
number: 67
shape: [1]
type: double
type: double
- description: Material
domain: [ATM, DEM, FEM, FVM, LBM, SPH, VIS]
key: MATERIAL
name: Material
number: 68
shape: [1]
type: uuid
8 changes: 8 additions & 0 deletions simphony/core/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,12 @@
number=67,
shape=[1],
dtype=numpy.float64),
'MATERIAL': Keyword(
name='Material',
description='Material', # noqa
domain=['ATM', 'DEM', 'FEM', 'FVM', 'LBM', 'SPH', 'VIS'],
key='MATERIAL',
number=68,
shape=[1],
dtype='uuid'),
}
2 changes: 2 additions & 0 deletions simphony/cuds/abc_modeling_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ABCModelingEngine(object): # pragma: no cover
container of attributes related to the computational method
SP : DataContainer
container of attributes related to the system parameters/conditions
materials : Materials
materials related to state data
"""
__metaclass__ = ABCMeta
Expand Down
31 changes: 31 additions & 0 deletions simphony/cuds/material.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import uuid

from simphony.core.data_container import DataContainer


class Material(object):
"""A class that represents a material.
Class describes a material and its data.
Attributes
----------
uid : uuid.UUID
the id of the material. If None, then it is generated
data : DataContainer
data associated with this material
description : str
textual description of the material
"""
def __init__(self, uid=None, data=None, description=""):
self.data = data if data else DataContainer()
self.uid = uid if uid else uuid.uuid4()
self.description = description

@classmethod
def from_material(cls, material):
return cls(
uid=material.uid,
description=material.description,
data=DataContainer(material.data))
99 changes: 99 additions & 0 deletions simphony/cuds/materials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from simphony.cuds.material import Material


class Materials(object):
"""A class that contains materials.
Class manages the materials.
"""
def __init__(self):
self._materials = {}

def add_material(self, material):
"""Add a material
Parameters
----------
material : Material
the material to be added.
Raises
------
ValueError:
If a material with the same uid already exists
"""
try:
self._materials[material.uid] = Material.from_material(material)
except KeyError:
raise ValueError

def remove_material(self, uid):
""" Remove a material
Parameters
----------
uid: uuid.UUID
the id of the material to be deleted
Raises
------
KeyError:
If there is no material with the given uid
"""
del self._materials[uid]

def update_material(self, material):
""" Update a material
Parameters
----------
material : Material
the material to be updated.
Raises
------
ValueError:
If a material with this uid does not exist
"""
self._materials[material.uid] = Material.from_material(material)

def get_material(self, uid):
""" Get the material
Parameters
----------
uid: uuid.UUID
the id of the material to be retrieved.
Returns
-------
material
Raises
------
KeyError:
If there is no material with the given uid
"""
return Material.from_material(self._materials[uid])

def iter_materials(self, uids=None):
""" Returns an iterator over a subset or all of the materials.
Parameters
----------
uids : sequence of uids, optional
uids of specific material to be iterated over. If uids is not
given, then all materials will be iterated over.
"""
if uids is None:
for material in self._materials.itervalues():
yield material
else:
for uid in uids:
yield self.get_material(uid)
66 changes: 66 additions & 0 deletions simphony/cuds/tests/test_materials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest
import uuid
from functools import partial

from simphony.cuds.materials import Materials
from simphony.cuds.material import Material
from simphony.testing.utils import (compare_material,
create_data_container)


class TestMaterials(unittest.TestCase):
"""Test case for Materials class."""

def setUp(self):
self.addTypeEqualityFunc(Material,
partial(compare_material, testcase=self))
self.materials = Materials()
self.example_materials = []
for i in xrange(5):
self.example_materials.append(
Material(description="Material {}".format(i),
data=create_data_container()))

def test_add_get_material(self):
self.materials.add_material(self.example_materials[0])
self.assertEqual(self.materials.get_material(
self.example_materials[0].uid),
self.example_materials[0])

def test_get_missing_material(self):
with self.assertRaises(KeyError):
self.materials.get_material(uuid.uuid4())

def test_remove_missing_material(self):
with self.assertRaises(KeyError):
self.materials.remove_material(uuid.uuid4())

def test_iter_materials_with_ids(self):
# given
material_subset = [material for material in self.example_materials[:2]]
subset_ids = [material.uid for material in material_subset]
for material in self.example_materials:
self.materials.add_material(material)

# when
iterated_with_id_materials = [
material for material in self.materials.iter_materials(
subset_ids)]

# then
for m, r in zip(iterated_with_id_materials, material_subset):
self.assertEqual(m, r)

# when
iterated_all_materials = {material.uid: material for material
in self.materials.iter_materials()}

# then
self.assertEqual(len(iterated_all_materials),
len(self.example_materials))
for material in self.example_materials:
self.assertEqual(material, iterated_all_materials[material.uid])


if __name__ == '__main__':
unittest.main()
5 changes: 3 additions & 2 deletions simphony/io/data_container_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ class Data(tables.IsDescription):
ln_of_restitution_coefficient = tables.Float64Col(pos=63)
rolling_friction = tables.Float64Col(pos=64)
volume_fraction = tables.Float64Col(pos=65)
material = tables.StringCol(pos=66, itemsize=32)


class Record(tables.IsDescription):

index = tables.StringCol(itemsize=32, pos=0)
data = Data()
mask = tables.BoolCol(pos=1, shape=(66,))
mask = tables.BoolCol(pos=1, shape=(67,))


class NoUIDRecord(tables.IsDescription):

data = Data()
mask = tables.BoolCol(pos=1, shape=(66,))
mask = tables.BoolCol(pos=1, shape=(67,))
7 changes: 5 additions & 2 deletions simphony/io/data_container_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import tables

from simphony.io.data_container_description import Record
from simphony.io.data_conversion import (convert_from_file_type,
convert_to_file_type)
from simphony.core.cuba import CUBA
from simphony.core.data_container import DataContainer

Expand Down Expand Up @@ -172,8 +174,9 @@ def _populate(self, row, value):
data = list(row['data'])
for key in value:
if key in positions:
data[positions[key]] = value[key]
data[positions[key]] = convert_to_file_type(value[key], key)
mask[positions[key]] = True

row['mask'] = mask
row['data'] = tuple(data)

Expand All @@ -185,5 +188,5 @@ def _retrieve(self, row):
mask = row['mask']
data = row['data']
return DataContainer({
cuba[index]: data[index]
cuba[index]: convert_from_file_type(data[index], cuba[index])
for index, valid in enumerate(mask) if valid})

0 comments on commit 6f455b7

Please sign in to comment.