Skip to content

Commit

Permalink
Include Comsol installation found on PATH in discovery (#89)
Browse files Browse the repository at this point in the history
The discovery mechanism was refactored. We are now first looking for
Comsol executables, i.e. `comsol.exe` on Windows or `comsol` on Linux
and macOS. We can then add to the list of candidates the Comsol
executable that the shell would find, if any, i.e. what would be called
when running `comsol` in the terminal.

This makes discovery more flexible, since that last installation does
not necessarily have to be in a default location. So finding it requires
no extra configuration other than "adding Comsol to PATH", which the
Comsol installer offers as an option (named not exactly like that, but
something to that effect).

So when there is but this one Comsol installation, MPh should work out
of the box, without the need to create symbolic links if that installation
is in a non-default location.

We retain the possibility of selecting a specific Comsol back-end, for
the purpose of running tests/benchmark across versions. If the
back-end on the executable search path (a.k.a. the `PATH`) is the same
as one of those already found by searching the system (file system on
Linux and macOS, the Registry on Windows), then we'll ignore it.

To achieve this functionality, we are now starting our search for other
Comsol components such as the Java VM from the location of the
Comsol executable, i.e. `comsol.exe` or the `comsol` shell script.
We then look for `comsol.ini` in the same folder and parse it in order
to get the relative path to the Java VM.
  • Loading branch information
john-hen committed Aug 31, 2022
1 parent 477cab8 commit b2fe3e6
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 253 deletions.
44 changes: 26 additions & 18 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
# Installation

MPh is [available on PyPI][pypi] and can be readily installed via
```none
MPh is [available on PyPI] and can be readily installed via
```
pip install MPh
```
Run `pip uninstall MPh` in order to remove the package from your system.

Requires [JPype][jpype] for the bridge from Python to [Comsol's
Java API][japi] and [NumPy][numpy] for returning (fast) numerical arrays.
`pip` makes sure the two Python dependencies are installed and adds them
if missing.
Run `pip uninstall MPh` to remove the package from your system.

Requires [JPype] for the bridge from Python to [Comsol's Java API]
and [NumPy] for returning (fast) numerical arrays. Pip makes sure the
two Python dependencies are installed and adds them if missing.

Comsol, obviously, you need to license and install yourself. Versions
from Comsol 5.1 onward are expected to work, though only Comsol 5.5 and
newer have been rigorously tested. A separate Java run-time environment
is *not* required as Comsol ships with one already built in.

On Linux and macOS, Comsol is expected to be found in its respective
default location. On Windows, any custom install location is supported,
as the installer stores that information in the central registry.
Comsol is expected to be installed in the default location suggested by
its installer. Though on Windows, custom locations are also supported,
as the installer stores that information in the central registry, which
MPh looks up.

Additionally, whichever Comsol installation starts when you run `comsol`
in the console, will be found as well, even if in a custom location.

If you want to be able to select an alternative Comsol installation via
MPh's API, like by passing the `version` option to {func}`mph.start`,
and that Comsol version happens to be installed in a custom location,
you can [create a symbolic link] in `~/.local` on Linux and in
`~/Application` on macOS. Have it point to the corresponding Comsol
folder and give the link a name that starts with `comsol`.

Though if, on Linux, you do have Comsol installed in a custom location,
[create a symbolic link][symlink] in `~/.local`, have it point to that
Comsol folder, and give it a name that starts with `comsol`.

[pypi]: https://pypi.python.org/pypi/mph
[jpype]: https://jpype.readthedocs.io
[japi]: https://comsol.com/documentation/COMSOL_ProgrammingReferenceManual.pdf
[numpy]: https://numpy.org
[symlink]: https://www.howtogeek.com/287014
[available on PyPI]: https://pypi.python.org/pypi/mph
[JPype]: https://jpype.readthedocs.io
[Comsol's Java API]: https://comsol.com/documentation/COMSOL_ProgrammingReferenceManual.pdf
[NumPy]: https://numpy.org
[create a symbolic link]: https://www.howtogeek.com/287014
10 changes: 5 additions & 5 deletions mph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
########################################
import jpype # Java bridge
import jpype.imports # Java object imports
import platform # platform information
import os # operating system
from pathlib import Path # file-system paths
from logging import getLogger # event logging
Expand Down Expand Up @@ -158,15 +157,16 @@ def __init__(self, cores=None, version=None, port=None, host='localhost'):
# 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():
if discovery.system == 'Windows' and faulthandler.is_enabled():
log.debug('Turning off Python fault handlers.')
faulthandler.disable()

# On Windows, prepend the Java folder to the library search path.
# On Windows, prepend the JRE bin folder to the library search path.
# See issue #49.
if platform.system() == 'Windows':
if discovery.system == 'Windows':
path = os.environ['PATH']
os.environ['PATH'] = str(backend['java']) + os.pathsep + path
jre = backend['jvm'].parent.parent
os.environ['PATH'] = str(jre) + os.pathsep + path

# Start the Java virtual machine.
log.debug(f'JPype version is {jpype.__version__}.')
Expand Down
4 changes: 2 additions & 2 deletions mph/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
########################################
# Globals #
########################################
log = getLogger(__package__) # event log
system = platform.system() # operating system
log = getLogger(__package__) # event log

options = {
'session': 'platform-dependent',
Expand Down Expand Up @@ -59,7 +60,6 @@ def location():
in the home directory on Linux, and in `Application Support` on
macOS.
"""
system = platform.system()
if system == 'Windows':
return Path(os.environ['APPDATA'])/'MPh'
elif system == 'Linux':
Expand Down

0 comments on commit b2fe3e6

Please sign in to comment.