From 1f537465149ec5594b232322a4ac2e0fd10d4671 Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Thu, 20 Aug 2026 14:29:41 +0200 Subject: [PATCH] Remove a dead element-code check in MeshFile, and record why Z is float64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The element-code check could never do anything: if (elmtCode != 21) or (elmtCode != 25): pass The condition is true for every value — 21 fails the second test, 25 fails the first — so it needs `and`. That was harmless only because the body is `pass`, but the TODO beside it invited filling the body in, at which point the check would have rejected every valid mesh. ElementType is already derived per element from the corner count a few lines below, so the header's code needs no separate handling; the comment now says what elmtCode is and that nothing acts on it. Whether an unexpected element code should raise, warn, or continue to be ignored is a policy question, filed as an issue rather than decided here. The `# TODO or np.float32 ?` beside the Z array is also answerable: float64 is correct. A .mesh stores coordinates as text, so the file can carry more precision than float32 holds, and MeshBuilder.SetNodes narrows Z only because that path leads to a dfsu, where node Z occupies four bytes. Narrowing on read would drop digits the file actually contains, and mikeio reads MeshFile.Z straight into its mesh geometry, so the loss would be silent there. No behaviour change: both hunks are comments plus the removal of an if/pass. --- mikecore/MeshFile.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mikecore/MeshFile.py b/mikecore/MeshFile.py index ef0a464..d7cdd12 100644 --- a/mikecore/MeshFile.py +++ b/mikecore/MeshFile.py @@ -242,7 +242,13 @@ def Read(self, filename:str): self.NodeIds = np.zeros(noNodes, dtype=np.int32) self.X = np.zeros(noNodes, dtype=np.float64) self.Y = np.zeros(noNodes, dtype=np.float64) - self.Z = np.zeros(noNodes, dtype=np.float64) # TODO or np.float32 ? + # float64 is correct here, and deliberate. A .mesh stores coordinates + # as text, so the file imposes no width and can carry more precision + # than float32 holds. MeshBuilder.SetNodes narrows Z to float32 + # because that is the path towards a dfsu, where node Z occupies four + # bytes; reading a file is not that path. Narrowing here would + # silently drop digits the file actually contains. + self.Z = np.zeros(noNodes, dtype=np.float64) self.Code = np.zeros(noNodes, dtype=np.int32) # Read nodes @@ -275,10 +281,10 @@ def Read(self, filename:str): except Exception as ex: raise Exception("Can not load mesh file (failed reading element header line): {0}. {1}".format(filename, ex)) - # Element code must be 21 or 25 (21 for triangular meshes, 25 for mixed meshes) - if (elmtCode != 21) or (elmtCode != 25): - pass # TODO?? Do we care? - + # elmtCode is the element code from the header: 21 for a triangular + # mesh, 25 for a mixed one. It is read but not acted on; ElementType + # is derived per element from the corner count below. + # Allocate memory for elements self.ElementIds = np.zeros(noElements, dtype=np.int32) self.ElementType = np.zeros(noElements, dtype=np.int32)