Node Loads


In [1]:
import numpy as np
from salib import extend

class NodeLoad


In [2]:
class NodeLoad(object):
    
    def __init__(self,fx=0.,fy=0.,mz=0.):
        if np.isscalar(fx):
            self.forces = np.matrix([fx,fy,mz],dtype=np.float64).T
        else:
            self.forces= fx.copy()
        
    def __mul__(self,scale):
        if scale == 1.0:
            return self
        return self.__class__(self.forces*scale)
    
    __rmul__ = __mul__
        
    def __repr__(self):
        return "{}({},{},{})".format(self.__class__.__name__,*list(np.array(self.forces.T)[0]))

In [3]:
##test:
p = NodeLoad(1,2,3)
p, p*2, 3*p


Out[3]:
(NodeLoad(1.0,2.0,3.0), NodeLoad(2.0,4.0,6.0), NodeLoad(3.0,6.0,9.0))

In [4]:
def makeNodeLoad(data):
    G = data.get
    return NodeLoad(G('FX',0),G('FY',0),G('MZ',0))

In [5]:
##test:
makeNodeLoad({"MZ":17, "FX":3})


Out[5]:
NodeLoad(3.0,0.0,17.0)

Indices

Implement ...


In [6]:
id(NodeLoad)


Out[6]:
2969864076L

In [7]:
@extend
class NodeLoad:
    
    def __getitem__(self,ix):
        return self.forces[ix,0]

In [8]:
##test:
p[0],p[2]


Out[8]:
(1.0, 3.0)

In [9]:
##test:
fx,fy,mz = p
mz


Out[9]:
3.0

In [10]:
##test:
p[[2,1,0]]


Out[10]:
matrix([[ 3.],
        [ 2.],
        [ 1.]])

Properties

Implement properties so that ...


In [11]:
@extend
class NodeLoad:
    
    @property
    def fx(self):
        return self.forces[0,0]
    
    @fx.setter
    def fx(self,v):
        self.forces[0,0] = v
        
    @property
    def fy(self):
        return self.forces[1,0]
    
    @fy.setter
    def fy(self,v):
        self.forces[1,0] = v
        
    @property
    def mz(self):
        return self.forces[2,0]
    
    @mz.setter
    def mz(self,v):
        self.forces[2,0] = v

In [12]:
##test:
p.fx, p.fy, p.mz


Out[12]:
(1.0, 2.0, 3.0)

In [13]:
##test:
p.fx = 2
p.fy *= 3
p.mz -= 4
p


Out[13]:
NodeLoad(2.0,6.0,-1.0)

In [ ]: