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

Vector #5

Merged
merged 2 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
4 changes: 2 additions & 2 deletions manual_tests/test_pyvnt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# test for KeyData and Foam classes

key1 = KeyData('solver', prop1, prop2)
print(key1.giveVal())
# print(key1.giveVal())
head = Foam("test_head",None, None)

# head.appendDict(args)
Expand All @@ -33,7 +33,7 @@
# key1 = KeyData('solver', prop1)
# print(key1)

writeTo(head, 'testFile.txt')
# writeTo(head, 'testFile.txt')



27 changes: 22 additions & 5 deletions pyvnt/Reference/vector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pyvnt.Reference.basic import *
from typing import Self
import math

class PropertyVector(ValueProperty):
'''
Expand All @@ -11,7 +13,7 @@ class PropertyVector(ValueProperty):
z: PropertyFloat object to store z value of the vector
'''

__slots__ = ('_PropertyVector__name', '_PropertyVector__x', '_PropertyVector__y', '_PropertyVector__z')
__slots__ = ('_ValueProperty__name', '_PropertyVector__x', '_PropertyVector__y', '_PropertyVector__z')

def instance_restricted(self):
pass
Expand Down Expand Up @@ -68,7 +70,7 @@ def magnitude(self) -> float:
'''
return math.sqrt(self.__x.giveVal()**2 + self.__y.giveVal()**2 + self.__z.giveVal()**2)

def normalise(self, tol: PropertyFloat) -> PropertyVector:
def normalise(self, tol: PropertyFloat) -> Self:
'''
Normalises the vector

Expand All @@ -77,10 +79,25 @@ def normalise(self, tol: PropertyFloat) -> PropertyVector:
'''
s = self.magnitude()
if s < tol.giveVal():
self.setProperties(self.__name, PropertyFloat(self.__name + "_x", 0), PropertyFloat(self.__name + "_y", 0), PropertyFloat(self.__name + "_z", 0))
self.setProperties(self._ValueProperty__name, PropertyFloat(self._ValueProperty__name + "_x", 0), PropertyFloat(self._ValueProperty__name + "_y", 0), PropertyFloat(self._ValueProperty__name + "_z", 0))
else:
self.setProperties(self.__name, PropertyFloat(self.__name + "_x", self.__x.giveVal()/s), PropertyFloat(self.__name + "_y", self.__y.giveVal()/s), PropertyFloat(self.__name + "_z", self.__z.giveVal()/s))
self.setProperties(self._ValueProperty__name, PropertyFloat(self._ValueProperty__name + "_x", self.__x.giveVal()/s), PropertyFloat(self._ValueProperty__name + "_y", self.__y.giveVal()/s), PropertyFloat(self._ValueProperty__name + "_z", self.__z.giveVal()/s))
return self

def giveVal(self):
'''
Returns the vector value
'''

res = (self.__x.giveVal(), self.__y.giveVal(), self.__z.giveVal())

return res

def writeOut(self, file):
'''
Returns the vector value in a string format
'''
file.write( f"({self.__x.giveVal()} {self.__y.giveVal()} {self.__z.giveVal()})")

def __repr__(self):
return f"PropertyVector(name = {self.__name}, x = {self.__x.giveVal()}, y = {self.__y.giveVal()}, z = {self.__z.giveVal()})"
return f"PropertyVector(name = {self._ValueProperty__name}, x = {self.__x.giveVal()}, y = {self.__y.giveVal()}, z = {self.__z.giveVal()})"
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
17 changes: 0 additions & 17 deletions testFile.txt

This file was deleted.

56 changes: 56 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest

from pyvnt import *

class TestVector:
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.vprop1 = PropertyVector('val1', self.hprop1, self.hprop2, self.hprop3)
self.vprop2 = PropertyVector('val2', self.hprop4, self.hprop5, self.hprop6)

def teardown_method(self, method):
del self.vprop1
del self.vprop2
del self.hprop1
del self.hprop2
del self.hprop3
del self.hprop4
del self.hprop5
del self.hprop6

def test_vector_print(self):
assert str(self.vprop1) == f"PropertyVector(name = val1, x = {self.hprop1.giveVal()}, y = {self.hprop2.giveVal()}, z = {self.hprop3.giveVal()})"
assert str(self.vprop2) == f"PropertyVector(name = val2, x = {self.hprop4.giveVal()}, y = {self.hprop5.giveVal()}, z = {self.hprop6.giveVal()})"

def test_vector_x(self):
assert self.vprop1.x() == 1
assert self.vprop2.x() == 4

def test_vector_y(self):
assert self.vprop1.y() == 2
assert self.vprop2.y() == 5

def test_vector_z(self):
assert self.vprop1.z() == 3
assert self.vprop2.z() == 6

def test_vector_magnitude(self):
assert self.vprop1.magnitude() == 14**0.5
assert self.vprop2.magnitude() == 77**0.5

def test_vector_normalise(self):
self.vprop1.normalise(PropertyFloat('tol', 0.1))
assert self.vprop1.x() == 1/14**0.5
assert self.vprop1.y() == 2/14**0.5
assert self.vprop1.z() == 3/14**0.5

def test_vector_giveVal(self):
assert self.vprop1.giveVal() == (1, 2, 3)
assert self.vprop2.giveVal() == (4, 5, 6)
Loading