Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added changes to Tensor class and added testcases for tensors #4

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions pyvnt/Reference/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
'''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -83,89 +84,89 @@ 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:
'''
Returns the diagonal vector of the tensor
'''
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
'''
return PropertyTensor(self._ValueProperty__name + "_T", [self.__values[0][0], self.__values[1][0], self.__values[2][0],
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
'''
Expand All @@ -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
'''
Expand All @@ -195,14 +196,32 @@ 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
'''
return PropertyTensor(self._ValueProperty__name + "_schur", [self.xx()*t.xx(), self.xy()*t.xy(), self.xz()*t.xz(),
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()})"

Expand Down
2 changes: 2 additions & 0 deletions pyvnt/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down
60 changes: 60 additions & 0 deletions tests/test_tensor.py
Original file line number Diff line number Diff line change
@@ -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)
Loading