# -*- 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.
#
from typing import Union
import numpy as np
from Muscat.LinAlg.ConstraintsHolder import ConstraintsHolder
from Muscat.LinAlg.Transform import Transform
from Muscat.MeshContainers.Filters.FilterObjects import ElementFilter
from Muscat.MeshContainers.Filters.FilterOperators import UnionFilter, FilterLike
[docs]
class KRBase:
def __init__(self):
super().__init__()
self.args = []
self.on = []
[docs]
def AddArg(self, name):
self.args.append(name)
return self
[docs]
def On(self, zone: Union[list[str],str, FilterLike]):
"""Add a domain to apply the constraint
Parameters
----------
zone : Union[list[str],str, FilterLike]
can be a etag, a list of etag, or a filter to select elements
Returns
-------
type(self)
so the user can chain all to this instance
"""
if type(zone) is list:
self.on.extend(zone)
else:
self.on.append(zone)
self.on = list(set(self.on))
return self
def _GetOnElementFilter(self) -> FilterLike:
"""Create a filter with the data from on
Returns
-------
FilterLike
the filter to select the elements to treat
"""
eTags = []
filters = []
for obj in self.on:
if isinstance(obj, str):
eTags.append(obj)
else:
filters.append(obj)
if len(eTags):
filters.append(ElementFilter(eTag=eTags))
return UnionFilter(filters=filters)
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().__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().__init__()
self.blockDirections = [False, False, False]
self.constraintDirections = "Target" # ["Global","Local","Origin","Target"]
self.originSystem = Transform()
self.targetSystem = Transform()
[docs]
def Block(self, directions, val=True):
self.blockDirections[directions] = val
return self
[docs]
def BlockDirection0(self, val=True):
self.blockDirections[0] = val
return self
[docs]
def BlockDirection1(self, val=True):
self.blockDirections[1] = val
return self
[docs]
def BlockDirection2(self, val=True):
self.blockDirections[2] = val
return self
[docs]
def Fix(self, directions, val=True):
"""This function will be deprecated please use Block"""
return self.Block(directions, val=val)
[docs]
def Fix0(self, val=True):
"""This function will be deprecated please use BlockDirection0"""
return self.BlockDirection0(val=val)
[docs]
def Fix1(self, val=True):
"""This function will be deprecated please use BlockDirection1"""
return self.BlockDirection1(val=val)
[docs]
def Fix2(self, val=True):
"""This function will be deprecated please use BlockDirection2"""
return self.BlockDirection2(val=val)
[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.constraintDirections == "Global":
res = np.zeros(3)
res[i] = 1
return res
elif self.constraintDirections == "Origin":
return self.originSystem.GetOrthoNormalBase().GetDirection(i, pos, direction)
elif self.constraintDirections == "Target":
return self.targetSystem.GetOrthoNormalBase().GetDirection(i, pos, direction)
elif self.constraintDirections == "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 consistent base(???)
raise ValueError("Error! 'Local' constraintDirections not well formed ") # pragma: no cover
else:
raise ValueError(f"Error! constraintDirections {self.constraintDirections} not well formed ") # pragma: no cover
[docs]
def CheckIntegrity(GUI: bool = False):
obj = KRBaseScalar()
obj.AddArg("u").On(["Z0", "Z1"])
obj.SetValue(1.0)
obj = KRBaseVector()
obj.AddArg("u").On("Z0").Fix0().Fix1(False).Fix2(True).Block(0, True)
for corrdSys in ["Global", "Origin", "Target"]:
obj.constraintDirections = corrdSys
obj.GetConstrainedDirections()
return "ok"
if __name__ == "__main__":
print(CheckIntegrity(GUI=True)) # pragma: no cover