Skip to content

Commit e8f4a04

Browse files
committed
- Added API call solveLinearSystem (same functionality to be used in the backend later) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8928 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent c1606e7 commit e8f4a04

File tree

10 files changed

+99
-6
lines changed

10 files changed

+99
-6
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,5 +1311,15 @@ function uriToFilename "Handles modelica:// and file:// URI's. The result is an
13111311
external "builtin";
13121312
end uriToFilename;
13131313

1314+
function solveLinearSystem
1315+
"Solve A*X = B
1316+
Returns info>0: Singular for element i. info<0: Bad input."
1317+
input Real[size(B,1),size(B,1)] A;
1318+
input Real[:] B;
1319+
output Real[size(B,1)] X;
1320+
output Integer info;
1321+
external "builtin";
1322+
end solveLinearSystem;
1323+
13141324
end Scripting;
13151325
end OpenModelica;

Compiler/FrontEnd/ValuesUtil.mo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,20 @@ algorithm
11171117
end matchcontinue;
11181118
end valueReals;
11191119

1120+
public function arrayValueReals "function: valueReals
1121+
1122+
Return the real value of a Value. If the value is an integer,
1123+
it is cast to a real.
1124+
"
1125+
input Value inValue;
1126+
output list<Real> outReal;
1127+
protected
1128+
list<Values.Value> vals;
1129+
algorithm
1130+
Values.ARRAY(valueLst=vals) := inValue;
1131+
outReal := valueReals(vals);
1132+
end arrayValueReals;
1133+
11201134
public function valueNeg "function: valueNeg
11211135
author: PA
11221136

Compiler/Makefile.common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ AST = $(top_builddir)/Parser/libomparse_rml.a $(top_builddir)/Parser/libantlr3.a
1818
RTOBJ = \
1919
$(srcdir)/runtime/runtime.a \
2020
$(srcdir)/../c_runtime/libc_runtime.a \
21-
$(srcdir)/modpar/libmodpar.a
21+
$(srcdir)/modpar/libmodpar.a
2222

2323
FRONTEND_DIR=$(srcdir)/FrontEnd
2424
BACKEND_DIR=$(srcdir)/BackEnd

Compiler/Script/CevalScript.mo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ algorithm
671671
AbsynDep.Depends aDep;
672672
Absyn.ComponentRef crefCName;
673673
list<tuple<String,Values.Value>> resultValues;
674-
list<Real> timeStamps;
674+
list<Real> timeStamps,realVals;
675675
list<DAE.Exp> expLst;
676676
list<tuple<String,list<String>>> deps;
677677
Absyn.CodeNode codeNode;
@@ -1848,6 +1848,12 @@ algorithm
18481848

18491849
case (cache,env,"uriToFilename",_,st,msg)
18501850
then (cache,Values.STRING(""),st);
1851+
1852+
case (cache,env,"solveLinearSystem",{Values.ARRAY(valueLst=vals),v},st,msg)
1853+
equation
1854+
(realVals,i) = System.solveLinearSystem(Util.listMap(vals,ValuesUtil.arrayValueReals),ValuesUtil.arrayValueReals(v));
1855+
v = ValuesUtil.makeArray(Util.listMap(realVals,ValuesUtil.makeReal));
1856+
then (cache,Values.TUPLE({v,Values.INTEGER(i)}),st);
18511857

18521858
end matchcontinue;
18531859
end cevalInteractiveFunctions2;

Compiler/Util/System.mo

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,17 @@ end openModelicaPlatform;
801801
public function getGCStatus
802802
output Integer used;
803803
output Integer allocated;
804-
external "C" System_getGCStatus(used,allocated);
804+
external "C" System_getGCStatus(used,allocated) annotation(Library = "omcruntime");
805805
end getGCStatus;
806806

807+
public function solveLinearSystem
808+
"Solves A*X = B. Fails and sets error buffer on failure.
809+
Returns info>0: Singular for element i. info<0: Bad input."
810+
input list<list<Real>> A;
811+
input list<Real> B;
812+
output list<Real> X;
813+
output Integer info;
814+
external "C" info=SystemImpl__solveLinearSystem(A,B,X) annotation(Library = "omcruntime");
815+
end solveLinearSystem;
816+
807817
end System;

Compiler/omc_debug/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endif
3232

3333
LDFLAGS = @LDFLAGS@ -lm -L$(RMLHOME)/lib/plain -lrml_g -lpthread \
3434
$(LIBSOCKET) $(CORBALIBS) $(LIBLPSOLVE55) \
35-
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ $(SQLITE3)
35+
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ -llapack $(SQLITE3)
3636

3737
PROG = omcd
3838
RMLC = @rmlc_bin@ -g -Wc,-O1 #-Wr,-East,-Ecps,-Efol

Compiler/omc_profiler/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ endif
2929

3030
LDFLAGS = @LDFLAGS@ -lm -L$(RMLHOME)/lib/plain -lrml_p \
3131
$(LIBSOCKET) $(CORBALIBS) $(LIBLPSOLVE55) \
32-
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ $(SQLITE3)
32+
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ -llapack $(SQLITE3)
3333

3434
PROG = omcp
3535
RMLC = @rmlc_bin@ -p

Compiler/omc_release/Makefile.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ endif
2929

3030
LDFLAGS = @LDFLAGS@ -lm -L$(RMLHOME)/lib/plain -lrml \
3131
$(LIBSOCKET) $(CORBALIBS) $(LIBLPSOLVE55) \
32-
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ $(SQLITE3)
32+
-L../../build/lib/omc @LIBSENDDATA_LDFLAGS@ -llapack $(SQLITE3)
3333

3434
PROG = omc
3535
RMLC = @rmlc_bin@

Compiler/runtime/System_rml.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,3 +2012,14 @@ RML_BEGIN_LABEL(System__getGCStatus)
20122012
RML_TAILCALLK(rmlSC);
20132013
}
20142014
RML_END_LABEL
2015+
2016+
2017+
RML_BEGIN_LABEL(System__solveLinearSystem)
2018+
{
2019+
void *res;
2020+
rmlA1 = mk_icon(SystemImpl__solveLinearSystem(rmlA0,rmlA1,&res));
2021+
rmlA0 = res;
2022+
RML_TAILCALLK(rmlSC);
2023+
}
2024+
RML_END_LABEL
2025+

Compiler/runtime/systemimpl.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,48 @@ static int SystemImpl__uriToClassAndPath(const char *uri, const char **scheme, c
13181318
return 1;
13191319
}
13201320

1321+
int SystemImpl__solveLinearSystem(void *lA, void *lB, void **res)
1322+
{
1323+
int sz = 0,i,j;
1324+
void *tmp = lB;
1325+
double *A,*B;
1326+
integer *ipiv;
1327+
integer info = 0,nrhs=1,lda,ldb;
1328+
while (RML_NILHDR != RML_GETHDR(tmp)) {
1329+
sz++;
1330+
tmp = RML_CDR(tmp);
1331+
}
1332+
A = (double*) malloc(sz*sz*sizeof(double));
1333+
assert(A != NULL);
1334+
B = (double*) malloc(sz*sizeof(double));
1335+
assert(B != NULL);
1336+
for (i=0; i<sz; i++) {
1337+
tmp = RML_CAR(lA);
1338+
for (j=0; j<sz; j++) {
1339+
A[j*sz+i] = rml_prim_get_real(RML_CAR(tmp));
1340+
tmp = RML_CDR(tmp);
1341+
}
1342+
B[i] = rml_prim_get_real(RML_CAR(lB));
1343+
lA = RML_CDR(lA);
1344+
lB = RML_CDR(lB);
1345+
}
1346+
ipiv = (integer*) calloc(sz,sizeof(integer));
1347+
assert(ipiv != 0);
1348+
lda = sz;
1349+
ldb = sz;
1350+
dgesv_(&sz,&nrhs,A,&lda,ipiv,B,&ldb,&info);
1351+
1352+
tmp = mk_nil();
1353+
while (sz--) {
1354+
tmp = mk_cons(mk_rcon(B[sz]),tmp);
1355+
}
1356+
free(A);
1357+
free(B);
1358+
free(ipiv);
1359+
*res = tmp;
1360+
return info;
1361+
}
1362+
13211363
#ifdef __cplusplus
13221364
}
13231365
#endif

0 commit comments

Comments
 (0)