diff --git a/pyvnt/Reference/tensor.py b/pyvnt/Reference/tensor.py index f6b3161..e0990ad 100644 --- a/pyvnt/Reference/tensor.py +++ b/pyvnt/Reference/tensor.py @@ -2,6 +2,7 @@ from pyvnt.Reference.errorClasses import InvalidTupleError from pyvnt.Reference.vector import PropertyVector import numpy as np +from typing import Self class PropertyTensor(ValueProperty): ''' @@ -29,7 +30,7 @@ def __init__(self, name, value: [PropertyFloat]): for i in range(3): for j in range(3): - inputs.append((i, j, value[i*3+j])) + inputs.append((i+1, j+1, value[i*3+j])) self.setProperties(name, *inputs) # TODO: Implement matrix operations for tensors, refer to the OpenFOAM team for details @@ -83,73 +84,73 @@ def xx(self) -> PropertyFloat: ''' Returns the xx value of the tensor ''' - return self.__values[0][0] + return self.__values[0][0].giveVal() def xy(self) -> PropertyFloat: ''' Returns the xy value of the tensor ''' - return self.__values[0][1] + return self.__values[0][1].giveVal() def xz(self) -> PropertyFloat: ''' Returns the xz value of the tensor ''' - return self.__values[0][2] + return self.__values[0][2].giveVal() def yx(self) -> PropertyFloat: ''' Returns the yx value of the tensor ''' - return self.__values[1][0] + return self.__values[1][0].giveVal() def yy(self) -> PropertyFloat: ''' Returns the yy value of the tensor ''' - return self.__values[1][1] + return self.__values[1][1].giveVal() def yz(self) -> PropertyFloat: ''' Returns the yz value of the tensor ''' - return self.__values[1][2] + return self.__values[1][2].giveVal() def zx(self) -> PropertyFloat: ''' Returns the zx value of the tensor ''' - return self.__values[2][0] + return self.__values[2][0].giveVal() def zy(self) -> PropertyFloat: ''' Returns the zy value of the tensor ''' - return self.__values[2][1] + return self.__values[2][1].giveVal() def zz(self) -> PropertyFloat: ''' Returns the zz value of the tensor ''' - return self.__values[2][2] + return self.__values[2][2].giveVal() def row(self, r: int) -> PropertyVector: ''' Returns the row vector of the tensor Parameters: - r: The row number of the tensor + r: The row number of the tensor in 1 based indexing ''' - return PropertyVector(self._ValueProperty__name + "_row", self.__values[r][0], self.__values[r][1], self.__values[r][2]) + return PropertyVector(self._ValueProperty__name + "_row", self.__values[r - 1][0], self.__values[r - 1][1], self.__values[r - 1][2]) def col(self, c: int) -> PropertyVector: ''' Returns the column vector of the tensor Parameters: - c: The column number of the tensor + c: The column number of the tensor in 1 based indexing ''' - return PropertyVector(self._ValueProperty__name + "_col", self.__values[0][c], self.__values[1][c], self.__values[2][c]) + return PropertyVector(self._ValueProperty__name + "_col", self.__values[0][c - 1], self.__values[1][c - 1], self.__values[2][c - 1]) def diag(self) -> PropertyVector: ''' @@ -157,7 +158,7 @@ def diag(self) -> PropertyVector: ''' return PropertyVector(self._ValueProperty__name + "_diag", self.__values[0][0], self.__values[1][1], self.__values[2][2]) - def T(self) -> PropertyTensor: + def T(self) -> Self: ''' Return non-Hermitian transpose of the tensor ''' @@ -165,7 +166,7 @@ def T(self) -> PropertyTensor: self.__values[0][1], self.__values[1][1], self.__values[2][1], self.__values[0][2], self.__values[1][2], self.__values[2][2]]) - def inv(self) -> PropertyTensor: + def inv(self) -> Self: ''' Returns the inverse of the tensor ''' @@ -179,7 +180,7 @@ def inv(self) -> PropertyTensor: res[1][0], res[1][1], res[1][2], res[2][0], res[2][1], res[2][2]]) - def inner(self, t: PropertyTensor) -> PropertyTensor: + def inner(self, t: Self) -> Self: ''' Returns the inner product of the tensor with another tensor ''' @@ -195,7 +196,7 @@ def inner(self, t: PropertyTensor) -> PropertyTensor: self.zx()*t.xy() + self.zy()*t.yy() + self.zz()*t.zy(), self.zx()*t.xz() + self.zy()*t.yz() + self.zz()*t.zz()]) - def schur(self, t: PropertyTensor) -> PropertyTensor: + def schur(self, t: Self) -> Self: ''' Returns the Schur product of the tensor with another tensor ''' @@ -203,6 +204,24 @@ def schur(self, t: PropertyTensor) -> PropertyTensor: self.yx()*t.yx(), self.yy()*t.yy(), self.yz()*t.yz(), self.zx()*t.zx(), self.zy()*t.zy(), self.zz()*t.zz()]) + def giveVal(self): + ''' + Returns the tensor value + ''' + res = (self.xx(), self.xy(), self.xz(), + self.yx(), self.yy(), self.yz(), + self.zx(), self.zy(), self.zz()) + return res + + def writeOut(self, file): + ''' + Writes the tensor value to a file + + Parameters: + file: The file object to write the value to + ''' + file.write(f"{self.giveVal()}") + def __repr__(self): return f"PropertyTensor(name = {self._ValueProperty__name}, xx = {self.xx()}, xy = {self.xy()}, xz = {self.xz()}, yx = {self.yx()}, yy = {self.yy()}, yz = {self.yz()}, zx = {self.zx()}, zy = {self.zy()}, zz = {self.zz()})" diff --git a/pyvnt/__init__.py b/pyvnt/__init__.py index e147810..af6fd89 100755 --- a/pyvnt/__init__.py +++ b/pyvnt/__init__.py @@ -1,4 +1,6 @@ from pyvnt.Reference.basic import * +from pyvnt.Reference.vector import * +from pyvnt.Reference.tensor import * from pyvnt.DictionaryElement.foamDS import * from pyvnt.DictionaryElement.keyData import * from pyvnt.Converter.Writer.writer import * diff --git a/tests/test_tensor.py b/tests/test_tensor.py new file mode 100644 index 0000000..cc72b59 --- /dev/null +++ b/tests/test_tensor.py @@ -0,0 +1,60 @@ +from pyvnt import * + +class TestTensor: + def setup_method(self, method): + self.hprop1 = PropertyFloat('val1', default=1) + self.hprop2 = PropertyFloat('val2', default=2) + self.hprop3 = PropertyFloat('val3', default=3) + self.hprop4 = PropertyFloat('val4', default=4) + self.hprop5 = PropertyFloat('val5', default=5) + self.hprop6 = PropertyFloat('val6', default=6) + self.hprop7 = PropertyFloat('val7', default=7) + self.hprop8 = PropertyFloat('val8', default=8) + self.hprop9 = PropertyFloat('val9', default=9) + + self.tprop1 = PropertyTensor('val1', [self.hprop1, self.hprop2, self.hprop3, self.hprop4, self.hprop5, self.hprop6, self.hprop7, self.hprop8, self.hprop9]) + self.tprop2 = PropertyTensor('val2', [self.hprop4, self.hprop5, self.hprop6, self.hprop7, self.hprop8, self.hprop9, self.hprop1, self.hprop2, self.hprop3]) + + def teardown_method(self, method): + del self.tprop1 + del self.tprop2 + del self.hprop1 + del self.hprop2 + del self.hprop3 + del self.hprop4 + del self.hprop5 + del self.hprop6 + del self.hprop7 + del self.hprop8 + del self.hprop9 + + def test_val_returns(self): + assert self.tprop1.xx() == 1 + assert self.tprop1.xy() == 2 + assert self.tprop1.xz() == 3 + + assert self.tprop1.yx() == 4 + assert self.tprop1.yy() == 5 + assert self.tprop1.yz() == 6 + + assert self.tprop1.zx() == 7 + assert self.tprop1.zy() == 8 + assert self.tprop1.zz() == 9 + + def test_tensor_print(self): + assert str(self.tprop1) == f"PropertyTensor(name = val1, xx = 1, xy = 2, xz = 3, yx = 4, yy = 5, yz = 6, zx = 7, zy = 8, zz = 9)" + assert str(self.tprop2) == f"PropertyTensor(name = val2, xx = 4, xy = 5, xz = 6, yx = 7, yy = 8, yz = 9, zx = 1, zy = 2, zz = 3)" + + def test_row(self): + assert str(self.tprop1.row(1)) == f"PropertyVector(name = val1_row, x = 1, y = 2, z = 3)" + assert str(self.tprop1.row(2)) == f"PropertyVector(name = val1_row, x = 4, y = 5, z = 6)" + assert str(self.tprop1.row(3)) == f"PropertyVector(name = val1_row, x = 7, y = 8, z = 9)" + + def test_col(self): + assert str(self.tprop1.col(1)) == f"PropertyVector(name = val1_col, x = 1, y = 4, z = 7)" + assert str(self.tprop1.col(2)) == f"PropertyVector(name = val1_col, x = 2, y = 5, z = 8)" + assert str(self.tprop1.col(3)) == f"PropertyVector(name = val1_col, x = 3, y = 6, z = 9)" + + def test_giveVal(self): + assert self.tprop1.giveVal() == (1, 2, 3, 4, 5, 6, 7, 8, 9) + assert self.tprop2.giveVal() == (4, 5, 6, 7, 8, 9, 1, 2, 3) \ No newline at end of file