nemocrys / pyelmer

A python interface to Elmer.
GNU General Public License v3.0
56 stars 18 forks source link

Improve run_elmer_grid function #23

Open arvedes opened 1 year ago

arvedes commented 1 year ago

There are some limitations of the run_elmer_grid function:

It shouldn't be a big deal to implement that.

muddi900 commented 1 year ago

For number 2, do you mean something like this.

+def run_elmer_grid(sim_dir, meshfile, elmergrid=None, **kwargs):
     """Run ElmerGrid on gmsh meshfile and move everithing into main
     directory.

@@ -14,6 +14,8 @@ def run_elmer_grid(sim_dir, meshfile, elmergrid=None):
         sim_dir (str): Simulation directory
         meshfile (str): Filename of .msh file
         elmergrid (str): ElmerGrid executable
+    Kwargs:
+        out_dir (str): Optional directory to save the output.
     """
     if elmergrid is None:
         # On Windows ElmerGrid.exe is not found once gmsh.initialize() was executed.
@@ -28,11 +30,12 @@ def run_elmer_grid(sim_dir, meshfile, elmergrid=None):
         subprocess.run(args, cwd=sim_dir, stdout=f, stderr=f)

     mesh_dir = sim_dir + "/" + ".".join(meshfile.split(".")[:-1])
+    out_dir = kwargs.get('out_dir', sim_dir)
     files = os.listdir(mesh_dir)
     for f in files:
         if os.path.exists(sim_dir + "/" + f):
             os.remove(sim_dir + "/" + f)
-        shutil.move(mesh_dir + "/" + f, sim_dir)
+        shutil.move(mesh_dir + "/" + f, out_dir)
     shutil.rmtree(mesh_dir)
arvedes commented 1 year ago

Yes, exactly. Alternatively we could do something like

def run_elmer_grid(sim_dir, meshfile, elmergrid=None, move_to_simdir=True):
    (...)

    if move_to_simdir:
        files = os.listdir(mesh_dir)
        for f in files:
            if os.path.exists(sim_dir + "/" + f):
                os.remove(sim_dir + "/" + f)
            shutil.move(mesh_dir + "/" + f, sim_dir)
        shutil.rmtree(mesh_dir)

In case move_to_simdir==False everyting would stay in mesh_dir folder, as it is the usual ElmerGrid behavior.

Which one would you prefer?

muddi900 commented 1 year ago

We can also add bool kwarg keep_mesh_dir to the value, which will not run the code block at all.

On Thu, Jan 5, 2023 at 3:15 PM Arved Wintzer @.***> wrote:

Yes, exactly. Alternatively we could do something like

def run_elmer_grid(sim_dir, meshfile, elmergrid=None, move_to_simdir=True): (...)

if move_to_simdir:
    files = os.listdir(mesh_dir)
    for f in files:
        if os.path.exists(sim_dir + "/" + f):
            os.remove(sim_dir + "/" + f)
        shutil.move(mesh_dir + "/" + f, sim_dir)
    shutil.rmtree(mesh_dir)

In case move_to_simdir==False everyting would stay in mesh_dir folder, as it is the usual ElmerGrid behavior.

Which one would you prefer?

— Reply to this email directly, view it on GitHub https://github.com/nemocrys/pyelmer/issues/23#issuecomment-1372024876, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI2VVZVIKNEP4GKYVUC3FZLWQ2NKTANCNFSM6AAAAAATQ5WT24 . You are receiving this because you commented.Message ID: @.***>

arvedes commented 1 year ago

Sounds good!