lanl / LaGriT

Los Alamos Grid Toolbox (LaGriT) is a library of user callable tools that provide mesh generation, mesh optimization and dynamic mesh maintenance in two and three dimensions.
https://lanl.github.io/LaGriT/
Other
116 stars 48 forks source link

PyLaGriT: calling LaGriT thru ctypes lib instead of pexpect process #220

Open daniellivingston opened 3 years ago

daniellivingston commented 3 years ago

Right now, PyLaGriT uses pexpect to launch LaGriT a child process, constructs LaGriT commands as strings (i.e., lg.create() -> cmo/create/mo1), and has pexpect send that string via stdin to the LaGriT child process.

This is a great approach but suffers the flaw that all data structures (x, y, z arrays of nodes, connectivity arrays, etc.) are inaccessible from Python. The only workaround is to dump the mesh object to a file and then to read the file in.

By compiling LaGriT as a library (Instead of an executable) and by using CTypes, we can directly call LaGriT functions from Python code.

Now, LaGriT source has a subroutine called do_task(char *string), which performs effectively what pexpect does above: takes in a LaGriT command string and runs it.

So, critically, almost no modification of PyLaGriT will be needed. The sendline() method just changes from calling pexpect to calling do_task().

A huge benefit of this will be the direct access (read and write) of internal LaGriT data structures. This would allow:

So, mockup code might be:

import ctypes

_lg = ctypes.CDLL('liblagrit.so')

class MeshObject:
    def __init__(self, *args):
        [do stuff]

    @property
    def x_vector(self):
        # defining ixic, ilen, itype, ierror variables left as an exercise to the reader... :D
        _lg.cmo_get_info('xic', self.name, ixic, ilen, itype, ierror)
        return convert_pointer_to_numpy_array_somehow(ixic)
daniellivingston commented 3 years ago

@dharp Thoughts?

dharp commented 3 years ago

Nice plan, Daniel! I know we had talked about this in the past, but this is a much more well-thought-out plan than we had back then. You've obviously thought this through some more. Seems like it could be doable and would be a huge advance beyond the current pylagrit. Also, I really like how you've laid it out to use the existing pylagrit methods. Very elegant!