python: do not use assert for required checks

Signed-off-by: Thorsten Liebig <liebig@imst.de>
pull/66/head
Thorsten Liebig 2020-01-04 15:58:20 +01:00
parent 9e5dcecd31
commit ebf017441e
4 changed files with 54 additions and 27 deletions

View File

@ -38,8 +38,10 @@ cdef class _nf2ff:
assert len(kw)==0, 'Unknown keyword(s): {}'.format(kw) assert len(kw)==0, 'Unknown keyword(s): {}'.format(kw)
def AnalyseFile(self, e_file, h_file): def AnalyseFile(self, e_file, h_file):
assert os.path.exists(e_file) if not os.path.exists(e_file):
assert os.path.exists(h_file) raise Exception('File "{}" does not exist'.format(e_file))
if not os.path.exists(h_file):
raise Exception('File "{}" does not exist'.format(e_file))
cdef string e_fn = e_file.encode('UTF-8') cdef string e_fn = e_file.encode('UTF-8')
cdef string h_fn = h_file.encode('UTF-8') cdef string h_fn = h_file.encode('UTF-8')
with nogil: with nogil:
@ -49,7 +51,8 @@ cdef class _nf2ff:
def SetMirror(self, mirr_type, ny, pos): def SetMirror(self, mirr_type, ny, pos):
if mirr_type<=0: if mirr_type<=0:
return return
assert mirr_type<3 if not mirr_type<3:
raise Exception('SetMirror: invalid mirror type!')
ny = CheckNyDir(ny) ny = CheckNyDir(ny)
self.thisptr.SetMirror(mirr_type, ny, pos) self.thisptr.SetMirror(mirr_type, ny, pos)

View File

@ -139,16 +139,21 @@ class nf2ff:
fn_e = os.path.join(sim_path, self.e_file + '_{}.h5'.format(n)) fn_e = os.path.join(sim_path, self.e_file + '_{}.h5'.format(n))
fn_h = os.path.join(sim_path, self.h_file + '_{}.h5'.format(n)) fn_h = os.path.join(sim_path, self.h_file + '_{}.h5'.format(n))
if os.path.exists(fn_e) and os.path.exists(fn_h): if os.path.exists(fn_e) and os.path.exists(fn_h):
assert nfc.AnalyseFile(fn_e, fn_h) if not nfc.AnalyseFile(fn_e, fn_h):
raise Exception('CalcNF2FF:: Unable to analyse files!')
nfc.Write2HDF5(fn) nfc.Write2HDF5(fn)
result = nf2ff_results(fn) result = nf2ff_results(fn)
if result.phi is not None: if result.phi is not None:
assert np.abs((result.r-radius)/radius)<1e-6, 'Radius does not match. Did you read an invalid chached result? Try "read_cached=False"' if not np.abs((result.r-radius)/radius)<1e-6:
assert utilities.Check_Array_Equal(np.rad2deg(result.theta), self.theta, 1e-4), 'Theta array does not match. Did you read an invalid chached result? Try "read_cached=False"' raise Exception('Radius does not match. Did you read an invalid chached result? Try "read_cached=False"')
assert utilities.Check_Array_Equal(np.rad2deg(result.phi), self.phi, 1e-4), 'Phi array does not match. Did you read an invalid chached result? Try "read_cached=False"' if not utilities.Check_Array_Equal(np.rad2deg(result.theta), self.theta, 1e-4):
assert utilities.Check_Array_Equal(result.freq, self.freq, 1e-6, relative=True), 'Frequency array does not match. Did you read an invalid chached result? Try "read_cached=False"' raise Exception('Theta array does not match. Did you read an invalid chached result? Try "read_cached=False"')
if not utilities.Check_Array_Equal(np.rad2deg(result.phi), self.phi, 1e-4):
raise Exception('Phi array does not match. Did you read an invalid chached result? Try "read_cached=False"')
if not utilities.Check_Array_Equal(result.freq, self.freq, 1e-6, relative=True):
raise Exception('Frequency array does not match. Did you read an invalid chached result? Try "read_cached=False"')
return result return result
class nf2ff_results: class nf2ff_results:

View File

@ -147,7 +147,8 @@ cdef class openEMS:
Set the coordinate system. 0 --> Cartesian (default), 1 --> cylindrical Set the coordinate system. 0 --> Cartesian (default), 1 --> cylindrical
""" """
assert (val==0 or val==1), 'SetCoordSystem: Invalid coordinate system' if not (val==0 or val==1):
raise Exception('SetCoordSystem: Invalid coordinate system')
if val==0: if val==0:
self.thisptr.SetCylinderCoords(False) self.thisptr.SetCylinderCoords(False)
elif val==1: elif val==1:
@ -164,7 +165,8 @@ cdef class openEMS:
-------- --------
openEMS.SetCylinderCoords openEMS.SetCylinderCoords
""" """
assert len(radii)>0, 'SetMultiGrid: invalid multi grid definition' if not len(radii)>0:
raise Exception('SetMultiGrid: invalid multi grid definition')
grid_str = ','.join(['{}'.format(x) for x in radii]) grid_str = ','.join(['{}'.format(x) for x in radii])
self.thisptr.SetupCylinderMultiGrid(grid_str.encode('UTF-8')) self.thisptr.SetupCylinderMultiGrid(grid_str.encode('UTF-8'))
@ -245,7 +247,8 @@ cdef class openEMS:
:param BC: (8,) array or list -- see options above :param BC: (8,) array or list -- see options above
""" """
assert len(BC)==6 if not len(BC)==6:
raise Exception('Invalid boundary condition size!')
for n in range(len(BC)): for n in range(len(BC)):
if type(BC[n])==int: if type(BC[n])==int:
self.thisptr.Set_BC_Type(n, BC[n]) self.thisptr.Set_BC_Type(n, BC[n])
@ -268,7 +271,8 @@ cdef class openEMS:
-------- --------
openEMS.ports.LumpedPort openEMS.ports.LumpedPort
""" """
assert self.__CSX is not None, 'AddLumpedPort: CSX is not set!' if self.__CSX is None:
raise Exception('AddLumpedPort: CSX is not set!')
port = ports.LumpedPort(self.__CSX, port_nr, R, start, stop, p_dir, excite, **kw) port = ports.LumpedPort(self.__CSX, port_nr, R, start, stop, p_dir, excite, **kw)
edges2grid = kw.get('edges2grid', None) edges2grid = kw.get('edges2grid', None)
if edges2grid is not None: if edges2grid is not None:
@ -288,7 +292,8 @@ cdef class openEMS:
-------- --------
openEMS.ports.WaveguidePort openEMS.ports.WaveguidePort
""" """
assert self.__CSX is not None, 'AddWaveGuidePort: CSX is not set!' if self.__CSX is None:
raise Exception('AddWaveGuidePort: CSX is not set!')
return ports.WaveguidePort(self.__CSX, port_nr, start, stop, p_dir, E_func, H_func, kc, excite, **kw) return ports.WaveguidePort(self.__CSX, port_nr, start, stop, p_dir, E_func, H_func, kc, excite, **kw)
def AddRectWaveGuidePort(self, port_nr, start, stop, p_dir, a, b, mode_name, excite=0, **kw): def AddRectWaveGuidePort(self, port_nr, start, stop, p_dir, a, b, mode_name, excite=0, **kw):
@ -300,7 +305,8 @@ cdef class openEMS:
-------- --------
openEMS.ports.RectWGPort openEMS.ports.RectWGPort
""" """
assert self.__CSX is not None, 'AddRectWaveGuidePort: CSX is not set!' if self.__CSX is None:
raise Exception('AddRectWaveGuidePort: CSX is not set!')
return ports.RectWGPort(self.__CSX, port_nr, start, stop, p_dir, a, b, mode_name, excite, **kw) return ports.RectWGPort(self.__CSX, port_nr, start, stop, p_dir, a, b, mode_name, excite, **kw)
def AddMSLPort(self, port_nr, metal_prop, start, stop, prop_dir, exc_dir, excite=0, **kw): def AddMSLPort(self, port_nr, metal_prop, start, stop, prop_dir, exc_dir, excite=0, **kw):
@ -312,7 +318,8 @@ cdef class openEMS:
-------- --------
openEMS.ports.MSLPort openEMS.ports.MSLPort
""" """
assert self.__CSX is not None, 'AddMSLPort: CSX is not set!' if self.__CSX is None:
raise Exception('AddMSLPort: CSX is not set!')
return ports.MSLPort(self.__CSX, port_nr, metal_prop, start, stop, prop_dir, exc_dir, excite, **kw) return ports.MSLPort(self.__CSX, port_nr, metal_prop, start, stop, prop_dir, exc_dir, excite, **kw)
def CreateNF2FFBox(self, name='nf2ff', start=None, stop=None, **kw): def CreateNF2FFBox(self, name='nf2ff', start=None, stop=None, **kw):
@ -331,7 +338,8 @@ cdef class openEMS:
-------- --------
openEMS.nf2ff.nf2ff openEMS.nf2ff.nf2ff
""" """
assert self.__CSX is not None, 'CreateNF2FFBox: CSX is not set!' if self.__CSX is None:
raise Exception('CreateNF2FFBox: CSX is not set!')
directions = [True]*6 directions = [True]*6
mirror = [0]*6 mirror = [0]*6
BC_size = [0]*6 BC_size = [0]*6
@ -351,13 +359,15 @@ cdef class openEMS:
if start is None or stop is None: if start is None or stop is None:
grid = self.__CSX.GetGrid() grid = self.__CSX.GetGrid()
assert grid.IsValid(), 'Error::CreateNF2FFBox: Grid is invalid' if not grid.IsValid():
raise Exception('Error::CreateNF2FFBox: Grid is invalid')
start = np.zeros(3) start = np.zeros(3)
stop = np.zeros(3) stop = np.zeros(3)
for n in range(3): for n in range(3):
l = grid.GetLines(n) l = grid.GetLines(n)
BC_type = self.thisptr.Get_BC_Type(2*n) BC_type = self.thisptr.Get_BC_Type(2*n)
assert len(l)>(BC_size[2*n]+BC_size[2*n+1]), 'Error::CreateNF2FFBox: not enough lines in some direction' if not len(l)>(BC_size[2*n]+BC_size[2*n+1]):
raise Exception('Error::CreateNF2FFBox: not enough lines in some direction')
start[n] = l[BC_size[2*n]] start[n] = l[BC_size[2*n]]
stop[n] = l[-1*BC_size[2*n+1]-1] stop[n] = l[-1*BC_size[2*n+1]-1]
return nf2ff.nf2ff(self.__CSX, name, start, stop, directions=directions, mirror=mirror, **kw) return nf2ff.nf2ff(self.__CSX, name, start, stop, directions=directions, mirror=mirror, **kw)

View File

@ -105,10 +105,12 @@ class Port(object):
if ref_impedance is not None: if ref_impedance is not None:
self.Z_ref = ref_impedance self.Z_ref = ref_impedance
assert self.Z_ref is not None if self.Z_ref is None:
raise Exception('Port Z_ref should not be None!')
if ref_plane_shift is not None: if ref_plane_shift is not None:
assert hasattr(self, 'beta') if not hasattr(self, 'beta'):
raise Exception('Port has no beta attribute!')
shift = ref_plane_shift shift = ref_plane_shift
if self.measplane_shift: if self.measplane_shift:
shift -= self.measplane_shift shift -= self.measplane_shift
@ -152,7 +154,8 @@ class LumpedPort(Port):
self.exc_ny = CheckNyDir(exc_dir) self.exc_ny = CheckNyDir(exc_dir)
self.direction = np.sign(self.stop[self.exc_ny]-self.start[self.exc_ny]) self.direction = np.sign(self.stop[self.exc_ny]-self.start[self.exc_ny])
assert self.start[self.exc_ny]!=self.stop[self.exc_ny], 'LumpedPort: start and stop may not be identical in excitation direction' if not self.start[self.exc_ny]!=self.stop[self.exc_ny]:
raise Exception('LumpedPort: start and stop may not be identical in excitation direction')
if self.R > 0: if self.R > 0:
lumped_R = CSX.AddLumpedElement(self.lbl_temp.format('resist'), ny=self.exc_ny, caps=True, R=self.R) lumped_R = CSX.AddLumpedElement(self.lbl_temp.format('resist'), ny=self.exc_ny, caps=True, R=self.R)
@ -206,10 +209,12 @@ class MSLPort(Port):
self.prop_ny = CheckNyDir(prop_dir) self.prop_ny = CheckNyDir(prop_dir)
self.direction = np.sign(stop[self.prop_ny]-start[self.prop_ny]) self.direction = np.sign(stop[self.prop_ny]-start[self.prop_ny])
self.upside_down = np.sign(stop[self.exc_ny] -start[self.exc_ny]) self.upside_down = np.sign(stop[self.exc_ny] -start[self.exc_ny])
assert (self.start!=self.stop).all() if not (self.start!=self.stop).all():
raise Exception('Start coordinate must not be equal to stop coordinate')
# assert stop[self.prop_ny]!=start[self.prop_ny], 'port length in propergation direction may not be zero!' # assert stop[self.prop_ny]!=start[self.prop_ny], 'port length in propergation direction may not be zero!'
# assert stop[self.exc_ny] !=start[self.exc_ny], 'port length in propergation direction may not be zero!' # assert stop[self.exc_ny] !=start[self.exc_ny], 'port length in propergation direction may not be zero!'
assert self.exc_ny!=self.prop_ny if not self.exc_ny!=self.prop_ny:
raise Exception('Excitation direction must not be equal to propagation direction')
self.feed_shift = 0 self.feed_shift = 0
if 'FeedShift' in kw: if 'FeedShift' in kw:
@ -230,7 +235,8 @@ class MSLPort(Port):
mesh = CSX.GetGrid() mesh = CSX.GetGrid()
prop_lines = mesh.GetLines(self.prop_ny) prop_lines = mesh.GetLines(self.prop_ny)
assert len(prop_lines)>5, 'At least 5 lines in propagation direction required!' if not len(prop_lines)>5:
raise Exception('At least 5 lines in propagation direction required!')
meas_pos_idx = np.argmin(np.abs(prop_lines-self.measplane_pos)) meas_pos_idx = np.argmin(np.abs(prop_lines-self.measplane_pos))
if meas_pos_idx==0: if meas_pos_idx==0:
meas_pos_idx=1 meas_pos_idx=1
@ -328,7 +334,8 @@ class WaveguidePort(Port):
self.direction = np.sign(stop[self.exc_ny]-start[self.exc_ny]) self.direction = np.sign(stop[self.exc_ny]-start[self.exc_ny])
self.ref_index = 1 self.ref_index = 1
assert not (self.excite!=0 and stop[self.exc_ny]==start[self.exc_ny]), 'port length in excitation direction may not be zero if port is excited!' if (self.excite!=0 and stop[self.exc_ny]==start[self.exc_ny]):
raise Exception('Port length in excitation direction may not be zero if port is excited!')
self.kc = kc self.kc = kc
self.E_func = E_WG_func self.E_func = E_WG_func
@ -387,7 +394,8 @@ class RectWGPort(WaveguidePort):
self.WG_size = [a, b] self.WG_size = [a, b]
self.WG_mode = mode_name self.WG_mode = mode_name
assert len(self.WG_mode)==4, 'Invalid mode definition' if not len(self.WG_mode)==4:
raise Exception('Invalid mode definition')
self.unit = self.CSX.GetGrid().GetDeltaUnit() self.unit = self.CSX.GetGrid().GetDeltaUnit()
if self.WG_mode.startswith('TE'): if self.WG_mode.startswith('TE'):
self.TE = True self.TE = True
@ -398,7 +406,8 @@ class RectWGPort(WaveguidePort):
self.M = float(self.WG_mode[2]) self.M = float(self.WG_mode[2])
self.N = float(self.WG_mode[3]) self.N = float(self.WG_mode[3])
assert self.TE, 'Currently only TE-modes are supported! Mode found: {}'.format(self.WG_mode) if not self.TE:
raise Exception('Currently only TE-modes are supported! Mode found: {}'.format(self.WG_mode))
# values by David M. Pozar, Microwave Engineering, third edition # values by David M. Pozar, Microwave Engineering, third edition
a = self.WG_size[0] a = self.WG_size[0]