0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OpenSWPCのシミュレーションプログラムのコンパイルで、NetCDFが開けない問題を解決する

Last updated at Posted at 2023-05-12

今回、問題になったのは、

Fatal Error: Can't open module file ‘netcdf.mod’ for reading at (1): No such file or directory compilation terminated.

というエラーでした。

説明

OpenSWPCは、Fortran90で主に地震動の波動計算などのシミュレーションができるコードがオープンソースになっていて、OpenSWPCから飛べるGithubからクローンできるのですが、ここのプログラム内では、NetCDFというデータ形式が利用されています。
ただ、githubからOpenSWPCのリポジトリをクローンしただけではNetCDFは利用できず、別途インストールする必要があります。
しかしこのNetCDFを利用可能にする時に詰まってしまいました。

なんせ使ってる人が少なく、ネットにもほぼヒントがないので苦労しました。
とりあえずOpenSWPCをコンパイルするところまでの手順を説明します。

環境

macOS
(また、Homebrewで必要なものをinstallする前提で話します。)
事前に、fortranを、brew installでインストールしておいてください。

brew install fortran

コンパイルするまでの本来の手順

①クローンする
②NetCDFを利用可能な状態にする
③srcでmakeコマンドを打ち、src内の全てのF90をコンパイルする(swpc_*内からでもいい)

しかしこの②で詰まったので、簡単に②をクリアする手順を説明します。

MacPorts をインストールする

MacPortsは、パッケージ管理システムです。これを使って必要なものをインストールします。
なぜhomebrewでインストールしないのかというと、
OpenSWPCに書かれているところでは、

! apt install libnetcdf-dev libnetcdff-dev

によって、libnetcdf-devとlibnetcdff-devの2つをインストールする手順が必要

とされていますが、手っ取り早く、代わりに使うhomebrewではlibnetcdf-devなんてないと言われてしまって、インストールできません。

そこでMacPortsを利用するため、ターミナルで、次をうちます。(時間がかかります)

sudo port install netcdf-cxx netcdf-fortran

(必要に応じてですが、とりあえず上を打つ前に、バージョンアップをお勧めします。)

sudo port selfupdate

長いインストールの後、

port installed | grep netcdf

で、netcdfが存在することを確認してください。
後者は、PCにインストールされた、"netcdf"という文字列を含むパッケージのみをフィルタリングして表示してくれます。

設定を確認する

最後に、OpenSWPCを開き、src/makefile.arch、src/makefile-tools.archを見て、

## Mac OSX gfortran and netcdf provided by homebrew

という部分が、それぞれ、

## Mac OSX gfortran and netcdf provided by homebrew
##
ifeq ($(arch),mac-gfortran)

  FC      = gfortran
  FFLAGS  = -O2 -ffast-math -D_INFO -I../include
  NCFLAG  = -D_NETCDF
  NCLIB   = -L/opt/local/lib
  NCINC   = -I/opt/local/include
  NETCDF  = -lnetcdf -lnetcdff

  ifeq ($(debug),true)
    FFLAGS  = -Wall -pedantic -fbounds-check -O -Wuninitialized \
	          -ffpe-trap=invalid,zero,overflow -fbacktrace -O0 \
	          -D_INFO -D_DEBUG -I../include
  endif

となっていることを確認してください。(ここは今の所直しは必要なく、デフォルトのままでいいとは思います)

これでNetCDFのインストールが完了したので、makeコマンドでコンパイルできます。

以上!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?