Skip to content

Commit

Permalink
Added simple test for atomistic case to python API
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz committed Aug 19, 2021
1 parent 29f3b2c commit 3d96f05
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 5 deletions.
44 changes: 39 additions & 5 deletions python/ovf/ovf.py
Expand Up @@ -39,16 +39,37 @@ class ovf_segment(ctypes.Structure):
("bounds_max", ctypes.c_float*3),
("lattice_constant", ctypes.c_float),
("origin", ctypes.c_float*3),
("bravaisa", ctypes.c_float*3),
("bravaisb", ctypes.c_float*3),
("bravaisc", ctypes.c_float*3),
("_bravaisa", ctypes.c_float*3),
("_bravaisb", ctypes.c_float*3),
("_bravaisc", ctypes.c_float*3),
("ncellpoints", ctypes.c_int),
("basis", ctypes.POINTER(ctypes.c_float))
("_basis", ctypes.POINTER(ctypes.c_float))
]

@property
def bravaisa(self):
print("Getting value...")
return [i for i in self._bravaisa]

@property
def bravaisb(self):
print("Getting value...")
return [i for i in self._bravaisb]

@property
def bravaisc(self):
print("Getting value...")
return [i for i in self._bravaisc]

@property
def basis(self):
print("Getting value...")
return [ [self._basis[3*i], self._basis[3*i+1], self._basis[3*i+2]] for i in range(self.ncellpoints)]

def __init__(self, title="", comment="", valuedim=1, valueunits="", valuelabels="", meshtype="", meshunits="",
step_size=[0.0, 0.0, 0.0], bounds_min=[0.0, 0.0, 0.0], bounds_max=[0.0, 0.0, 0.0], lattice_constant=0.0,
origin=[0.0, 0.0, 0.0], pointcount=0, n_cells=[1,1,1]):
origin=[0.0, 0.0, 0.0], pointcount=0, n_cells=[1,1,1],
bravaisa = [0.0, 0.0, 0.0], bravaisb = [0.0, 0.0, 0.0], bravaisc = [0.0, 0.0, 0.0], basis = [[0,0,0]]):

self.title = title.encode('utf-8')
self.comment = comment.encode('utf-8')
Expand All @@ -58,8 +79,21 @@ def __init__(self, title="", comment="", valuedim=1, valueunits="", valuelabels=
self.meshtype = meshtype.encode('utf-8')
self.meshunits = meshunits.encode('utf-8')
self.pointcount = pointcount

self.ncellpoints = len(basis)
self._basis = ctypes.cast( (ctypes.c_float * self.ncellpoints * 3)(), ctypes.POINTER(ctypes.c_float) )

for i in range(self.ncellpoints):
self._basis[3*i] = basis[i][0]
self._basis[3*i + 1] = basis[i][1]
self._basis[3*i + 2] = basis[i][2]

for i in range(3):
self.n_cells[i] = n_cells[i]
self._bravaisa[i] = bravaisa[i]
self._bravaisb[i] = bravaisb[i]
self._bravaisc[i] = bravaisc[i]

self.N = n_cells[0]*n_cells[1]*n_cells[2]
for i in range(3):
self.step_size[i] = step_size[i]
Expand Down
93 changes: 93 additions & 0 deletions python/test/simple.py
Expand Up @@ -90,6 +90,99 @@ def test_write(self):
self.assertTrue( success == ovf.OK )
print("----- ovf test reading done")

def test_atomistic(self):
print("----- ovf test writing atomistic")
with ovf.ovf_file("testfile_atomistic_py.aovf") as ovf_file:
ovf_file.ovf_extension_format = ovf.EXTENSION_FORMAT_AOVF_COMP
data = np.zeros((2, 2, 1, 3), dtype='d')
data[0,1,0,:] = [3.0, 2.0, 1.0]

segment = ovf.ovf_segment(
title="python write test",
comment="more details in this comment...",
meshtype="lattice",
valuedim=3,
n_cells=[1,2,1],
basis = [[0,0,0], [0.2, 0.2, 0.2]],
bravaisa=[1,0,0],
bravaisb=[0,1,0],
bravaisc=[0,0,1]
)

success = ovf_file.write_segment(segment, data)
if success != ovf.OK:
print("write_segment failed: ", ovf_file.get_latest_message())
self.assertTrue( success == ovf.OK )
data[0,1,0,:] = [4.0, 5.0, 6.0]
segment.title = "python append test".encode('utf-8')
success = ovf_file.append_segment(segment, data)
if success != ovf.OK:
print("append_segment failed: ", ovf_file.get_latest_message())
self.assertTrue( success == ovf.OK )
print("----- ovf test writing atomistic done")

print("----- ovf test reading atomistic")
with ovf.ovf_file("testfile_atomistic_py.aovf") as ovf_file:
pass
print("found: ", ovf_file.found)
print("is_ovf: ", ovf_file.is_ovf)
print("version: ", ovf_file.version)
print("n_segments: ", ovf_file.n_segments)
print("extension_format: ", ovf_file.ovf_extension_format)

self.assertTrue( ovf_file.found == True )
self.assertTrue( ovf_file.is_ovf == True )
self.assertTrue( ovf_file.ovf_extension_format == ovf.EXTENSION_FORMAT_AOVF_COMP )

self.assertTrue( ovf_file.version == 1 )
self.assertTrue( ovf_file.n_segments == 2 )
segment = ovf.ovf_segment()
success = ovf_file.read_segment_header(0, segment)
if success != ovf.OK:
print("read_segment_header failed: ", ovf_file.get_latest_message())
self.assertTrue( success == ovf.OK )

print("ncellpoints", segment.ncellpoints)
print("N", segment.N)

self.assertEqual(segment.ncellpoints, 2)
self.assertEqual(segment.N, 4)

print("bravaisa: ", segment.bravaisa)
print("bravaisb: ", segment.bravaisb)
print("bravaisac: ", segment.bravaisc)

self.assertAlmostEqual( segment.bravaisa, [1,0,0] )
self.assertAlmostEqual( segment.bravaisb, [0,1,0] )
self.assertAlmostEqual( segment.bravaisc, [0,0,1] )

print("basis: ", segment.basis)
basis_expected = [[0,0,0], [0.2, 0.2, 0.2]]
[self.assertAlmostEqual( b, be ) for b, be in zip(segment.basis[0], basis_expected[0]) ]
[self.assertAlmostEqual( b, be ) for b, be in zip(segment.basis[1], basis_expected[1]) ]

data_shape = (segment.n_cells[0], segment.n_cells[1], segment.n_cells[2], segment.ncellpoints, 3)
data = np.zeros(data_shape, dtype='f')
print("data shape: ", data_shape)
success = ovf_file.read_segment_data(0, segment, data)
if success != ovf.OK:
print("read_segment_data failed: ", ovf_file.get_latest_message())
print("first segment: ", data[0,1,0,:])
self.assertTrue( success == ovf.OK )

success = ovf_file.read_segment_header(1, segment)
if success != ovf.OK:
print("read_segment_header failed: ", ovf_file.get_latest_message())
self.assertTrue( success == ovf.OK )
data_shape = (segment.n_cells[0], segment.n_cells[1], segment.n_cells[2], segment.ncellpoints, 3)
data = np.zeros(data_shape, dtype='d')
success = ovf_file.read_segment_data(1, segment, data)
if success != ovf.OK:
print("read_segment_data failed: ", ovf_file.get_latest_message())
print("second segment: ", data[0,1,0,:])
self.assertTrue( success == ovf.OK )
print("----- ovf test reading atomistic done")

#########


Expand Down

0 comments on commit 3d96f05

Please sign in to comment.