pypa / packaging-problems

An issue tracker for the problems in packaging
150 stars 35 forks source link

error: command '/usr/bin/clang++' failed with exit code 1 #772

Open acse-yw11823 opened 5 months ago

acse-yw11823 commented 5 months ago

Problem description

Hello, I am using the following to install the SeismicMesh but I meet the error, tried 3 hours to solve it now, pls help me code

pip install SeismicMesh

error:

(base) (firedrake) yw11823@IC-FVFL80FW1WGC spyro % pip install SeismicMesh

Collecting SeismicMesh
  Using cached SeismicMesh-3.6.2.tar.gz (20.5 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from SeismicMesh) (1.26.3)
Collecting segyio (from SeismicMesh)
  Using cached segyio-1.9.12-cp311-cp311-macosx_11_0_arm64.whl.metadata (2.8 kB)
Requirement already satisfied: scipy in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from SeismicMesh) (1.11.4)
Collecting meshio (from SeismicMesh)
  Using cached meshio-5.3.5-py3-none-any.whl.metadata (11 kB)
Collecting h5py (from SeismicMesh)
  Using cached h5py-3.11.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (2.5 kB)
Requirement already satisfied: matplotlib in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from SeismicMesh) (3.8.4)
Collecting mpi4py (from SeismicMesh)
  Using cached mpi4py-3.1.6-cp311-cp311-macosx_11_0_arm64.whl
Collecting pyamg (from SeismicMesh)
  Using cached pyamg-5.1.0-cp311-cp311-macosx_11_0_arm64.whl.metadata (8.1 kB)
Collecting pytest-codeblocks (from SeismicMesh)
  Using cached pytest_codeblocks-0.17.0-py3-none-any.whl.metadata (5.4 kB)
Requirement already satisfied: contourpy>=1.0.1 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (1.2.0)
Requirement already satisfied: cycler>=0.10 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (4.25.0)
Requirement already satisfied: kiwisolver>=1.3.1 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (1.4.5)
Requirement already satisfied: packaging>=20.0 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (23.1)
Requirement already satisfied: pillow>=8 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (10.3.0)
Requirement already satisfied: pyparsing>=2.3.1 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (3.1.1)
Requirement already satisfied: python-dateutil>=2.7 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from matplotlib->SeismicMesh) (2.8.2)
Collecting rich (from meshio->SeismicMesh)
  Using cached rich-13.7.1-py3-none-any.whl.metadata (18 kB)
Collecting pytest>=7.0.0 (from pytest-codeblocks->SeismicMesh)
  Using cached pytest-8.2.2-py3-none-any.whl.metadata (7.6 kB)
Collecting iniconfig (from pytest>=7.0.0->pytest-codeblocks->SeismicMesh)
  Using cached iniconfig-2.0.0-py3-none-any.whl.metadata (2.6 kB)
Collecting pluggy<2.0,>=1.5 (from pytest>=7.0.0->pytest-codeblocks->SeismicMesh)
  Using cached pluggy-1.5.0-py3-none-any.whl.metadata (4.8 kB)
Requirement already satisfied: six>=1.5 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib->SeismicMesh) (1.16.0)
Collecting markdown-it-py>=2.2.0 (from rich->meshio->SeismicMesh)
  Using cached markdown_it_py-3.0.0-py3-none-any.whl.metadata (6.9 kB)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/homebrew/Caskroom/miniconda/base/lib/python3.11/site-packages (from rich->meshio->SeismicMesh) (2.15.1)
Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich->meshio->SeismicMesh)
  Using cached mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)
Using cached h5py-3.11.0-cp311-cp311-macosx_11_0_arm64.whl (2.9 MB)
Using cached meshio-5.3.5-py3-none-any.whl (166 kB)
Using cached pyamg-5.1.0-cp311-cp311-macosx_11_0_arm64.whl (1.6 MB)
Using cached pytest_codeblocks-0.17.0-py3-none-any.whl (7.7 kB)
Using cached segyio-1.9.12-cp311-cp311-macosx_11_0_arm64.whl (81 kB)
Using cached pytest-8.2.2-py3-none-any.whl (339 kB)
Using cached rich-13.7.1-py3-none-any.whl (240 kB)
Using cached markdown_it_py-3.0.0-py3-none-any.whl (87 kB)
Using cached pluggy-1.5.0-py3-none-any.whl (20 kB)
Using cached iniconfig-2.0.0-py3-none-any.whl (5.9 kB)
Using cached mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Building wheels for collected packages: SeismicMesh
  Building wheel for SeismicMesh (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for SeismicMesh (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [84 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-11.1-arm64-cpython-311
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh
      copying SeismicMesh/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh
      copying SeismicMesh/__about__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh
      creating build/lib.macosx-11.1-arm64-cpython-311/geo
      copying geo/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/geo
      copying geo/geometry.py -> build/lib.macosx-11.1-arm64-cpython-311/geo
      creating build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/line_base.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/spline.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/bspline.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/ellipse_arc.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/plane_surface.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/surface_loop.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/curve_loop.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/line.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/polygon.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/volume.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/point.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/geometry.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/circle_arc.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/dummy.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      copying common/surface.py -> build/lib.macosx-11.1-arm64-cpython-311/common
      creating build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/disk.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/box.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/torus.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/boolean.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/ball.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/geometry.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/rectangle.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/cone.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/wedge.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      copying occ/cylinder.py -> build/lib.macosx-11.1-arm64-cpython-311/occ
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing
      copying SeismicMesh/sizing/size_function.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing
      copying SeismicMesh/sizing/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing
      copying SeismicMesh/sizing/mesh_size_function.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/decomp
      copying SeismicMesh/decomp/blocker.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/decomp
      copying SeismicMesh/decomp/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/decomp
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry
      copying SeismicMesh/geometry/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry
      copying SeismicMesh/geometry/utils.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry
      copying SeismicMesh/geometry/rotation.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry
      copying SeismicMesh/geometry/signed_distance_functions.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/migration
      copying SeismicMesh/migration/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/migration
      copying SeismicMesh/migration/migration.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/migration
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation
      copying SeismicMesh/generation/mesh_generator.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation
      copying SeismicMesh/generation/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation
      copying SeismicMesh/generation/utils.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing/cpp
      copying SeismicMesh/sizing/cpp/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing/cpp
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry/cpp
      copying SeismicMesh/geometry/cpp/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/geometry/cpp
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/migration/cpp
      copying SeismicMesh/migration/cpp/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/migration/cpp
      creating build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation/cpp
      copying SeismicMesh/generation/cpp/__init__.py -> build/lib.macosx-11.1-arm64-cpython-311/SeismicMesh/generation/cpp
      running build_ext
      clang -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/include -arch arm64 -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/include -arch arm64 -I/opt/homebrew/Caskroom/miniconda/base/include/python3.11 -c flagcheck.cpp -o flagcheck.o -std=c++17
      building '_FastHJ' extension
      creating build/temp.macosx-11.1-arm64-cpython-311
      creating build/temp.macosx-11.1-arm64-cpython-311/SeismicMesh
      creating build/temp.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing
      creating build/temp.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing/cpp
      clang -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/include -arch arm64 -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/include -arch arm64 -I/private/var/folders/hx/g9tst_895vb2cxj5f1f0lghh0000gp/T/pip-build-env-vj6wm35d/overlay/lib/python3.11/site-packages/pybind11/include -I/opt/homebrew/Caskroom/miniconda/base/include/python3.11 -c SeismicMesh/sizing/cpp/FastHJ.cpp -o build/temp.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing/cpp/FastHJ.o -std=c++17 -mmacosx-version-min=10.14 -fvisibility=hidden -g0
      SeismicMesh/sizing/cpp/FastHJ.cpp:81:7: warning: unused variable 'maxSz' [-Wunused-variable]
        int maxSz = dims[0] * dims[1] * dims[2];
            ^
      1 warning generated.
      clang++ -bundle -undefined dynamic_lookup -Wl,-rpath,/opt/homebrew/Caskroom/miniconda/base/lib -L/opt/homebrew/Caskroom/miniconda/base/lib -Wl,-rpath,/opt/homebrew/Caskroom/miniconda/base/lib -L/opt/homebrew/Caskroom/miniconda/base/lib build/temp.macosx-11.1-arm64-cpython-311/SeismicMesh/sizing/cpp/FastHJ.o -lgmp -lmpfr -o build/lib.macosx-11.1-arm64-cpython-311/_FastHJ.cpython-311-darwin.so -mmacosx-version-min=10.14
      ld: warning: duplicate -rpath '/opt/homebrew/Caskroom/miniconda/base/lib' ignored
      ld: library 'gmp' not found
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      error: command '/usr/bin/clang++' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for SeismicMesh
Failed to build SeismicMesh
ERROR: Could not build wheels for SeismicMesh, which is required to install pyproject.toml-based projects

and I installed gmp where :

(base) (firedrake) yw11823@IC-FVFL80FW1WGC spyro % brew info gmp                             

==> gmp: stable 6.3.0 (bottled), HEAD
GNU multiple precision arithmetic library
https://gmplib.org/
Installed
/opt/homebrew/Cellar/gmp/6.3.0 (21 files, 3.3MB) *
  Poured from bottle using the formulae.brew.sh API on 2024-03-07 at 22:17:08
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/g/gmp.rb
License: LGPL-3.0-or-later or GPL-2.0-or-later
==> Dependencies
Build: autoconf ✔, automake ✔, libtool ✔
==> Options
--HEAD
        Install HEAD version
==> Analytics
install: 133,867 (30 days), 383,296 (90 days), 1,458,764 (365 days)
install-on-request: 7,955 (30 days), 17,367 (90 days), 85,672 (365 days)
build-error: 136 (30 days)

and I edited the terminal where

(base) (firedrake) yw11823@IC-FVFL80FW1WGC spyro % open ~/.zshrc 

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/homebrew/Caskroom/miniconda/base/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/homebrew/Caskroom/miniconda/base/etc/profile.d/conda.sh" ]; then
        . "/opt/homebrew/Caskroom/miniconda/base/etc/profile.d/conda.sh"
    else
        export PATH="/opt/homebrew/Caskroom/miniconda/base/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

export PATH="/opt/homebrew/bin:$PATH"
export PATH="/opt/homebrew/opt/m4/bin:$PATH"
export C_INCLUDE_PATH="/opt/homebrew/opt/gmp/include:$C_INCLUDE_PATH"
export LIBRARY_PATH="/opt/homebrew/opt/gmp/lib:$LIBRARY_PATH"

# Add Homebrew's Python 3.11 to the PATH
# export PATH="/opt/homebrew/opt/python@3.11/bin:$PATH"

export PATH="/Library/TeX/texbin:$PATH"
henryiii commented 5 months ago

The package is archived and I think it only supported Intel macos and Python 3.7, which was never ported to ARM macs.

https://pypi.org/project/SeismicMesh/3.6.2/#files

https://github.com/krober10nd/SeismicMesh

acse-yw11823 commented 5 months ago

Many thanks for the reply. My current version of pyrhon is :

(firedrake) (base) yw11823@IC-FVFL80FW1WGC demos % python --version
Python 3.11.9

And I will go through the links now.

acse-yw11823 commented 5 months ago

Hello, I did what I could do so far and also asked my professor. I am currently using the Mac Apple M1. Should I download the SeismicMesh file following the link https://pypi.org/project/SeismicMesh/3.6.2/#files and install through it again? Or there is no way I can use this package on my laptop :(

henryiii commented 5 months ago

The package is archived, which means no one can update it or even open an issue. If you want to resurrect it, you'll likely need to fork it and maintain your own copy. Unless you are willing to do that, you'll probably want to look around for a maintained library instead.

I think the problem you are initially facing is that homebrew on Apple Silicon installs to /opt/homebrew instead of /usr/local, and it doesn't know to look there. You might be able to progress farther if you set:

export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/lib
export INCLUDE_PATH=$INCLUDE_PATH:/opt/homebrew/include

Edit: it's not respecting those variables; even on an Intel Mac it needs /usr/local/lib and it's not present.

But no promises for abandonware!

henryiii commented 5 months ago

Also, I recommend using a non-Conda Python if you are not installing with conda, especially if you are building packages from source. Conda's packages are compiled with it's own compiler stack, and it's really only designed to be used with Conda-build in a controlled environment (like the one conda-forge uses). You can get Python from official installers, homebrew, etc. But so far, that's not your problem (and it probably is fine most of the time).

henryiii commented 5 months ago

I was able to get it to build on 3.12 (on Intel, no idea if it works) by cloning it, and adding the following hard coded paths to setup.cfg:

[build_ext]
include-dirs = /usr/local/include
library-dirs = /usr/local/lib

(You'll want to replace /usr/local with /opt/homebrew on your machine, I'm not using Apple Silicon as my daily driver yet). With brew install gmp cgal first, it built with pip install -v ..

If you want to fork and maintain, a better solution would be to switch to scikit-build-core; there's already a CMakeLists, you'd just need to update that, and CMake would find the libraries for you. I can start that if you are interested, but I can't maintain long-term. ;)

acse-yw11823 commented 5 months ago

Many thanks!!!! Will hae a look now

acse-yw11823 commented 5 months ago

Thanks a lot!!!!! It works by using this method!!! I do appreciate your time! thanksss

I was able to get it to build on 3.12 (on Intel, no idea if it works) by cloning it, and adding the following hard coded paths to setup.cfg:

[build_ext]
include-dirs = /usr/local/include
library-dirs = /usr/local/lib

(You'll want to replace /usr/local with /opt/homebrew on your machine, I'm not using Apple Silicon as my daily driver yet). With brew install gmp cgal first, it built with pip install -v ..

If you want to fork and maintain, a better solution would be to switch to scikit-build-core; there's already a CMakeLists, you'd just need to update that, and CMake would find the libraries for you. I can start that if you are interested, but I can't maintain long-term. ;)

henryiii commented 5 months ago

I've tried making a scikit-build-core version, would you like to try:

pip install -v git+https://github.com/henryiii/SeismicMesh

? Changes: https://github.com/henryiii/SeismicMesh/commit/a9482b057346b9b38e7e5c2e5db76ec08bfe7cab

henryiii commented 5 months ago

Oops, forgot pybind11. Added.

acse-yw11823 commented 5 months ago

Hello, this might be a bit random but I have a ROL-related error when I running the seismic imaging model. This is my code:

from firedrake import *
import numpy as np
import finat
from ROL.firedrake_vector import FiredrakeVector as FeVector
import ROL
from mpi4py import MPI

import spyro

# import gc

outdir = "fwi_p5/"

model = {}

model["opts"] = {
    "method": "KMV",  # either CG or KMV
    "quadrature": "KMV",  # Equi or KMV
    "degree": 5,  # p order
    "dimension": 2,  # dimension
    "regularization": True,  # regularization is on?
    "gamma": 1.0e-6,  # regularization parameter
}
model["parallelism"] = {
    "type": "spatial",
}
model["mesh"] = {
    "Lz": 3.5,  # depth in km - always positive
    "Lx": 17.0,  # width in km - always positive
    "Ly": 0.0,  # thickness in km - always positive
    "meshfile": "/Users/yw11823/ACSE/irp/spyro/paper/FWI_2D_DATA/meshes/marmousi_exact.msh",
    "initmodel": "/Users/yw11823/ACSE/irp/spyro/paper/FWI_2D_DATA/velocity_models/marmousi_exact.hdf5",
    "truemodel": "not_used.hdf5",
}
model["BCs"] = {
    "status": True,  # True or false
    "outer_bc": "non-reflective",  # None or non-reflective (outer boundary condition)
    "damping_type": "polynomial",  # polynomial, hyperbolic, shifted_hyperbolic
    "exponent": 2,  # damping layer has a exponent variation
    "cmax": 4.5,  # maximum acoustic wave velocity in PML - km/s
    "R": 1e-6,  # theoretical reflection coefficient
    "lz": 0.9,  # thickness of the PML in the z-direction (km) - always positive
    "lx": 0.9,  # thickness of the PML in the x-direction (km) - always positive
    "ly": 0.0,  # thickness of the PML in the y-direction (km) - always positive
}
model["acquisition"] = {
    "source_type": "Ricker",
    "num_sources": 40,
    "source_pos": spyro.create_transect((-0.01, 1.0), (-0.01, 15.0), 40),
    "frequency": 5.0,
    "delay": 1.0,
    "num_receivers": 500,
    "receiver_locations": spyro.create_transect((-0.10, 0.1), (-0.10, 17.0), 500),
}
model["timeaxis"] = {
    "t0": 0.0,  # Initial time for event
    "tf": 6.00,  # Final time for event
    "dt": 0.001,
    "amplitude": 1,  # the Ricker has an amplitude of 1.
    "nspool": 1000,  # how frequently to output solution to pvds
    "fspool": 10,  # how frequently to save solution to RAM
}
comm = spyro.utils.mpi_init(model)
# if comm.comm.rank == 0 and comm.ensemble_comm.rank == 0:
#    fil = open("FUNCTIONAL_FWI_P5.txt", "w")
mesh, V = spyro.io.read_mesh(model, comm)
vp = spyro.io.interpolate(model, mesh, V, guess=True)
if comm.ensemble_comm.rank == 0:
    File("guess_velocity.pvd", comm=comm.comm).write(vp)
sources = spyro.Sources(model, mesh, V, comm)
receivers = spyro.Receivers(model, mesh, V, comm)
wavelet = spyro.full_ricker_wavelet(
    dt=model["timeaxis"]["dt"],
    tf=model["timeaxis"]["tf"],
    freq=model["acquisition"]["frequency"],
)

if comm.ensemble_comm.rank == 0:
    control_file = File(outdir + "control.pvd", comm=comm.comm)
    grad_file = File(outdir + "grad.pvd", comm=comm.comm)

quad_rule = finat.quadrature.make_quadrature(
    V.finat_element.cell, V.ufl_element().degree(), "KMV"
)
dxlump = dx(scheme=quad_rule)

water = np.where(vp.dat.data[:] < 1.51)

class L2Inner(object):
    def __init__(self):
        self.A = assemble(
            TrialFunction(V) * TestFunction(V) * dxlump, mat_type="matfree"
        )
        self.Ap = as_backend_type(self.A).mat()

    def eval(self, _u, _v):
        upet = as_backend_type(_u).vec()
        vpet = as_backend_type(_v).vec()
        A_u = self.Ap.createVecLeft()
        self.Ap.mult(upet, A_u)
        return vpet.dot(A_u)

kount = 0

def regularize_gradient(vp, dJ, gamma):
    """Tikhonov regularization"""
    m_u = TrialFunction(V)
    m_v = TestFunction(V)
    mgrad = m_u * m_v * dx(scheme=qr_x)
    ffG = dot(grad(vp), grad(m_v)) * dx(scheme=qr_x)
    G = mgrad - ffG
    lhsG, rhsG = lhs(G), rhs(G)
    gradreg = Function(V)
    grad_prob = LinearVariationalProblem(lhsG, rhsG, gradreg)
    grad_solver = LinearVariationalSolver(
        grad_prob,
        solver_parameters={
            "ksp_type": "preonly",
            "pc_type": "jacobi",
            "mat_type": "matfree",
        },
    )
    grad_solver.solve()
    dJ += gamma * gradreg
    return dJ

class Objective(ROL.Objective):
    def __init__(self, inner_product):
        ROL.Objective.__init__(self)
        self.inner_product = inner_product
        self.p_guess = None
        self.misfit = 0.0
        self.p_exact_recv = spyro.io.load_shots(model, comm)

    def value(self, x, tol):
        """Compute the functional"""
        J_total = np.zeros((1))
        self.p_guess, p_guess_recv = spyro.solvers.forward(
            model,
            mesh,
            comm,
            vp,
            sources,
            wavelet,
            receivers,
        )
        self.misfit = spyro.utils.evaluate_misfit(
            model, p_guess_recv, self.p_exact_recv
        )
        J_total[0] += spyro.utils.compute_functional(model, self.misfit, velocity=vp)
        J_total = COMM_WORLD.allreduce(J_total, op=MPI.SUM)
        J_total[0] /= comm.ensemble_comm.size
        if comm.comm.size > 1:
            J_total[0] /= comm.comm.size
        return J_total[0]

    def gradient(self, g, x, tol):
        """Compute the gradient of the functional"""
        dJ = Function(V, name="gradient")
        dJ_local = spyro.solvers.gradient(
            model,
            mesh,
            comm,
            vp,
            receivers,
            self.p_guess,
            self.misfit,
        )
        if comm.ensemble_comm.size > 1:
            comm.allreduce(dJ_local, dJ)
        else:
            dJ = dJ_local
        dJ /= comm.ensemble_comm.size
        if comm.comm.size > 1:
            dJ /= comm.comm.size
        # regularize the gradient if asked.
        if model["opts"]["regularization"]:
            gamma = model["opts"]["gamma"]
            dJ = regularize_gradient(vp, dJ, gamma)
        # mask the water layer
        dJ.dat.data[water] = 0.0
        # Visualize
        if comm.ensemble_comm.rank == 0:
            grad_file.write(dJ)
        g.scale(0)
        g.vec += dJ

    def update(self, x, flag, iteration):
        vp.assign(Function(V, x.vec, name="velocity"))
        # If iteration reduces functional, save it.
        if iteration >= 0:
            if comm.ensemble_comm.rank == 0:
                control_file.write(vp)

paramsDict = {
    "General": {"Secant": {"Type": "Limited-Memory BFGS", "Maximum Storage": 10}},
    "Step": {
        "Type": "Augmented Lagrangian",
        "Augmented Lagrangian": {
            "Subproblem Step Type": "Line Search",
            "Subproblem Iteration Limit": 5.0,
        },
        "Line Search": {"Descent Method": {"Type": "Quasi-Newton Step"}},
    },
    "Status Test": {
        "Gradient Tolerance": 1e-16,
        "Iteration Limit": 100,
        "Step Tolerance": 1.0e-16,
    },
}

params = ROL.ParameterList(paramsDict, "Parameters")

inner_product = L2Inner()

obj = Objective(inner_product)

u = Function(V, name="velocity").assign(vp)
opt = FeVector(u.vector(), inner_product)
# Add control bounds to the problem (uses more RAM)
xlo = Function(V)
xlo.interpolate(Constant(1.0))
x_lo = FeVector(xlo.vector(), inner_product)

xup = Function(V)
xup.interpolate(Constant(5.0))
x_up = FeVector(xup.vector(), inner_product)

bnd = ROL.Bounds(x_lo, x_up, 1.0)

# Set up the line search
algo = ROL.Algorithm("Line Search", params)

algo.run(opt, obj, bnd)

if comm.ensemble_comm.rank == 0:
    File("res.pvd", comm=comm.comm).write(obj.vp)

# fil.close()

And this is my error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[44], [line 238](vscode-notebook-cell:?execution_count=44&line=238)
    [235](vscode-notebook-cell:?execution_count=44&line=235) bnd = ROL.Bounds(x_lo, x_up, 1.0)
    [237](vscode-notebook-cell:?execution_count=44&line=237) # Set up the line search
--> [238](vscode-notebook-cell:?execution_count=44&line=238) algo = ROL.Algorithm("Line Search", params)
    [240](vscode-notebook-cell:?execution_count=44&line=240) algo.run(opt, obj, bnd)
    [242](vscode-notebook-cell:?execution_count=44&line=242) if comm.ensemble_comm.rank == 0:

TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. _ROL.Algorithm(arg0: ROL::Step<double>, arg1: _ROL.StatusTest)

Invoked with: 'Line Search', <_ROL.ParameterList object at 0x17f3fc2f0>

I tried to solve it but no idea for now. By any chance, you might be familiar with this error. If you are not familiar that would be fine. I posted the discussion on PYROL GitHub but it seems like no one is there :(

Many thanks

henryiii commented 5 months ago

Don’t know much about it, but from the pybind11 generated error message, the arguments here are wrong: ROL.Algorithm("Line Search", params). The first argument has to be a “Step” instance, but you passed a string. Second one has to be a ParameterList, which I’m guessing it might be.