Example #1
0
    def __init__(self, cores=None, version=None, port=None, host='localhost'):

        # Make sure this is the one and only client.
        if jpype.isJVMStarted():
            error = 'Only one client can be instantiated at a time.'
            logger.critical(error)
            raise NotImplementedError(error)

        # Discover Comsol back-end.
        backend = discovery.backend(version)

        # Instruct Comsol to limit number of processor cores to use.
        if cores:
            os.environ['COMSOL_NUM_THREADS'] = str(cores)

        # Start the Java virtual machine.
        logger.info(f'JPype version is {jpype.__version__}.')
        logger.info('Starting Java virtual machine.')
        jpype.startJVM(str(backend['jvm']),
                       classpath=str(backend['root'] / 'plugins' / '*'),
                       convertStrings=False)
        logger.info('Java virtual machine has started.')

        # Initialize a stand-alone client if no server port given.
        from com.comsol.model.util import ModelUtil as java
        if port is None:
            logger.info('Initializing stand-alone client.')
            check_environment(backend)
            graphics = True
            java.initStandalone(graphics)
            logger.info('Stand-alone client initialized.')
        # Otherwise skip stand-alone initialization and connect to server.
        else:
            logger.info(f'Connecting to server "{host}" at port {port}.')
            java.connect(host, port)

        # Log number of used processor cores as reported by Comsol instance.
        cores = java.getPreference('cluster.processor.numberofprocessors')
        cores = int(str(cores))
        noun = 'core' if cores == 1 else 'cores'
        logger.info(f'Running on {cores} processor {noun}.')

        # Load Comsol settings from disk so as to not just use defaults.
        java.loadPreferences()

        # Override certain settings not useful in headless operation.
        java.setPreference('updates.update.check', 'off')
        java.setPreference('tempfiles.saving.warnifoverwriteolder', 'off')
        java.setPreference('tempfiles.recovery.autosave', 'off')
        try:
            java.setPreference('tempfiles.recovery.checkforrecoveries', 'off')
        except Exception:
            logger.warning('Could not turn off check for recovery files.')
        java.setPreference('tempfiles.saving.optimize', 'filesize')

        # Save useful information in instance attributes.
        self.version = backend['name']
        self.cores = cores
        self.host = host
        self.port = port
        self.java = java
Example #2
0
File: client.py Project: max3-2/MPh
    def __init__(self, cores=None, version=None, port=None, host='localhost'):
        # Make sure this is the one and only client.
        if jpype.isJVMStarted():
            error = 'Only one client can be instantiated at a time.'
            logger.error(error)
            raise NotImplementedError(error)

        # Discover Comsol back-end.
        backend = discovery.backend(version)

        # Instruct Comsol to limit number of processor cores to use.
        if cores:
            os.environ['COMSOL_NUM_THREADS'] = str(cores)

        # On Windows, turn off fault handlers if enabled.
        # Without this, pyTest will crash when starting the Java VM.
        # See "Errors reported by Python fault handler" in JPype docs.
        # The problem may be the SIGSEGV signal, see JPype issue #886.
        if platform.system() == 'Windows' and faulthandler.is_enabled():
            logger.debug('Turning off Python fault handlers.')
            faulthandler.disable()

        # Start the Java virtual machine.
        logger.debug(f'JPype version is {jpype.__version__}.')
        logger.info('Starting Java virtual machine.')
        java_args = [str(backend['jvm'])]
        if option('classkit'):
            java_args += ['-Dcs.ckl']
        logger.debug(f'JVM arguments: {java_args}')
        jpype.startJVM(*java_args,
                       classpath=str(backend['root'] / 'plugins' / '*'),
                       convertStrings=False)
        logger.info('Java virtual machine has started.')

        # Initialize a stand-alone client if no server port given.
        from com.comsol.model.util import ModelUtil as java
        if port is None:
            logger.info('Initializing stand-alone client.')
            check_environment(backend)
            graphics = True
            java.initStandalone(graphics)
            logger.info('Stand-alone client initialized.')
        # Otherwise skip stand-alone initialization and connect to server.
        else:
            logger.info(f'Connecting to server "{host}" at port {port}.')
            java.connect(host, port)

        # Log number of used processor cores as reported by Comsol instance.
        cores = java.getPreference('cluster.processor.numberofprocessors')
        cores = int(str(cores))
        noun = 'core' if cores == 1 else 'cores'
        logger.info(f'Running on {cores} processor {noun}.')

        # Load Comsol settings from disk so as to not just use defaults.
        java.loadPreferences()

        # Override certain settings not useful in headless operation.
        java.setPreference('updates.update.check', 'off')
        java.setPreference('tempfiles.saving.warnifoverwriteolder', 'off')
        java.setPreference('tempfiles.recovery.autosave', 'off')
        try:
            # Preference not defined on certain systems, see issue #39.
            java.setPreference('tempfiles.recovery.checkforrecoveries', 'off')
        except Exception:
            logger.debug('Could not turn off check for recovery files.')
        java.setPreference('tempfiles.saving.optimize', 'filesize')

        # Save useful information in instance attributes.
        self.version = backend['name']
        self.cores = cores
        self.host = host
        self.port = port
        self.java = java
Example #3
0
    def __init__(self, cores=None, version=None, port=None, host='localhost'):

        # Make sure this is the first (and only) client created.
        if jpype.isJVMStarted():
            error = 'Only one client can be instantiated at a time.'
            logger.error(error)
            raise RuntimeError(error)

        # Determine relevant folders of the Comsol back-end.
        main = backend.folder(version)
        arch = backend.architecture()
        jre  = main / 'java' / arch / 'jre' / 'bin'
        jvm  = jre / 'server' / 'jvm.dll'
        api  = main / 'plugins' / '*'

        # Manipulate binary search path to only point to Comsol's JVM.
        path = os.environ['PATH']
        os.environ['PATH'] = str(jre)

        # Set environment variable so Comsol will restrict cores at start-up.
        if cores:
            os.environ['COMSOL_NUM_THREADS'] = str(cores)

        # Start the Java virtual machine.
        logger.info('Starting Java virtual machine.')
        jpype.startJVM(str(jvm), classpath=str(api), convertStrings=False)
        from com.comsol.model.util import ModelUtil as java
        logger.info('Java virtual machine has started.')

        # Restore the original search path.
        os.environ['PATH'] = path

        # Connect to a server if requested.
        if port is not None:
            logger.info(f'Connecting to server "{host}" at port {port}.')
            java.connect(host, port)
        # Otherwise initialize stand-alone session.
        else:
            logger.info('Initializing stand-alone client.')
            graphics = False
            java.initStandalone(graphics)
            logger.info('Stand-alone client initialized.')

        # Log number of used processor cores as reported by Comsol instance.
        cores = java.getPreference('cluster.processor.numberofprocessors')
        cores = int(str(cores))
        noun = 'core' if cores == 1 else 'cores'
        logger.info(f'Running on {cores} processor {noun}.')

        # Override the default setting of certain preferences.
        java.setPreference('updates.update.check', 'off')
        java.setPreference('tempfiles.saving.warnifoverwriteolder', 'off')
        java.setPreference('tempfiles.recovery.autosave', 'off')
        java.setPreference('tempfiles.recovery.checkforrecoveries', 'off')
        java.setPreference('tempfiles.saving.optimize', 'filesize')

        # Save setup in instance attributes.
        self.cores   = cores
        self.version = version
        self.host    = host
        self.port    = port
        self.folder  = main
        self.java    = java
Example #4
0
    def __init__(self, cores=None, version=None, port=None, host='localhost'):

        # Make sure this is the one and only client.
        if jpype.isJVMStarted():
            error = 'Only one client can be instantiated at a time.'
            logger.error(error)
            raise NotImplementedError(error)

        # Discover Comsol back-end.
        backend = discovery.backend(version)

        # Set environment variables for loading external libraries.
        system = platform.system()
        root = backend['root']
        if system == 'Windows':
            var = 'PATH'
            if var in os.environ:
                path = os.environ[var].split(os.pathsep)
            else:
                path = []
            lib = str(root / 'lib' / 'glnxa64')
            if lib not in path:
                os.environ[var] = os.pathsep.join([lib] + path)
        elif system == 'Linux':
            lib = str(root / 'lib' / 'glnxa64')
            gcc = str(root / 'lib' / 'glnxa64' / 'gcc')
            ext = str(root / 'ext' / 'graphicsmagick' / 'glnxa64')
            cad = str(root / 'ext' / 'cadimport' / 'glnxa64')
            pre = str(root / 'java' / 'glnxa64' / 'jre' / 'lib' / 'amd64' /
                      'libjsig.so')
            var = 'LD_LIBRARY_PATH'
            if var in os.environ:
                path = os.environ[var].split(os.pathsep)
            else:
                path = []
            if lib not in path:
                os.environ[var] = os.pathsep.join([lib, gcc, ext, cad] + path)
            vars = ('MAGICK_CONFIGURE_PATH', 'MAGICK_CODER_MODULE_PATH',
                    'MAGICK_FILTER_MODULE_PATH')
            for var in vars:
                os.environ[var] = ext
            os.environ['LD_PRELOAD'] = pre
            os.environ['LC_NUMERIC'] = os.environ['LC_ALL'] = 'C'
        elif system == 'Darwin':
            var = 'DYLD_LIBRARY_PATH'
            if var in os.environ:
                path = os.environ[var].split(os.pathsep)
            else:
                path = []
            lib = str(root / 'lib' / 'maci64')
            ext = str(root / 'ext' / 'graphicsmagick' / 'maci64')
            cad = str(root / 'ext' / 'cadimport' / 'maci64')
            if lib not in path:
                os.environ[var] = os.pathsep.join([lib, ext, cad] + path)

        # Instruct Comsol to limit number of processor cores to use.
        if cores:
            os.environ['COMSOL_NUM_THREADS'] = str(cores)

        # Start the Java virtual machine.
        logger.info(f'JPype version is {jpype.__version__}.')
        logger.info('Starting Java virtual machine.')
        jpype.startJVM(str(backend['jvm']),
                       classpath=str(root / 'plugins' / '*'),
                       convertStrings=False)
        logger.info('Java virtual machine has started.')

        # Initialize a stand-alone client if no server port given.
        from com.comsol.model.util import ModelUtil as java
        if port is None:
            logger.info('Initializing stand-alone client.')
            graphics = True
            java.initStandalone(graphics)
            logger.info('Stand-alone client initialized.')
        # Otherwise skip stand-alone initialization and connect to server.
        else:
            logger.info(f'Connecting to server "{host}" at port {port}.')
            java.connect(host, port)

        # Log number of used processor cores as reported by Comsol instance.
        cores = java.getPreference('cluster.processor.numberofprocessors')
        cores = int(str(cores))
        noun = 'core' if cores == 1 else 'cores'
        logger.info(f'Running on {cores} processor {noun}.')

        # Load Comsol settings from disk so as to not just use defaults.
        java.loadPreferences()

        # Override certain settings not useful in headless operation.
        java.setPreference('updates.update.check', 'off')
        java.setPreference('tempfiles.saving.warnifoverwriteolder', 'off')
        java.setPreference('tempfiles.recovery.autosave', 'off')
        try:
            java.setPreference('tempfiles.recovery.checkforrecoveries', 'off')
        except Exception:
            logger.warning('Could not turn off check for recovery files.')
        java.setPreference('tempfiles.saving.optimize', 'filesize')

        # Save useful information in instance attributes.
        self.version = backend['name']
        self.cores = cores
        self.host = host
        self.port = port
        self.java = java