forked from thearn/pandemic
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_pyoptsparse_ipopt.sh
290 lines (251 loc) · 7.49 KB
/
build_pyoptsparse_ipopt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env bash
# Finds/downloads and unpacks pyOptSparse, IPOPT, and deps source
# archives to current directory. Chdirs to each directory in turn
# to build and install each package (except for pyOptSparse if
# build is disabled by command line options).
#
# Default values:
# IPOPT 3.12.x has a broken configure on Mac
IPOPT_VER=3.13.1
HSL_VER=2014.01.17
PREFIX=$HOME/ipopt
LINEAR_SOLVER=MUMPS
BUILD_PYOPTSPARSE=1
PYOPTSPARSE_BRANCH=v1.2
COMPILER_SUITE=GNU
INCLUDE_SNOPT=0
SNOPT_DIR=SNOPT
BUILD_TIME=`date +%s`
usage() {
cat <<USAGE
Download, configure, build, and install pyOptSparse with IPOPT
support and dependencies.
Usage:
$0 [-b branch] [-h] [-l linear_solver] [-n] [-p prefix] [-s snopt_dir]
-b branch pyOptSparse git branch. Default: v1.2
-h Display usage and exit.
-l linear_solver One of mumps, hsl, or pardiso. Default: mumps
-n Prepare, but do NOT build/install pyOptSparse.
Default: build & install
-p prefix Where to install. Default: $HOME/ipopt
Note: If older versions are already installed in
this dir, the build may fail. If it does, rename
the directory or removing the old versions.
-s snopt_dir Include SNOPT from snopt_dir. Default: no SNOPT
NOTES:
If HSL is selected as the linear solver, the
coinhsl-archive-${HSL_VER}.tar.gz file must exist in the current
directory. This can be obtained from http://www.hsl.rl.ac.uk/ipopt/
If PARDISO is selected as the linear solver, the Intel compiler suite
with MKL must be available.
Examples:
$0
$0 -l pardiso
$0 -l hsl -n
USAGE
exit 3
}
while getopts ":b:hl:np:s:" opt; do
case ${opt} in
b)
PYOPTSPARSE_BRANCH="$OPTARG" ;;
h)
usage ;;
l)
case ${OPTARG^^} in
MUMPS|HSL)
LINEAR_SOLVER=${OPTARG^^}
COMPILER_SUITE=GNU ;;
PARDISO)
LINEAR_SOLVER=${OPTARG^^}
COMPILER_SUITE=Intel ;;
*)
echo "Unrecognized linear solver specified."
usage ;;
esac
;;
n)
BUILD_PYOPTSPARSE=0 ;;
p)
PREFIX="$OPTARG" ;;
s)
INCLUDE_SNOPT=1
SNOPT_DIR="$OPTARG"
if [ ! -d "$SNOPT_DIR" ]; then
echo "Specified SNOPT source dir $SNOPT_DIR doesn't exist."
exit 1
fi
;;
\?)
echo "Unrecognized option -${OPTARG} specified."
usage ;;
:)
echo "Option -${OPTARG} requires an argument."
usage ;;
esac
done
# Choose compiler and make settings:
case $COMPILER_SUITE in
GNU)
CC=gcc
CXX=g++
FC=gfortran
;;
Intel)
CC=icc
CXX=icpc
FC=ifort
;;
*)
echo "Unknown compiler suite specified."
exit 2
;;
esac
MAKEFLAGS='-j 6'
export CC CXX FC MAKEFLAGS
REQUIRED_CMDS="make $CC $CXX $FC sed git curl tar"
if [ $BUILD_PYOPTSPARSE = 1 ]; then
REQUIRED_CMDS="$REQUIRED_CMDS python pip swig"
fi
####################################################################
set -e
trap 'cmd_failed $? $LINENO' EXIT
cmd_failed() {
if [ "$1" != "0" ]; then
echo "FATAL ERROR: The command failed with error $1 at line $2."
exit 1
fi
}
missing_cmds=''
for c in $REQUIRED_CMDS; do
type -p $c > /dev/null || missing_cmds="$missing_cmds $c"
done
[ -z "$missing_cmds" ] || {
echo "Missing required commands:$missing_cmds"
exit 1
}
# TODO: Pre-check for more deps: lapack, blas, numpy
bkp_dir() {
check_dir=$1
if [ -d "$check_dir" ]; then
echo "Renaming $check_dir to ${check_dir}.bkp.${BUILD_TIME}"
mv "$check_dir" "${check_dir}.bkp.${BUILD_TIME}"
fi
}
install_metis() {
bkp_dir ThirdParty-Metis
# Install METIS
git clone https://github.com/coin-or-tools/ThirdParty-Metis.git
pushd ThirdParty-Metis
./get.Metis
./configure --prefix=$PREFIX
make
make install
popd
}
install_ipopt() {
bkp_dir Ipopt
echo $CC $CXX $FC
if [ $IPOPT_VER = 'MASTER' ]; then
git clone https://github.com/coin-or/Ipopt.git
else
ipopt_file=Ipopt-${IPOPT_VER}.tgz
curl -O https://www.coin-or.org/download/source/Ipopt/$ipopt_file
tar xf $ipopt_file
rm $ipopt_file
mv Ipopt-*${IPOPT_VER}* Ipopt
fi
pushd Ipopt
./configure --prefix=${PREFIX} --disable-java "$@"
make
make install
popd
}
build_pyoptsparse() {
patch_type=$1
bkp_dir pyoptsparse
git clone -b "$PYOPTSPARSE_BRANCH" https://github.com/mdolab/pyoptsparse.git
case $patch_type in
mumps)
sed -i -e "s/coinhsl/coinmumps', 'coinmetis/" pyoptsparse/pyoptsparse/pyIPOPT/setup.py
;;
pardiso)
sed -i -e "s/'coinhsl', //;s/, 'blas', 'lapack'//" pyoptsparse/pyoptsparse/pyIPOPT/setup.py
;;
esac
if [ $INCLUDE_SNOPT = 1 ]; then
cp -a "${SNOPT_DIR}/." ./pyoptsparse/pyoptsparse/pySNOPT/source/.
fi
if [ $BUILD_PYOPTSPARSE = 1 ]; then
python -m pip install sqlitedict
# Necessary for pyoptsparse to find IPOPT:
export IPOPT_INC=$PREFIX/include/coin-or
export IPOPT_LIB=$PREFIX/lib
python -m pip install --no-cache-dir ./pyoptsparse
else
echo -----------------------------------------------------
echo NOT building pyOptSparse by request. Make sure to set
echo these variables before building it yourself:
echo
echo export IPOPT_INC=$PREFIX/include/coin-or
echo export IPOPT_LIB=$PREFIX/lib
echo -----------------------------------------------------
fi
}
install_with_mumps() {
install_metis
bkp_dir ThirdParty-Mumps
# Install MUMPS
git clone https://github.com/coin-or-tools/ThirdParty-Mumps.git
pushd ThirdParty-Mumps
./get.Mumps
./configure --with-metis --with-metis-lflags="-L${PREFIX}/lib -lcoinmetis" \
--with-metis-cflags="-I${PREFIX}/include" --prefix=$PREFIX
make
make install
popd
install_ipopt --with-mumps --with-mumps-lflags="-L${PREFIX}/lib -lcoinmumps" \
--with-mumps-cflags="-I${PREFIX}/include/coin-or/mumps"
# Build and install pyoptsparse
build_pyoptsparse mumps
}
install_with_hsl() {
install_metis
bkp_dir ThirdParty-HSL
# Unpack, build, and install HSL archive lib:
hsl_top=coinhsl-archive-${HSL_VER}
hsl_tar_file=../${hsl_top}.tar.gz
git clone https://github.com/coin-or-tools/ThirdParty-HSL
pushd ThirdParty-HSL
tar xf $hsl_tar_file
mv $hsl_top coinhsl
./configure --prefix=$PREFIX --with-metis \
--with-metis-lflags="-L${PREFIX}/lib -lcoinmetis" \
--with-metis-cflags="-I${PREFIX}/include"
make
make install
popd
install_ipopt --with-hsl --with-hsl-lflags="-L${PREFIX}/lib -lcoinhsl -lcoinmetis" \
--with-hsl-cflags="-I${PREFIX}/include/coin-or/hsl" --disable-linear-solver-loader
build_pyoptsparse hsl
}
install_with_pardiso() {
install_ipopt --with-lapack="-mkl"
# pyOptSparse doesn't do well with Intel compilers, so unset:
unset CC CXX FC
build_pyoptsparse pardiso
}
case $LINEAR_SOLVER in
MUMPS)
install_with_mumps ;;
HSL)
install_with_hsl ;;
PARDISO)
install_with_pardiso ;;
*)
echo "Unknown linear solver specified."
exit 2
;;
esac
echo Done.
exit 0