-
Notifications
You must be signed in to change notification settings - Fork 27
Refactor parsing of numeric ASCII lists-of-lists #772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RamogninoF
wants to merge
1
commit into
gerlero:main
Choose a base branch
from
RamogninoF:parsing-ascii-listlist
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−113
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
| from foamlib import FoamFile | ||
|
|
||
| faces_contents = r""" | ||
| /*--------------------------------*- C++ -*----------------------------------*\ | ||
| | ========= | | | ||
| | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | | ||
| | \\ / O peration | Version: 2206 | | ||
| | \\ / A nd | Website: www.openfoam.com | | ||
| | \\/ M anipulation | | | ||
| \*---------------------------------------------------------------------------*/ | ||
| FoamFile | ||
| { | ||
| version 2.0; | ||
| format ascii; | ||
| class faceList; | ||
| location "constant/polyMesh"; | ||
| object faces; | ||
| } | ||
| // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | ||
|
|
||
| 3 | ||
| ( | ||
| 3(0 1 2) | ||
| 4(3 4 5 6) | ||
| 5(7 8 9 10 11) | ||
| ) | ||
|
|
||
| // ************************************************************************* // | ||
| """ | ||
|
|
||
|
|
||
| def test_parse_poly_faces(tmp_path: Path) -> None: | ||
| """Test that ascii faceList with triangles, quads, and pentagons is parsed correctly.""" | ||
| path = tmp_path / "faces" | ||
| path.write_text(faces_contents) | ||
|
|
||
| file = FoamFile(path) | ||
| faces = file[None] | ||
|
|
||
| assert len(faces) == 3 | ||
| assert np.array_equal(faces[0], [0, 1, 2]) | ||
| assert np.array_equal(faces[1], [3, 4, 5, 6]) | ||
| assert np.array_equal(faces[2], [7, 8, 9, 10, 11]) | ||
|
|
||
|
|
||
| float_list_list_contents = r""" | ||
| 3 | ||
| ( | ||
| 2(0.1 0.2) | ||
| 3(0.3 0.4 0.5) | ||
| 1(0.6) | ||
| ) | ||
| """ | ||
|
|
||
|
|
||
| def test_parse_float_list_list(tmp_path: Path) -> None: | ||
| """Test that a standalone ascii numeric list-of-lists with float values is parsed correctly.""" | ||
| path = tmp_path / "floats" | ||
| path.write_text(float_list_list_contents) | ||
|
|
||
| file = FoamFile(path) | ||
| data = file[None] | ||
|
|
||
| assert len(data) == 3 | ||
| assert np.allclose(data[0], [0.1, 0.2]) | ||
| assert np.allclose(data[1], [0.3, 0.4, 0.5]) | ||
| assert np.allclose(data[2], [0.6]) | ||
|
|
||
|
|
||
| commented_faces_contents = r""" | ||
| 3 | ||
| ( | ||
| 3(0 1 2) // triangle | ||
| 4 /* quad */ (3 4 5 6) | ||
| 5( | ||
| 7 // comment inside | ||
| 8 | ||
| 9 | ||
| 10 | ||
| 11 | ||
| ) | ||
| ) | ||
| """ | ||
|
|
||
|
|
||
| def test_parse_commented_faces(tmp_path: Path) -> None: | ||
| """Test that ascii faceList with inline comments is parsed correctly.""" | ||
| path = tmp_path / "faces_commented" | ||
| path.write_text(commented_faces_contents) | ||
|
|
||
| file = FoamFile(path) | ||
| faces = file[None] | ||
|
|
||
| assert len(faces) == 3 | ||
| assert np.array_equal(faces[0], [0, 1, 2]) | ||
| assert np.array_equal(faces[1], [3, 4, 5, 6]) | ||
| assert np.array_equal(faces[2], [7, 8, 9, 10, 11]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RamogninoF problem to me is that this doesn't actually check that the sublist is well-formed. E.g. this will readily accept a list with a wrong count like
2 (1 2 3)...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I have no background in parsing logics etc. and all this is far beyond my capabilities, I just hope this can be a useful starting point for you. At the current state parsing of meshes in ascii format is just straight impossible due to the time required for parsing (I gave up even on a 10k cells mesh after it was taking more then 10 minutes parsing the faces file). I would like to be able to support handling also ascii meshes rather then only binary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simple alternative could be just to add hardcoded parser for up to 10-vertex faces or so, which I think would be more then enough for most cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or what if the string is parsed twice via regex, one to retrieve the list and one to get the prefix marking it's length, and these quantities are compared to validate the parsed data before returning?