Source code for Muscat.FE.Fields.FieldBase

# -*- 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 Muscat.Containers.Mesh import Mesh

unaryOps = {"__neg__":np.negative,
            "__abs__":np.abs}

binaryOps = {"__add__":np.add,
             "__mul__":np.multiply,
             "__pow__":np.power,
             "__sub__":np.subtract,
             "__rmul__":np.multiply,
             "__div__":np.divide,
             "__truediv__":np.divide,
             "__isub__":np.ndarray.__isub__,
             "__iadd__":np.ndarray.__iadd__,
#             "__itruediv__":np.ndarray.__idiv__,

             "__gt__":np.ndarray.__gt__,
             "__ge__":np.ndarray.__ge__,
             "__lt__":np.ndarray.__lt__,
             "__le__":np.ndarray.__le__,
             "__eq__":np.ndarray.__eq__,
             "__ne__":np.ndarray.__ne__,

             }


[docs]class FieldBase(): def __init__(self,name=None,mesh=None): super().__init__() if name is None: self.name = "" else: self.name = name self.mesh: Mesh = mesh
[docs] def GetName(self): return self.name
[docs] def SetName(self,name): self.name = name
[docs] def ConvertDataForNativeTreatment(self): self.data = self.unaryOp(np.ascontiguousarray).data
def __neg__(self): return self.unaryOp(np.negative) def __abs__(self): return self.unaryOp(np.abs) def __add__(self,other): return self.binaryOp(other,binaryOps["__add__"]) def __radd__(self,other): return self.binaryOp(other,binaryOps["__add__"]) def __mul__(self,other): return self.binaryOp(other,binaryOps["__mul__"]) def __rmul__(self,other): return self.binaryOp(other,binaryOps["__mul__"]) def __div__(self,other): return self.binaryOp(other,binaryOps["__div__"]) def __pow__(self,other): return self.binaryOp(other,binaryOps["__pow__"]) def __gt__(self,other): return self.binaryOp(other,binaryOps["__gt__"]) def __ge__(self,other): return self.binaryOp(other,binaryOps["__ge__"]) def __lt__(self,other): return self.binaryOp(other,binaryOps["__lt__"]) def __le__(self,other): return self.binaryOp(other,binaryOps["__le__"]) def __eq__(self,other): return self.binaryOp(other,binaryOps["__eq__"]) def __ne__(self,other): return self.binaryOp(other,binaryOps["__ne__"]) def __sub__(self,other): return self.binaryOp(other,binaryOps["__sub__"]) def __rsub__(self,other): res = -self res += other return res def __iadd__(self,other): res = self.binaryOp(other,binaryOps["__iadd__"]) res.name = self.name return res def __isub__(self,other): res = self.binaryOp(other,binaryOps["__isub__"]) res.name = self.name return res def __imul__(self,other): self.data = self.binaryOp(other,binaryOps["__mul__"]).data return self def __truediv__(self,other): return self.binaryOp(other,binaryOps["__truediv__"]) def __bool__(self): raise ValueError("The truth value of a field is ambiguous.") def __getattr__(self,name): op = getattr(np,name,None) if op is None: raise(AttributeError(str(type(self)) + " does not have the '"+str(name)+"' attribute.")) return def newfunc(): res = self.unaryOp(op) return res return newfunc
[docs]def CheckIntegrity(GUI:bool=False): obj = FieldBase("temp") print(obj) return "ok"
if __name__ == '__main__': print(CheckIntegrity(True))# pragma: no cover