Skip to content

Commit

Permalink
check omf file test and code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dehoni committed Jun 21, 2022
1 parent 1e1f3d5 commit 1c6a1c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 60 deletions.
81 changes: 27 additions & 54 deletions src/sas/sascalc/calculator/sas_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,79 +1061,69 @@ def read(self, path):
logging.error(str(exc)+" when processing %r"%line)
elif line:
# Reading Header; Segment count ignored

s_line = line.split(":", 1)
if s_line[0].lower().count("oommf") > 0:
try:
oommf = s_line[1].lstrip()
except IndexError:
s_line = line.split(" ", 1)
oommf = s_line[1].lstrip()
if len(s_line) < 2: s_line = line.split(" ",1)
oommf = s_line[1].strip()

if s_line[0].lower().count("title") > 0:
title = s_line[1].lstrip()
title = s_line[1].strip()
if s_line[0].lower().count("desc") > 0:
desc += s_line[1].lstrip()
desc += s_line[1].strip()
desc += '\n'
if s_line[0].lower().count("meshtype") > 0:
meshtype = s_line[1].lstrip()
meshtype = s_line[1].strip()
if s_line[0].lower().count("meshunit") > 0:
meshunit = s_line[1].lstrip()
meshunit = s_line[1].strip()
if meshunit.count("m") < 1:
msg = "Error: \n"
msg += "We accept only m as meshunit"
logging.error(msg)
return None
if s_line[0].lower().count("xbase") > 0:
xbase = s_line[1].lstrip()
xbase = s_line[1].strip()
if s_line[0].lower().count("ybase") > 0:
ybase = s_line[1].lstrip()
ybase = s_line[1].strip()
if s_line[0].lower().count("zbase") > 0:
zbase = s_line[1].lstrip()
zbase = s_line[1].strip()
if s_line[0].lower().count("xstepsize") > 0:
xstepsize = s_line[1].lstrip()
xstepsize = s_line[1].strip()
if s_line[0].lower().count("ystepsize") > 0:
ystepsize = s_line[1].lstrip()
ystepsize = s_line[1].strip()
if s_line[0].lower().count("zstepsize") > 0:
zstepsize = s_line[1].lstrip()
zstepsize = s_line[1].strip()
if s_line[0].lower().count("xnodes") > 0:
xnodes = s_line[1].lstrip()
#print(s_line[0].lower().count("ynodes"))
xnodes = s_line[1].strip()
if s_line[0].lower().count("ynodes") > 0:
ynodes = s_line[1].lstrip()
#print(ynodes)
ynodes = s_line[1].strip()
if s_line[0].lower().count("znodes") > 0:
znodes = s_line[1].lstrip()
znodes = s_line[1].strip()
if s_line[0].lower().count("xmin") > 0:
xmin = s_line[1].lstrip()
xmin = s_line[1].strip()
if s_line[0].lower().count("ymin") > 0:
ymin = s_line[1].lstrip()
ymin = s_line[1].strip()
if s_line[0].lower().count("zmin") > 0:
zmin = s_line[1].lstrip()
zmin = s_line[1].strip()
if s_line[0].lower().count("xmax") > 0:
xmax = s_line[1].lstrip()
xmax = s_line[1].strip()
if s_line[0].lower().count("ymax") > 0:
ymax = s_line[1].lstrip()
ymax = s_line[1].strip()
if s_line[0].lower().count("zmax") > 0:
zmax = s_line[1].lstrip()
zmax = s_line[1].strip()
if s_line[0].lower().count("valueunit") > 0:
valueunit = s_line[1].lstrip()
valueunit = s_line[1].strip()
if valueunit.count("mT") < 1 and valueunit.count("A/m") < 1:
msg = "Error: \n"
msg += "We accept only mT or A/m as valueunit"
logging.error(msg)
return None
elif valueunit.count("mT") > 1 or valueunit.count("A/m") > 1:
elif "mT" in valueunit or "A/m" in valueunit:
valueunit = valueunit.split(" ", 1)
valueunit = valueunit[0].lstrip()
valueunit = valueunit[0].strip()
if s_line[0].lower().count("valuemultiplier") > 0:
valuemultiplier = s_line[1].lstrip()
valuemultiplier = s_line[1].strip()
else:
valuemultiplier = 1
# if s_line[0].lower().count("valuerangeminmag") > 0:
# valuerangeminmag = s_line[1].lstrip()
# if s_line[0].lower().count("valuerangemaxmag") > 0:
# valuerangemaxmag = s_line[1].lstrip()
if s_line[0].lower().count("end") > 0:
output.filename = os.path.basename(path)
output.oommf = oommf
Expand All @@ -1156,10 +1146,6 @@ def read(self, path):
output.ymax = float(ymax) * METER2ANG
output.zmax = float(zmax) * METER2ANG
output.valuemultiplier = valuemultiplier
# output.valuerangeminmag \
# = mag2sld(float(valuerangeminmag), valueunit)
# output.valuerangemaxmag \
# = mag2sld(float(valuerangemaxmag), valueunit)
mx = np.reshape(mx, (len(mx),))
my = np.reshape(my, (len(my),))
mz = np.reshape(mz, (len(mz),))
Expand Down Expand Up @@ -1427,8 +1413,7 @@ def __init__(self):
self.my = None
self.mz = None
self.valuemultiplier = 1.
# self.valuerangeminmag = 0
# self.valuerangemaxmag = 0


def __str__(self):
"""
Expand Down Expand Up @@ -1461,10 +1446,7 @@ def __str__(self):
_str += "zmax: %s [%s]\n" % (str(self.zmax), self.meshunit)
_str += "valueunit: %s\n" % self.valueunit
_str += "valuemultiplier: %s\n" % str(self.valuemultiplier)
# _str += "ValueRangeMinMag:%s [%s]\n" % (str(self.valuerangeminmag),
# self.valueunit)
# _str += "ValueRangeMaxMag:%s [%s]\n" % (str(self.valuerangemaxmag),
# self.valueunit)

return _str

def set_m(self, mx, my, mz):
Expand Down Expand Up @@ -1525,10 +1507,6 @@ def __init__(self, pos_x, pos_y, pos_z, sld_n=None,
self.sld_mz = sld_mz
self.vol_pix = vol_pix
self.data_length = len(pos_x)
#self.sld_m = None
#self.sld_phi = None
#self.sld_theta = None
#self.sld_phi = None
self.pix_symbol = None
if sld_mx is not None and sld_my is not None and sld_mz is not None:
self.set_sldms(sld_mx, sld_my, sld_mz)
Expand Down Expand Up @@ -1596,11 +1574,6 @@ def set_sldms(self, sld_mx, sld_my, sld_mz):
else:
self.sld_mz = sld_mz

#sld_m = np.sqrt(sld_mx**2 + sld_my**2 + sld_mz**2)
#if isinstance(sld_m, float):
# self.sld_m = np.full_like(self.pos_x, sld_m)
#else:
# self.sld_m = sld_m

def set_pixel_symbols(self, symbol='pixel'):
"""
Expand Down
16 changes: 10 additions & 6 deletions test/sascalculator/utest_sas_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from sas.sascalc.calculator import sas_gen

MFACTOR_AM = 2.90636E-12


def find(filename):
return os.path.join(os.path.dirname(__file__), 'data', filename)
Expand Down Expand Up @@ -72,9 +74,10 @@ def test_omfreader_V1(self):
"""
f = self.omfloader.read(find("isolated_skyrmion_V1.omf"))

self.assertEqual(f.sld_mx[0], 505613.032564973)
self.assertEqual(f.sld_my[0], -505613.032564973)
self.assertEqual(f.sld_mz[0], 835889.300446479)

self.assertEqual(f.sld_mx[0], MFACTOR_AM * 505613.032564973)
self.assertEqual(f.sld_my[0], - MFACTOR_AM * 505613.032564973)
self.assertEqual(f.sld_mz[0], MFACTOR_AM * 835889.300446479)
self.assertEqual(f.pos_x[0], 0.0)
self.assertEqual(f.pos_y[0], 0.0)
self.assertEqual(f.pos_z[0], 0.0)
Expand All @@ -85,9 +88,10 @@ def test_omfreader_V2(self):
"""
f = self.omfloader.read(find("isolated_skyrmion_V2.omf"))

self.assertEqual(f.sld_mx[0], 505613.032564973)
self.assertEqual(f.sld_my[0], -505613.032564973)
self.assertEqual(f.sld_mz[0], 835889.300446479)

self.assertEqual(f.sld_mx[0], MFACTOR_AM * 505613.032564973)
self.assertEqual(f.sld_my[0], - MFACTOR_AM * 505613.032564973)
self.assertEqual(f.sld_mz[0], MFACTOR_AM * 835889.300446479)
self.assertEqual(f.pos_x[0], 0.0)
self.assertEqual(f.pos_y[0], 0.0)
self.assertEqual(f.pos_z[0], 0.0)
Expand Down

0 comments on commit 1c6a1c8

Please sign in to comment.