Source code for BasicTools.FE.KR.KRBase

# -*- coding: utf-8 -*-
#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.
#

import numpy as np

from BasicTools.Helpers.BaseOutputObject import BaseOutputObject as BOO
from BasicTools.Linalg.ConstraintsHolder import ConstraintsHolder
from BasicTools.Linalg.Transform import Transform


[docs]class KRBase(BOO): def __init__(self): super(KRBase,self).__init__() self.args = [] self.on = []
[docs] def AddArg(self,name): self.args.append(name) return self
[docs] def On(self,zone): if type(zone) is list: self.on.extend(zone) else: self.on.append(zone) self.on = list(set(self.on)) return self
def _GetConstraintHolder(self,CH): if CH is None: return ConstraintsHolder() return CH def _ComputeOffsets(self, fields): totalNumberOfDofsI = 0 offsets = [] fieldOffsets = { } for field in fields: offsets.append(totalNumberOfDofsI) fieldOffsets[field.name] = totalNumberOfDofsI totalNumberOfDofsI += field.numbering["size"] return offsets, fieldOffsets, totalNumberOfDofsI
[docs]class KRBaseScalar(KRBase): def __init__(self): super(KRBaseScalar,self).__init__() self.value = lambda x: 0.0
[docs] def SetFunction(self,func): self.value = func return self
[docs] def SetValue(self,val): self.SetFunction(lambda x: val) return self
[docs]class KRBaseVector(KRBase): def __init__(self): super(KRBaseVector,self).__init__() self.blockDirections = [False,False,False] self.constraintDiretions = "Target" # ["Global","Local","Origin","Target","Normal"] self.originSystem = Transform() self.targetSystem = Transform()
[docs] def Fix(self,direc,val=True): self.blockDirections[direc] = val return self
[docs] def Fix0(self,val=True): self.blockDirections[0] = val return self
[docs] def Fix1(self,val=True): self.blockDirections[1] = val return self
[docs] def Fix2(self,val=True): self.blockDirections[2] = val return self
[docs] def GetConstrainedDirections(self,pos=None,direction=None): res = [] for x,y in zip([0,1,2],self.blockDirections ): if y: res.append(self.GetDirections(x,pos,direction)) return res
[docs] def GetDirections(self,i,pos=None,direction=None): if self.constraintDiretions == "Global": res = np.zeros(3) res[i] = 1 return res elif self.constraintDiretions == "Origin": return self.originSystem.GetOrthoNormalBase().GetDirection(i,pos,direction) elif self.constraintDiretions == "Target": return self.targetSystem.GetOrthoNormalBase().GetDirection(i,pos,direction) elif self.constraintDiretions == "Local": if i == 0: return direction / np.linalg.norm(direction) #Create a local base based on the direction vector # need a second vector to generate a connsistent base(???) raise Exception("Error! 'Local' constraintDiretions not well formed ") else: raise Exception("Error! constraintDiretions not well formed ")
[docs]def CheckIntegrity(GUI=False): obj = KRBaseScalar() obj.AddArg("u").On("Z0") obj.SetValue(1.0) obj = KRBaseVector() obj.AddArg("u").On("Z0").Fix0().Fix1(False).Fix2(True) return "ok"
if __name__ == '__main__': print(CheckIntegrity(GUI=True))