Beispiel #1
0
    def __init__(self):
        super(OpenCL, self).__init__()
        # Create back-end and cache it
        OpenCL._index += 1
        mname = 'myokit_opencl_info_' + str(OpenCL._index)
        mname += '_' + str(myokit._pid_hash())
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)
        args = {'module_name': mname}

        # Define libraries
        libs = []
        flags = []
        import platform
        if platform.system() != 'Darwin':  # pragma: no osx cover
            libs.append('OpenCL')
        else:  # pragma: no cover
            flags.append('-framework OpenCL')

        # Add include / linker paths
        libd = list(myokit.OPENCL_LIB)
        incd = list(myokit.OPENCL_INC)
        incd.append(myokit.DIR_CFUNC)

        try:
            OpenCL._message = None
            OpenCL._instance = self._compile(mname,
                                             fname,
                                             args,
                                             libs,
                                             libd,
                                             incd,
                                             larg=flags)
        except myokit.CompilationError as e:
            OpenCL._instance = False
            OpenCL._message = str(e)
Beispiel #2
0
    def __init__(self, model, protocol=None):
        super(ICSimulation, self).__init__()

        # Require a valid model
        if not model.is_valid():
            model.validate()
        model = model.clone()
        self._model = model

        # Set protocol
        self.set_protocol(protocol)

        # Get state and default state from model
        self._state = self._model.state()
        self._default_state = list(self._state)

        # Create initial list of derivatives
        n = len(self._state)
        self._deriv = [0.0] * n**2
        for i in range(n):
            self._deriv[i * (n + 1)] = 1.0

        # Starting time
        self._time = 0

        # Default time step
        self._dt = 0
        self.set_step_size()

        # Unique simulation id
        ICSimulation._index += 1
        module_name = 'myokit_ICSimulation_' + str(ICSimulation._index)
        module_name += '_' + str(myokit._pid_hash())

        # Arguments
        args = {
            'module_name': module_name,
            'model': self._model,
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)

        # Debug
        if myokit.DEBUG:
            print(self._code(
                fname, args, line_numbers=myokit.DEBUG_LINE_NUMBERS))
            import sys
            sys.exit(1)

        # Define libraries
        libs = []
        if platform.system() != 'Windows':  # pragma: no windows cover
            libs.append('m')

        # Create simulation
        libd = []
        incd = [myokit.DIR_CFUNC]
        self._sim = self._compile(module_name, fname, args, libs, libd, incd)
Beispiel #3
0
    def _create_backend(self):
        """
        Creates this simulation's backend.
        """
        # Unique simulation id
        FiberTissueSimulation._index += 1
        mname = 'myokit_sim_fiber_tissue_' + str(FiberTissueSimulation._index)
        mname += '_' + str(myokit._pid_hash())

        # Arguments
        args = {
            'module_name': mname,
            'modelf': self._modelf,
            'modelt': self._modelt,
            'vmf': self._vmf,
            'vmt': self._vmt,
            'boundf': self._bound_variablesf,
            'boundt': self._bound_variablest,
            'precision': self._precision,
            'native_math': self._native_math,
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)

        # Debug
        if myokit.DEBUG:
            print(
                self._code(fname, args,
                           line_numbers=myokit.DEBUG_LINE_NUMBERS))
            return

        # Define libraries
        libs = []
        flags = []
        plat = platform.system()
        if plat != 'Darwin':  # pragma: no osx cover
            libs.append('OpenCL')
        else:  # pragma: no cover
            flags.append('-framework OpenCL')
        if plat != 'Windows':  # pragma: no windows cover
            libs.append('m')

        # Create extension
        libd = list(myokit.OPENCL_LIB)
        incd = list(myokit.OPENCL_INC)
        incd.append(myokit.DIR_CFUNC)
        self._sim = self._compile(mname,
                                  fname,
                                  args,
                                  libs,
                                  libd,
                                  incd,
                                  larg=flags)
Beispiel #4
0
    def __init__(self, model):
        super(JacobianCalculator, self).__init__()
        # Require a valid model
        model.validate()

        # Clone model
        self._model = model.clone()

        # Unbind all inputs
        for label, var in self._model.bindings():
            var.set_binding(None)

        # Extension module id
        JacobianCalculator._index += 1
        module_name = 'myokit_JacobianCalculator_'
        module_name += str(JacobianCalculator._index)
        module_name += '_' + str(myokit._pid_hash())

        # Template arguments
        args = {
            'module_name': module_name,
            'model': self._model,
            'inputs': [],
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)

        # Debug
        if myokit.DEBUG:
            print(
                self._code(fname, args, line_numbers=myokit.DEBUG_LINE_NUMBERS)
            )
            import sys
            sys.exit(1)

        # Define libraries
        libs = []
        if platform.system() != 'Windows':  # pragma: no windows cover
            libs.append('m')

        # Compile extension
        libd = []
        incd = [myokit.DIR_CFUNC]
        self._ext = self._compile(module_name, fname, args, libs, libd, incd)
Beispiel #5
0
    def __init__(self, model, variables=None, exclude_selected=False):
        super(RhsBenchmarker, self).__init__()

        # Require a valid model
        model.validate()

        # Clone model
        self._model = model.clone()

        # Check given variables
        self._check_variables(variables)

        # Extension module id
        RhsBenchmarker._index += 1
        module_name = 'myokit_RhsBenchmarker_' + str(RhsBenchmarker._index)
        module_name += '_' + str(myokit._pid_hash())

        # Distutils arguments
        args = {
            'module_name': module_name,
            'model': self._model,
            'variables': self._variables,
            'exclude_selected': exclude_selected,
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)

        # Debug
        if myokit.DEBUG:
            print(
                self._code(fname, args,
                           line_numbers=myokit.DEBUG_LINE_NUMBERS))
            import sys
            sys.exit(1)

        # Define libraries
        libs = []
        if platform.system() != 'Windows':  # pragma: no windows cover
            libs.append('m')

        # Create extension
        self._ext = self._compile(module_name, fname, args, libs)
Beispiel #6
0
    def __init__(self):
        super(Sundials, self).__init__()
        # Create and cache back-end
        Sundials._index += 1

        # Define libraries
        libd = list(myokit.SUNDIALS_LIB)
        incd = list(myokit.SUNDIALS_INC)
        incd.append(myokit.DIR_CFUNC)
        libs = []

        # Create Sundials back-end
        mname = 'myokit_sundials_info_' + str(Sundials._index)
        mname += '_' + str(myokit._pid_hash())
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)
        args = {'module_name': mname}
        try:
            Sundials._instance = self._compile(
                mname, fname, args, libs, libd, incd)
        except myokit.CompilationError as e:  # pragma: no cover
            Sundials._instance = False
            Sundials._message = str(e)
Beispiel #7
0
    def __init__(
            self, model, protocol=None, variables=None, parameters=None):
        super(PSimulation, self).__init__()

        # Check presence of variables and parameters arguments (are required
        # arguments but protocol is not...)
        if variables is None:
            raise ValueError(
                'Please specify a set of variables whose derivatives should be'
                ' tracked.')
        if parameters is None:
            raise ValueError('Please specify a set of parameters.')

        # Require a valid model
        if not model.is_valid():
            model.validate()
        model = model.clone()
        self._model = model

        # Set protocol
        self.set_protocol(protocol)

        # Check tracked variables
        if len(variables) != len(set(variables)):
            raise ValueError('Duplicate variable in `variables`.')
        self._variables = []
        for v in variables:
            if isinstance(v, myokit.Variable):
                v = v.qname()
            v = self._model.get(v, myokit.Variable)
            if not (v.is_state() or v.is_intermediary()):
                if v.is_bound():
                    raise ValueError(
                        'Variables to track cannot be bound to external'
                        ' inputs.')
                else:
                    raise ValueError('Variables to track cannot be constants.')
            self._variables.append(v)

        # Check parameters
        if len(parameters) != len(set(parameters)):
            raise ValueError('Duplicate parameter in `parameters`.')
        self._parameters = []
        for p in parameters:
            if isinstance(p, myokit.Variable):
                p = p.qname()
            p = self._model.get(p, myokit.Variable)
            if not p.is_literal():
                if p.is_bound():
                    raise ValueError(
                        'Parameters cannot be bound to external inputs.')
                else:
                    raise ValueError('Parameters must be literal constants.')
            self._parameters.append(p)
        del(parameters)

        # Create list of parameter values
        self._values = []
        for p in self._parameters:
            self._values.append(p.rhs().eval())

        # Get state and default state from model
        self._state = self._model.state()
        self._default_state = list(self._state)

        # Create list of state-parameter-derivatives
        ms = len(self._state)
        mp = len(self._parameters)
        self._state_ddp = [0.0] * (ms * mp)

        # Starting time
        self._time = 0

        # Default time step
        self._dt = 0
        self.set_step_size()

        # Unique simulation id
        PSimulation._index += 1
        module_name = 'myokit_PSimulation_' + str(PSimulation._index)
        module_name += '_' + str(myokit._pid_hash())

        # Arguments
        args = {
            'module_name': module_name,
            'model': self._model,
            'variables': self._variables,
            'parameters': self._parameters,
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)

        # Debug
        if myokit.DEBUG:
            print(
                self._code(fname, args, line_numbers=myokit.DEBUG_LINE_NUMBERS)
            )
            import sys
            sys.exit(1)

        # Define libraries
        libs = []
        if platform.system() != 'Windows':
            libs.append('m')

        # Create simulation
        libd = []
        incd = [myokit.DIR_CFUNC]
        self._sim = self._compile(module_name, fname, args, libs, libd, incd)
Beispiel #8
0
    def __init__(self, model, protocol=None, apd_var=None):
        super(Simulation, self).__init__()

        # Require a valid model
        if not model.is_valid():
            model.validate()
        model = model.clone()
        self._model = model

        # Set protocol
        self._protocol = None
        self._fixed_form_protocol = None
        self.set_protocol(protocol)

        # Check potential and threshold values
        if apd_var is None:
            self._apd_var = None
        else:
            if isinstance(apd_var, myokit.Variable):
                apd_var = apd_var.qname()
            self._apd_var = self._model.get(apd_var)
            if not self._apd_var.is_state():
                raise ValueError('The `apd_var` must be a state variable.')

        # Get state and default state from model
        self._state = self._model.state()
        self._default_state = list(self._state)

        # Last state reached before error
        self._error_state = None

        # Starting time
        self._time = 0

        # Unique simulation id
        Simulation._index += 1
        module_name = 'myokit_sim_' + str(Simulation._index)
        module_name += '_' + str(myokit._pid_hash())

        # Arguments
        args = {
            'module_name': module_name,
            'model': self._model,
            'potential': self._apd_var,
        }
        fname = os.path.join(myokit.DIR_CFUNC, SOURCE_FILE)
        # Debug
        if myokit.DEBUG:
            print(
                self._code(fname, args,
                           line_numbers=myokit.DEBUG_LINE_NUMBERS))
            import sys
            sys.exit(1)

        # Define libraries
        libs = [
            'sundials_cvode',
            'sundials_nvecserial',
        ]
        if platform.system() != 'Windows':  # pragma: no windows cover
            libs.append('m')

        # Define library paths
        # Note: Sundials path on windows already includes local binaries
        libd = list(myokit.SUNDIALS_LIB)
        incd = list(myokit.SUNDIALS_INC)
        incd.append(myokit.DIR_CFUNC)

        # Create extension
        self._sim = self._compile(module_name, fname, args, libs, libd, incd)

        # Set default tolerance values
        self._tolerance = None
        self.set_tolerance()

        # Set default min and max step size
        self._dtmax = self._dtmin = None