fluiddyn / fluidfoam

OpenFoam postprocessing python tool
GNU General Public License v3.0
158 stars 50 forks source link

Simple change to improve compatibility #47

Closed dirk-netl closed 1 year ago

dirk-netl commented 1 year ago

Hi,

Started using this library to parse foam files. I used it with the bunny and started getting some errors. That is because the old file format for neighbors allowed -1 to be present in the file to say "the face isn't here". I updated the _parse_owner function in the foam reader to handle this case. works fine now.

def _parse_owner(self):

        for line in self.lines_stripped:
            try:
                int(line)
                break
            except ValueError:
                continue
            break
        self.nb_faces = int(line)
        data = self.content.split(line, -1)[-1]

        self.type_data = self.header[b"class"]

        if not self.is_ascii:
            nb_numbers = self.nb_faces
            data = b"\n(".join(data.split(b"\n(")[1:])
            self.values = np.array(
                struct.unpack(
                    "{}i".format(nb_numbers),
                    data[: nb_numbers * struct.calcsize("i")],
                )
            )
        else:
            lines = data.split(b"\n(")[1:]
            lines = [line.split(b")")[0] for line in lines]
            data = b" ".join(lines).strip()
            self.values = np.array([int(s) for s in data.split()])
        if self.values.min() < 0:
            self.values = self.values[np.where(self.values>=0)[0]]
            self.nb_faces = self.values.size
        self.nb_cell = np.max(self.values) + 1
CyrilleBonamy commented 1 year ago

Thanks you very much for your contribution. I just incorporated your change, but about that line "data = self.content.split(line, -1)[-1]", i went back to the initial one "data = self.content.split(line, 2)[-1]" because otherwise it introduces a bug. Can you try the latest github version of fluidfoam and confirm that it is correct for you now

dirk-netl commented 1 year ago

It looks like it worked.

CyrilleBonamy commented 1 year ago

Perfect! So I close the issue. Thank you again for your contribution