Skip to content

Commit

Permalink
Created static "free" and "fixed" constraints.
Browse files Browse the repository at this point in the history
"Fixed" had previously been a subclass of "Displacement", while a free
constraint was represented by None.  None is an insufficiently explicit way of
representing free, so that was changed to a Force constraint with zero
strength.  And for both free and fixed, there's no reason to have multiple
instances of each.
  • Loading branch information
randyheydon committed Jan 12, 2012
1 parent 187f2b0 commit 0b6d38e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
4 changes: 2 additions & 2 deletions febabel/_formats/cnfg.py
Expand Up @@ -201,9 +201,9 @@ def read(self, filename):
# Add the appropriate constraint at the current time for each DOF.
for switch,constr in zip(switches, constr_string.split(SEPCHAR)):
if 'free' in constr:
switch.points[step_start] = None
switch.points[step_start] = con.free
elif 'fixed' in constr:
switch.points[step_start] = con.Fixed()
switch.points[step_start] = con.fixed
elif 'force' in constr:
_, lc, m = constr.split(SEPCHAR2)
switch.points[step_start] = con.Force(
Expand Down
10 changes: 6 additions & 4 deletions febabel/common.py
@@ -1,3 +1,7 @@
import febabel as f



class Base(object):
"""The base class for all objects used in FEbabel.
Expand Down Expand Up @@ -40,13 +44,11 @@ class Constrainable(Base):
__slots__ = ['constraints']

def __init__(self, *degrees_of_freedom):
self.constraints = dict( (i,None) for i in degrees_of_freedom )
self.constraints = dict( (i,f.constraints.free) for i in degrees_of_freedom )
# TODO: Prevent new DOFs from being added after the fact.

def get_children(self):
s = set(self.constraints.itervalues())
s.discard(None)
return s
return set(self.constraints.itervalues())



Expand Down
16 changes: 10 additions & 6 deletions febabel/constraints.py
Expand Up @@ -84,12 +84,16 @@ def get_children(self):
return set([self.loadcurve])


class Force(Constraint): pass
class Displacement(Constraint): pass
# FIXME: Single instance of Fixed (and Free?)
class Fixed(Displacement):
def __init__(self):
Displacement.__init__(self, loadcurve_zero, 0)
class Force(Constraint):
def __repr__(self):
return 'free' if self is free else Constraint.__repr__(self)
class Displacement(Constraint):
def __repr__(self):
return 'fixed' if self is fixed else Constraint.__repr__(self)
# Whenever possible, use these instances rather than creating new Force or
# Displacement instances. This will allow for more efficient solutions.
free = Force(loadcurve_zero, 0)
fixed = Displacement(loadcurve_zero, 0)


class SwitchConstraint(Switch, Constraint):
Expand Down
1 change: 0 additions & 1 deletion febabel/problem.py
Expand Up @@ -30,7 +30,6 @@ def get_descendants_sorted(self):
geo.Element: set(),
mat.Material: set(),
Constrainable: set(),
con.Constraint: set(),
con.LoadCurve: set(),
Switch: set(),
None: set()
Expand Down
16 changes: 7 additions & 9 deletions test/test_cnfg.py
Expand Up @@ -110,23 +110,21 @@ def test_read_cnfg(self):
self.assertEqual(m.base.k, 397)

# Check that constraints are applied properly to the rigid bodies.
from febabel.constraints import free, fixed
mtibia = list( p.sets['tf_joint.inp:tibia'] )[0].material
self.assertTrue( isinstance(mtibia.constraints['Rx'],
f.constraints.SwitchConstraint) )
for dof in ('x','y','z','Rx','Ry','Rz'):
self.assertTrue( isinstance(mtibia.constraints[dof].points[0],
f.constraints.Fixed) )
self.assertEqual(mtibia.constraints[dof].points, {0:fixed})

mfemur = list( p.sets['tf_joint.inp:femur'] )[0].material
self.assertTrue( isinstance(mfemur.constraints['Rx'],
f.constraints.SwitchConstraint) )
self.assertEqual(mfemur.constraints['x'].points, {0:None})
self.assertEqual(mfemur.constraints['y'].points, {0:None})
self.assertTrue( isinstance(mfemur.constraints['Rx'].points[0],
f.constraints.Fixed) )
self.assertEqual(mfemur.constraints['Ry'].points, {0:None})
self.assertTrue( isinstance(mfemur.constraints['Rz'].points[0],
f.constraints.Fixed) )
self.assertEqual(mfemur.constraints['x'].points, {0:free})
self.assertEqual(mfemur.constraints['y'].points, {0:free})
self.assertEqual(mfemur.constraints['Rx'].points, {0:fixed})
self.assertEqual(mfemur.constraints['Ry'].points, {0:free})
self.assertEqual(mfemur.constraints['Rz'].points, {0:fixed})

force = mfemur.constraints['z'].points[0]
self.assertTrue( isinstance(force, f.constraints.Force) )
Expand Down
6 changes: 3 additions & 3 deletions test/test_constraints.py
Expand Up @@ -15,7 +15,7 @@ class TestConstraint(unittest.TestCase):
def test_init(self):
force = con.Force(con.loadcurve_ramp, 75)
disp = con.Displacement(con.LoadCurve({0:0, 0.5:50, 1:51}))
fix = con.Fixed()
fix = con.fixed



Expand All @@ -24,9 +24,9 @@ class TestSwitch(unittest.TestCase):

def test_get_active(self):
f1 = con.Displacement(con.loadcurve_ramp, -10)
f2 = con.Fixed()
f2 = con.fixed
f3 = con.Force(con.loadcurve_constant, 100)
f4 = None
f4 = con.free
s = con.SwitchConstraint({0:f1, 1:f2, 1.1:f3, 1.2:f4})
self.assertTrue( s.get_active(-0.2) is None )
self.assertTrue( s.get_active(0) is f1 )
Expand Down
9 changes: 5 additions & 4 deletions test/test_problem.py
Expand Up @@ -37,17 +37,18 @@ def test_descendants(self):
self.assertTrue(isinstance(i, f.geometry.Element))

desc = p.get_descendants()
# 2 Elements, 12 Nodes, 3 Materials (1 base), 1 AxisOrientation.
self.assertEqual(len(desc), 2 + 12 + 3 + 1)
# 2 Elements, 12 Nodes, 3 Materials (1 base), 1 AxisOrientation,
# 1 Constraint (free), 1 LoadCurve (loadcurve_zero).
self.assertEqual(len(desc), 2 + 12 + 3 + 1 + 1 + 1)

desc_s = p.get_descendants_sorted()
self.assertEqual(len(desc_s[f.geometry.Element]), 2)
self.assertEqual(len(desc_s[f.geometry.Node]), 12)
self.assertEqual(len(desc_s[f.materials.Material]), 3)
self.assertEqual(len(desc_s[f.common.Constrainable]), 12)
self.assertEqual(len(desc_s[f.constraints.LoadCurve]), 0)
self.assertEqual(len(desc_s[f.constraints.LoadCurve]), 1)
self.assertEqual(len(desc_s[f.common.Switch]), 0)
self.assertEqual(len(desc_s[None]), 1)
self.assertEqual(len(desc_s[None]), 2)



Expand Down

0 comments on commit 0b6d38e

Please sign in to comment.