nschloe / pygmsh

:spider_web: Gmsh for Python
GNU General Public License v3.0
843 stars 161 forks source link

Need ability to reverse line orientation in a line loop #32

Closed briandrawert closed 7 years ago

briandrawert commented 7 years ago

I am getting around this by manually entering a - sign in front of

for k in range(nslices-1):
    ll = geom.add_line_loop([
        gmsh_hlines[n][k],
        gmsh_lines[n][k],
        '-' + gmsh_hlines[n][k+1],
        '-' + gmsh_lines[n-1][k]
        ])
    s = geom.add_plane_surface(ll)
nschloe commented 7 years ago

In front of what exactly?

briandrawert commented 7 years ago

The '.geo' code ends up looking like this:

ll1 = newll;
Line Loop(ll1) = {l21,l1,-l22};
surf1 = news;
Plane Surface(surf1) = {ll1};

Note the negative sign in front of l22.

nschloe commented 7 years ago

Ah, hm, yes. I see now that you needed to insert that as otherwise, the line loop would not consist of "consecutive" lines, i.e., the last point of line k wouldn't be the first point of line k+1.

How would you suggest that pygmsh works here? Checking the points of the elements in the line loop, and turn lines around if necessary? I believe that'll be a bit hard since right now, pygmsh doesn't really keep track. If a line is defined, the Gmsh code is written into a string and then forgotten.

briandrawert commented 7 years ago

Ideally it would be as you describe. One simpler option might be to have the return from the "add_line" function to be an object that extends the string class:

class mystr(str):
    def __neg__(self):
        return '-'+self

Then, add line loop could use this syntax:

geom.add_line_loop(l1, l2, -l3, -l4)
nschloe commented 7 years ago

I thought about this a little bit and I think we shouldn't take make it more complex than necessary. If gmsh requires you to insert a negation - sign, then pygmsh should too. Making this automatic would certainly be an improvement, but then again so would be many other things, like for example the order of the lines in a line loop.

Keeping things simple make it easier to maintain pygmsh and make it clearer for the user what actually happens.

Now, as for the syntax, there are two options: '-' + l1 and -l1. (The first one is used in in pygmsh code, e.g., in add_ellipsoid.) While the second is clearly shorter, the first makes explicit what happens: - is prepended to the line. Not sure what to prefer here.

nschloe commented 7 years ago

Alright, this is now fixed. All objects returned from pygmsh are now class instances, not strings, and you can (and in fact must) now do -my_line to reverse the line orientation.