opensim-org / opensim-core

SimTK OpenSim C++ libraries and command-line applications, and Java/Python wrapping.
https://opensim.stanford.edu
Apache License 2.0
792 stars 317 forks source link

Building from source and python scripting do not work on M1 Mac #3444

Closed guoling13 closed 1 year ago

guoling13 commented 1 year ago

I have downloaded the OpenSim 4.4 GUI on my M1 Mac and it is working well. However, I am working on a project that will require accessing and modifying the source code, either directly in the C++ code, or using scripting in Python.

I tried setting up OpenSim scripting in Python following the instructions from here https://simtk-confluence.stanford.edu:8443/display/OpenSim/Scripting+in+Python. I was able to install the opensim package but could not import it. This was the error I got:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/__init__.py", line 9, in <module>
    from .simbody import *
  File "/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/simbody.py", line 13, in <module>
    from . import _simbody
ImportError: dlopen(/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/_simbody.so, 0x0002): tried: '/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/_simbody.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/_simbody.so' (no such file), '/Users/cc/miniconda/envs/kinematics/lib/python3.9/site-packages/opensim/_simbody.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

From what I understand ‘arm64’ refers to the chip in the M1 mac. I also tried building the conda package following instructions here: https://github.com/opensim-org/conda-opensim, but this seems to require python 3.7 and other dependencies which my M1 mac doesn’t support.

I also tried building opensim-core following this: https://github.com/opensim-org/opensim-core/wiki/Build-Instructions. I ran the .sh script and a lot of things were installed but I got this error at the end:

CMake Error at /opt/homebrew/Cellar/cmake/3.26.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Python3 (missing: Python3_NumPy_INCLUDE_DIRS NumPy) (found
  suitable version "3.9.16", minimum required is "3.6")
Call Stack (most recent call first):
  /opt/homebrew/Cellar/cmake/3.26.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  /opt/homebrew/Cellar/cmake/3.26.3/share/cmake/Modules/FindPython/Support.cmake:3766 (find_package_handle_standard_args)
  /opt/homebrew/Cellar/cmake/3.26.3/share/cmake/Modules/FindPython3.cmake:551 (include)
  CMakeLists.txt:541 (find_package)

-- Configuring incomplete, errors occurred!

My computer specs MacBook Pro 14", 2021 Apple M1 Max, 32GB memory OS Ventura 13.3.1

aymanhab commented 1 year ago

The error message you reported is due to the fact that the OpenSim application is built with x86_64 architecture /stack, while the python you're trying to use is built for M1 (arm64). There are two solutions to this problem:

  1. Install python for x86_64 and run through the emulator (rosetta I think).
  2. Use the jupyter notebooks online which run python on linux in the background (link here for examples https://github.com/opensim-org/opensim-models/tree/master/notebooks)

The error message when building from source is due to missing numpy, you should install numpy first to the python you're using for the build and then build will go through

The published conda package is also based on x86_64 architecture, if you want to build the stack on M1 (arm64) and make a PR to help that would be much appreciated.

aymanhab commented 1 year ago

@carmichaelong do we have enough info to document building the stack (with or without Moco) on Apple silicon? I remember you had some version working locally at some point. If that's documented in another issue/PR please point there and you can close this one. Thank you.

carmichaelong commented 1 year ago

Issue: https://github.com/opensim-org/opensim-core/issues/3168 PR: https://github.com/opensim-org/opensim-core/pull/3192

The issue has some instructions to build using the branch from the PR that worked for another user. We can close this issue since the other one can track this as well.