Commit 818db069 authored by Stauff, Nicolas Emile's avatar Stauff, Nicolas Emile
Browse files

Merge branch 'griffin_wrapper' into 'main'

Griffin wrapper code

See merge request nstauff/PyGriffin!2
parents 40ebc7f2 6a4fef6e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
__version__ = '0.0.1'

from pygriffin.config import *
from pygriffin.pygriffin import *
+0 −1
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ class PyGriffinConfig():
        # always read the config file,
        # even if we don't use it
        self.__config = ConfigParser()
        print("Parsing file {}".format(self._RC_FILE))
        self.__config.read(self._RC_FILE)

        # if the Griffin path parameter is set, use that
+96 −0
Original line number Diff line number Diff line
from numbers import Integral
from pathlib import Path
import os
import subprocess

import pygriffin.checkvalue as cv
from pygriffin.config import PyGriffinConfig

class PyGriffin:
    """
    Class for connecting PyGriffin inputs and running the problems

    Parameters
    ----------
    input : str or Path object
        Location of Griffin input file
    n_procs : int
        Number of processes in Griffin run
    n_threads : int
    """

    def __init__(self, input, n_procs=1, n_threads=1):
        self.config = PyGriffinConfig()
        self.input = input
        self.n_procs = n_procs
        self.n_threads = n_threads

    def run(self, cwd=None):
        """
        Execute Griffin

        Parameters
        ----------
        cwd : str or Path
            Directory from which to run Griffin

        Returns
        -------
        int
            Errorcode of the Griffin run (0 is success)
        """

        cmd = []

        if self.n_procs > 1:
            cmd += ['mpirun', '-n', str(self.n_procs)]

        # add griffin executable to command line
        cmd.append(str(self.config.griffin_exec))

        cmd += ['-i', str(self.input)]

        if self.n_threads > 1:
            cmd.append('--n-threads={}'.format(self.n_threads))

        cwd = Path(cwd)
        if not cwd.is_dir():
            msg = ("Specified working directory "
            "doesn't exist: {}".format(cwd))
            raise ValueError(msg)

        p = subprocess.Popen(cmd, universal_newlines=True, cwd=str(cwd))

        return p.wait()

    def __call__(self, cwd=None):
        return self.run(cwd)

    @property
    def input(self):
        return self._input

    @input.setter
    def input(self, input):
        cv.check_type('Griffin input', input, (str, Path))
        self._input = Path(input).absolute()

    @property
    def n_procs(self):
        return self._n_procs

    @n_procs.setter
    def n_procs(self, procs):
        cv.check_type('Griffin processes', procs, Integral)
        cv.check_greater_than('Griffin processes', procs, 0)
        self._n_procs = procs

    @property
    def n_threads(self):
        return self._n_threads

    @n_threads.setter
    def n_threads(self, threads):
        cv.check_type('Griffin threads', threads, Integral)
        cv.check_greater_than('Griffin threads', threads, 0)
        self._n_threads = threads
+2 −0
Original line number Diff line number Diff line
[exec]
griffin = ./griffin_mock
 No newline at end of file