Skip to content

Commit 7f0b6a3

Browse files
author
Marcus Walther
committed
- SparseMatrix-Support added for umfpack-linear solver
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@21338 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 10b2b6f commit 7f0b6a3

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed

SimulationRuntime/cpp/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ENDIF(NOT ANALYZATION_MODE)
99

1010

1111
option(ANALYZATION_MODE "ANALYZATION_MODE" OFF)
12+
option(USE_UMFPACK "USE_UMFPACK" OFF)
1213

1314
IF(ANALYZATION_MODE)
1415
message(STATUS "Using analyzation mode")
@@ -175,6 +176,19 @@ add_definitions(-DPMC_USE_SUNDIALS)
175176
INSTALL(FILES ${SUNDIALS_LIBS} DESTINATION bin)
176177
endif()
177178

179+
if(USE_UMFPACK)
180+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/Solver/UmfPack/")
181+
find_package(SuiteSparse)
182+
message(STATUS "Using UmfPack")
183+
list(FIND SUITESPARSE_LIBRARIES "umfpack" umfpack_found)
184+
if(umfpack_found EQUAL -1)
185+
message(FATAL_ERROR "Umfpack not found")
186+
endif(umfpack_found EQUAL -1)
187+
add_definitions(-DUSE_UMFPACK)
188+
else(USE_UMFPACK)
189+
message(STATUS "UmfPack disabled")
190+
endif(USE_UMFPACK)
191+
178192
#Mico corba
179193
if(USE_MICO)
180194
find_library(MICO_LIBRARY mico2313

SimulationRuntime/cpp/Core/Math/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 2.6)
22

33
project(${MathName})
44
# add the solver default implementation library
5-
add_library(${MathName} STATIC ArrayOperations.cpp Functions.cpp )
5+
add_library(${MathName} STATIC ArrayOperations.cpp Functions.cpp SparseMatrix.cpp)
66
if (UNIX)
77
set_target_properties(${MathName} PROPERTIES COMPILE_FLAGS -fPIC)
88
endif(UNIX)
@@ -13,7 +13,7 @@ install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Math/Functions.h
1313
${CMAKE_SOURCE_DIR}/Include/Core/Math/ArrayOperations.h
1414
${CMAKE_SOURCE_DIR}/Include/Core/Math/Utility.h
1515
${CMAKE_SOURCE_DIR}/Include/Core/Math/Constants.h
16-
DESTINATION include/omc/cpp/Core/Math)
17-
install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Math/ILapack.h
18-
${CMAKE_SOURCE_DIR}/Include/Core/Math/OMAPI.h
16+
${CMAKE_SOURCE_DIR}/Include/Core/Math/SparseMatrix.h
17+
${CMAKE_SOURCE_DIR}/Include/Core/Math/ILapack.h
18+
${CMAKE_SOURCE_DIR}/Include/Core/Math/OMAPI.h
1919
DESTINATION include/omc/cpp/Core/Math)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "Math/SparseMatrix.h"
2+
3+
#ifdef USE_UMFPACK
4+
void sparse_matrix::build(sparse_inserter& ins) {
5+
if(n==-1) {
6+
n=ins.content.rbegin()->first.first;
7+
} else {
8+
if(n-1!=ins.content.rbegin()->first.first) {
9+
throw std::runtime_error("size doesn't match");
10+
}
11+
}
12+
size_t n=ins.content.size();
13+
Ap.resize(this->n+1,0);
14+
Ai.resize(n);
15+
Ax.resize(n);
16+
unsigned int j=0;
17+
int rowold=1;
18+
for(map< pair<int,int>, double>::iterator it=ins.content.begin(); it!=ins.content.end(); it++) {
19+
if(it->first.first+1==rowold) {
20+
++Ap[rowold];
21+
} else {
22+
Ap[it->first.first+1]=Ap[rowold]+1;
23+
rowold=it->first.first+1;
24+
}
25+
Ai[j]=it->first.second;
26+
Ax[j]=it->second;
27+
++j;
28+
}
29+
}
30+
31+
int sparse_matrix::solve(const double* b, double * x) {
32+
int status, sys;
33+
double Control [UMFPACK_CONTROL], Info [UMFPACK_INFO] ;
34+
void *Symbolic, *Numeric ;
35+
umfpack_di_defaults (Control) ;
36+
status = umfpack_di_symbolic (sparse_matrix::n, sparse_matrix::n, &sparse_matrix::Ap[0], &sparse_matrix::Ai[0], &sparse_matrix::Ax[0], &Symbolic, Control, Info) ;
37+
status = umfpack_di_numeric (&sparse_matrix::Ap[0], &sparse_matrix::Ai[0], &sparse_matrix::Ax[0], Symbolic, &Numeric, Control, Info);
38+
status = umfpack_di_solve (sys, &sparse_matrix::Ap[0], &sparse_matrix::Ai[0], &sparse_matrix::Ax[0], x, b, Numeric, Control, Info);
39+
umfpack_di_free_symbolic (&Symbolic);
40+
umfpack_di_free_numeric (&Numeric);
41+
return 0;
42+
}
43+
#endif
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
//#include <map>
4+
//#include <vector>
5+
#include "Modelica.h"
6+
//#include <stdexcept>
7+
8+
#ifdef USE_UMFPACK
9+
#include "umfpack.h"
10+
11+
using std::map;
12+
using std::pair;
13+
using std::make_pair;
14+
15+
struct sparse_inserter {
16+
struct t2 {
17+
int i;
18+
int j;
19+
map< pair<int,int>, double> & content;
20+
t2(int i, int j, map< pair<int,int>, double> & c): i(i), j(j), content(c) {}
21+
void operator=(double t) {
22+
content[make_pair(j,i)]=t;
23+
}
24+
};
25+
26+
struct t1 {
27+
int i;
28+
map< pair<int,int>, double> & content;
29+
t1(int i,map< pair<int,int>, double> & c): i(i), content(c) {}
30+
t2 operator[](size_t j) {
31+
t2 res(i,j,content);
32+
return res;
33+
}
34+
};
35+
36+
37+
map< pair<int,int>, double> content;
38+
t1 operator[](size_t i) {
39+
t1 res(i,content);
40+
return res;
41+
}
42+
43+
};
44+
45+
struct sparse_matrix {
46+
std::vector<int> Ap;
47+
std::vector<int> Ai;
48+
std::vector<double> Ax;
49+
int n;
50+
sparse_matrix(int n=-1): n(n) {}
51+
52+
void build(sparse_inserter& ins);
53+
int solve(const double* b,double* x);
54+
};
55+
56+
#endif

SimulationRuntime/cpp/Include/Core/Modelica.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ typedef boost::function<void (unordered_map<string,unsigned int>&,unordered_map<
134134
#include <Math/Functions.h>
135135
#include <Math/ArrayOperations.h>
136136
#include <Math/Utility.h>
137+
#include <Math/SparseMatrix.h>
137138
#include "HistoryImpl.h"
138139
#include "DataExchange/Policies/TextfileWriter.h"
139140

SimulationRuntime/cpp/Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ builddir_bin=$(top_builddir)/build/bin
88
builddir_lib=$(top_builddir)/build/lib/omc
99
builddir_inc=$(top_builddir)/build/include/omc
1010

11+
ANALYZATION_MODE_COMMAND="-DANALYZATION_MODE=ON"
12+
USE_UMFPACK_COMMAND="-DUSE_UMFPACK=ON"
13+
1114
runtimeCpp: clean
12-
ifeq ("$(ANALYZATION_MODE)","true")
13-
cd ./Build && echo "change to Build" && cmake -DANALYZATION_MODE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX:PATH="$(builddir_build)" ../
14-
$(MAKE) -C Build;
15-
else
16-
cd ./Build && echo "change to Build" && cmake -D CMAKE_BUILD_TYPE=RelWithDebInfo -D CMAKE_INSTALL_PREFIX:PATH="$(builddir_build)" ../
17-
$(MAKE) -C Build;
15+
ifneq ("$(ANALYZATION_MODE)","true")
16+
$(eval ANALYZATION_MODE_COMMAND="")
1817
endif
18+
ifneq ("$(USE_UMFPACK)","true")
19+
$(eval USE_UMFPACK_COMMAND="")
20+
endif
21+
cd ./Build && echo "change to Build" && cmake $(ANALYZATION_MODE) $(USE_UMFPACK_COMMAND) -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX:PATH="$(builddir_build)" ../
22+
$(MAKE) -C Build;
1923

2024
install: runtimeCpp
2125
(cd Build; $(MAKE) install)

0 commit comments

Comments
 (0)