Skip to content

Commit b25c2c8

Browse files
authored
Merge pull request #121 from eX-Mech/raise-exception-not-log
Raise exception not log
2 parents 8422b62 + f32029b commit b25c2c8

File tree

4 files changed

+34
-59
lines changed

4 files changed

+34
-59
lines changed

src/pymech/core.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,12 @@ def check_connectivity(self, tol=1e-3):
337337
logger.error(
338338
f"face centers: ({xc:.6e} {yc:.6e} {zc:.6e}), ({xc1:.6e} {yc1:.6e} {zc1:.6e})"
339339
)
340+
341+
if err:
342+
raise ValueError(
343+
"Some errors were encountered while checking connectivity."
344+
)
345+
340346
return not err
341347

342348
def check_bcs_present(self):
@@ -356,6 +362,12 @@ def check_bcs_present(self):
356362
logger.error(
357363
f"missing boundary condition at element {iel}, face {iface}, field {ibc}"
358364
)
365+
366+
if not res:
367+
raise ValueError(
368+
"Some errors were encountered while checking boundary conditions."
369+
)
370+
359371
return res
360372

361373
def merge(self, other, tol=1e-2, ignore_empty=True, ignore_all_bcs=False):
@@ -384,17 +396,15 @@ def merge(self, other, tol=1e-2, ignore_empty=True, ignore_all_bcs=False):
384396

385397
# perform some consistency checks
386398
if self.ndim != other.ndim:
387-
logger.error(
399+
raise ValueError(
388400
f"Cannot merge meshes of dimensions {self.ndim} and {other.ndim}!"
389401
)
390-
return -1
391402
if self.lr1[0] != other.lr1[0]:
392-
logger.error(
403+
raise ValueError(
393404
"Cannot merge meshes of different polynomial orders ({} != {})".format(
394405
self.lr1[0], other.lr1[0]
395406
)
396407
)
397-
return -2
398408

399409
# add the new elements (in an inconsistent state if there are internal boundary conditions)
400410
nel1 = self.nel
@@ -415,7 +425,7 @@ def merge(self, other, tol=1e-2, ignore_empty=True, ignore_all_bcs=False):
415425
nchanges = 0 # counter for the boundary conditions connected
416426
if nbc == 0 or ignore_all_bcs:
417427
# Quickly exit the function
418-
logger.debug("no pairs of faces to merge")
428+
logger.warning("No pairs of faces to merge.")
419429
return nchanges
420430

421431
for iel0, iface0 in product(range(nel1, self.nel), range(nfaces)):

src/pymech/neksuite/field.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __attrs_post_init__(self):
7979
elif wdsz == 8:
8080
self.realtype = "d"
8181
else:
82-
logger.error(f"Could not interpret real type (wdsz = {wdsz})")
82+
raise ValueError(f"Could not interpret real type (wdsz = {wdsz})")
8383

8484
orders = self.orders
8585
if not self.nb_pts_elem:
@@ -103,12 +103,10 @@ def _variables_to_nb_vars(self) -> Optional[Tuple[int, ...]]:
103103
nb_dims = self.nb_dims
104104

105105
if not variables:
106-
logger.error("Failed to convert variables to nb_vars")
107-
return None
106+
raise ValueError("Failed to convert variables to nb_vars")
108107

109108
if not nb_dims:
110-
logger.error("Unintialized nb_dims")
111-
return None
109+
raise ValueError("Unintialized nb_dims")
112110

113111
def nb_scalars():
114112
index_s = variables.index("S")
@@ -127,8 +125,7 @@ def nb_scalars():
127125
def _nb_vars_to_variables(self) -> Optional[str]:
128126
nb_vars = self.nb_vars
129127
if not nb_vars:
130-
logger.error("Failed to convert nb_vars to variables")
131-
return None
128+
raise ValueError("Failed to convert nb_vars to variables")
132129

133130
str_vars = ("X", "U", "P", "T", f"S{nb_vars[4]:02d}")
134131
variables = (str_vars[i] if nb_vars[i] > 0 else "" for i in range(5))
@@ -191,11 +188,7 @@ def readnek(fname, dtype="float64", skip_vars=()):
191188
192189
"""
193190
#
194-
try:
195-
infile = open(fname, "rb")
196-
except OSError as e:
197-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
198-
return -1
191+
infile = open(fname, "rb")
199192
#
200193
# ---------------------------------------------------------------------------
201194
# READ HEADER
@@ -217,8 +210,8 @@ def readnek(fname, dtype="float64", skip_vars=()):
217210
logger.debug("Reading big-endian file\n")
218211
emode = ">"
219212
else:
220-
logger.error("Could not interpret endianness")
221-
return -3
213+
raise ValueError("Could not interpret endianness")
214+
222215
#
223216
# read element map for the file
224217
elmap = infile.read(4 * h.nb_elems_file)
@@ -391,11 +384,7 @@ def writenek(fname, data):
391384
data structure
392385
"""
393386
#
394-
try:
395-
outfile = open(fname, "wb")
396-
except OSError as e:
397-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
398-
return -1
387+
outfile = open(fname, "wb")
399388
#
400389
# ---------------------------------------------------------------------------
401390
# WRITE HEADER
@@ -422,8 +411,7 @@ def writenek(fname, data):
422411
elif h.wdsz == 8:
423412
logger.debug("Writing double-precision file")
424413
else:
425-
logger.error("Could not interpret real type (wdsz = %i)" % (data.wdsz))
426-
return -2
414+
raise ValueError("Could not interpret real type (wdsz = %i)" % (data.wdsz))
427415
#
428416
# generate header
429417
outfile.write(h.as_bytestring())

src/pymech/neksuite/map.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ def readma2(fname):
2626
procmap : 1d int ndarray
2727
processor map (0-based) indicating ownership for each element of the mesh
2828
"""
29-
try:
30-
infile = open(fname, "rb")
31-
except OSError as e:
32-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
33-
return -1
29+
infile = open(fname, "rb")
3430
#
3531
# read header
3632
header = infile.read(132).split()
@@ -77,8 +73,7 @@ def readma2(fname):
7773
emode = ">"
7874
# endian = "big"
7975
else:
80-
logger.error("Could not interpret endianness")
81-
return -3
76+
raise ValueError("Could not interpret endianness")
8277

8378
# read the entire contents of the file
8479
# for each element, there are nvert vertices and a processor id

src/pymech/neksuite/mesh.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ def readrea(fname):
1818
file name
1919
"""
2020
#
21-
try:
22-
infile = open(fname)
23-
except OSError as e:
24-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
25-
# return -1
21+
infile = open(fname)
2622
#
2723
# ---------------------------------------------------------------------------
2824
# count the number of boundary conditions
@@ -211,11 +207,7 @@ def writerea(fname, data):
211207
data structure
212208
"""
213209
#
214-
try:
215-
outfile = open(fname, "w")
216-
except OSError as e:
217-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
218-
# return -1
210+
outfile = open(fname, "w")
219211
#
220212
# ---------------------------------------------------------------------------
221213
# READ HEADER (2 lines) + ndim + number of parameters
@@ -615,11 +607,7 @@ def readre2(fname):
615607
file name
616608
"""
617609
#
618-
try:
619-
infile = open(fname, "rb")
620-
except OSError as e:
621-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
622-
return -1
610+
infile = open(fname, "rb")
623611
# the header for re2 files is 80 ASCII bytes, something like
624612
# #v002 18669 2 18669 this is the hdr %
625613
header = infile.read(80).split()
@@ -644,8 +632,8 @@ def readre2(fname):
644632
emode = ">"
645633
endian = "big"
646634
else:
647-
logger.error("Could not interpret endianness")
648-
return -3
635+
raise ValueError("Could not interpret endianness")
636+
649637
#
650638
# there are no GLL points here, only quad/hex vertices
651639
lr1 = [2, 2, ndim - 1]
@@ -775,27 +763,21 @@ def writere2(fname, data):
775763
#
776764
# We could extract the corners, but for now just return an error if lr1 is too large
777765
if data.lr1 != [2, 2, data.ndim - 1]:
778-
logger.critical(
766+
raise ValueError(
779767
"wrong element dimensions for re2 file! {} != {}".format(
780768
data.lr1, [2, 2, data.ndim - 1]
781769
)
782770
)
783-
return -2
784771
#
785772
if data.var[0] != data.ndim:
786-
logger.critical(
773+
raise ValueError(
787774
"wrong number of geometric variables for re2 file! expected {}, found {}".format(
788775
data.ndim, data.var[0]
789776
)
790777
)
791-
return -3
792778
#
793779
# Open file
794-
try:
795-
outfile = open(fname, "wb")
796-
except OSError as e:
797-
logger.critical(f"I/O error ({e.errno}): {e.strerror}")
798-
return -1
780+
outfile = open(fname, "wb")
799781
#
800782
# ---------------------------------------------------------------------------
801783
# WRITE HEADER

0 commit comments

Comments
 (0)