Skip to content

Commit

Permalink
Merge pull request coin-or#133 from zanellia/mumps
Browse files Browse the repository at this point in the history
Adding support for MUMPS sparse linear solver
  • Loading branch information
apotschka committed Oct 17, 2022
2 parents 67ae315 + fc9753e commit 9bd3374
Show file tree
Hide file tree
Showing 18 changed files with 16,162 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "external/ThirdParty-Mumps"]
path = external/ThirdParty-Mumps
url = git@github.com:coin-or-tools/ThirdParty-Mumps.git
25 changes: 23 additions & 2 deletions Makefile
Expand Up @@ -36,10 +36,26 @@ include make.mk
##


all: bin src examples
ifeq ($(DEF_SOLVER), SOLVER_MUMPS)
EXTERNAL = mumps
else
EXTERNAL =
endif

all: $(EXTERNAL) bin src examples
#src_aw testing

src:
ifeq ($(DEF_SOLVER), SOLVER_MUMPS)
mumps:
@echo $(QPOASESROOT)
@cd external/ThirdParty-Mumps; \
if [ -d "MUMPS" ]; then \
echo "Found MUMPS source code."; \
else get.Mumps; ./configure --prefix=$(PWD)/external/mumps_installation; fi; \
make && make install
endif

src: $(EXTERNAL)
@cd $@; ${MAKE} -s

bin:
Expand All @@ -64,6 +80,11 @@ debugging:
@cd $@; ${MAKE} -s

clean:
ifeq ($(DEF_SOLVER), SOLVER_MUMPS)
@echo Cleaning up \(mumps\)
@cd external/ThirdParty-Mumps && ${MAKE} -s clean
@cd external && ${RM} -rf mumps_installation
endif
@cd src && ${MAKE} -s clean
@cd examples && ${MAKE} -s clean
@cd bin && ${RM} -f *.* *{EXE}
Expand Down
58 changes: 58 additions & 0 deletions examples/generate_sparse_qp/main.py
@@ -0,0 +1,58 @@
import numpy as np
import scipy as sp
from scipy.sparse import csc_matrix, coo_matrix, csr_matrix, lil_matrix, random
from jinja2 import Environment
from jinja2.loaders import FileSystemLoader
import os

seed = 42
density = 0.1
gamma = 0.01

out_file_name = 'qp_data.hpp'
in_file_name = 'qp_data.in.hpp'

NV = 100
NC = 10

H = csc_matrix((NV, NV))
A = csc_matrix((NV, NV))

myinf = 1e10

for i in range(NV):
H[i,i] = 1.0

# H = H + gamma*random(NV, NV, density=density, format='csc', random_state=seed)
# H = H.T*H

for i in range(NC):
A[i,i] = 1.0

H_ri = H.indices
H_cp = H.indptr
H_val = H.data
H_nnz = H.nnz

A_ri = A.indices
A_cp = A.indptr
A_val = A.data
A_nnz = A.nnz

g = np.ones((NV,1))

lb = -myinf*np.ones((NV, 1))
ub = myinf*np.ones((NV, 1))

lbA = -np.ones((NC, 1))
ubA = np.ones((NC, 1))

print('rendering templated C++ code...')
env = Environment(loader=FileSystemLoader(os.path.dirname(os.path.abspath(__file__))))
tmpl = env.get_template(in_file_name)

code = tmpl.render(NV = NV, NC = NC, H_cp = H_cp, H_ri = H_ri, H_val = H_val, H_nnz = H_nnz, \
A_cp = A_cp, A_ri = A_ri, A_val = A_val, A_nnz = A_nnz, g = g, lb = lb, ub = ub, lbA = lbA, ubA = ubA)

with open(out_file_name, "w+") as f:
f.write(code.replace('inf', 'Inf'))
108 changes: 108 additions & 0 deletions examples/generate_sparse_qp/qp_data.in.hpp
@@ -0,0 +1,108 @@
/*
* This file is part of qpOASES.
*
* qpOASES -- An Implementation of the Online Active Set Strategy.
* Copyright (C) 2007-2017 by Hans Joachim Ferreau, Andreas Potschka,
* Christian Kirches et al. All rights reserved.
*
* qpOASES is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* qpOASES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with qpOASES; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/


/**
* \author Andrea Zanelli
* \version 3.2
* \date 2022
*
* QP data generated by a Python script for testing purposes.
*/


USING_NAMESPACE_QPOASES

#define NV {{NV}}
#define NC {{NC}}

const real_t Inf = INFTY;

sparse_int_t H_ri[] = {
{% for d in H_ri %}
{{ d }},
{%- endfor %}
};

sparse_int_t H_cp[] = {
{% for d in H_cp %}
{{ d }},
{%- endfor %}
};

real_t H_val[] = {
{% for d in H_val %}
{{ d }},
{%- endfor %}
};

sparse_int_t A_ri[] = {
{% for d in A_ri %}
{{ d }},
{%- endfor %}
};

sparse_int_t A_cp[] = {
{% for d in A_cp %}
{{ d }},
{%- endfor %}
};

real_t A_val[] = {
{% for d in A_val %}
{{ d }},
{%- endfor %}
};

real_t g[] = {
{% for d in g %}
{{ d[0] }},
{%- endfor %}
};

real_t lb[] = {
{% for d in lb %}
{{ d[0] }},
{%- endfor %}
};

real_t ub[] = {
{% for d in ub %}
{{ d[0] }},
{%- endfor %}
};

real_t lbA[] = {
{% for d in lbA %}
{{ d[0] }},
{%- endfor %}
};

real_t ubA[] = {
{% for d in ubA %}
{{ d[0] }},
{%- endfor %}
};

long H_nnz = {{ H_nnz }};
long A_nnz = {{ A_nnz }};

0 comments on commit 9bd3374

Please sign in to comment.