-
Notifications
You must be signed in to change notification settings - Fork 296
/
SerializeSparsityPattern.mo
139 lines (125 loc) · 4.77 KB
/
SerializeSparsityPattern.mo
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package SerializeSparsityPattern
import List;
import SimCode;
import Util;
function serialize
input SimCode.SimCode code;
output String dummy = "";
protected
array<Integer> columnPointers, rowIndices, columns;
String fname;
algorithm
for jac in code.jacobianMatrices loop
if not listEmpty(jac.sparsity) then
fname := code.fileNamePrefix + "_Jac" + jac.matrixName + ".bin";
columnPointers := listArray(0 :: list(listLength(Util.tuple22(column)) for column in jac.sparsity));
rowIndices := listArray(List.flatten(list(Util.tuple22(column) for column in jac.sparsity)));
serializeJacobian(fname, arrayLength(columnPointers), arrayLength(rowIndices), columnPointers, rowIndices);
for color in jac.coloredCols loop
columns := listArray(color);
serializeColor(fname, arrayLength(columns), columns);
end for;
end if;
end for;
end serialize;
// *********************
// write to binary stuff
// *********************
protected
function serializeJacobian
input String name;
input Integer numCols;
input Integer nnz;
input array<Integer> colPtrs;
input array<Integer> rowInds;
external "C" serializeJ(name, numCols, nnz, colPtrs, rowInds) annotation(Include="
extern FILE* omc_fopen(const char *filename, const char *mode);
extern size_t omc_fwrite(void *buffer, size_t size, size_t count, FILE *stream);
static void serializeJ(const char* name, int numCols, int nnz, modelica_metatype colPtrs, modelica_metatype rowInds)
{
unsigned int i, j;
size_t count;
FILE* pFile = omc_fopen(name, \"wb\");
if (pFile == NULL) {
throwStreamPrint(NULL, \"Could not open sparsity pattern file %s.\", name);
}
/* compute and write sparsePattern->leadindex */
j = 0;
for (i = 0; i < numCols; i++) {
j += (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(colPtrs)[i]);
count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
if (count != 1) {
throwStreamPrint(NULL, \"Error while writing sparsePattern->leadindex. Expected %d, got %zu\", 1, count);
}
}
/* write sparsePattern->index */
for (i = 0; i < nnz; i++) {
j = (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(rowInds)[i]);
count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
if (count != 1) {
throwStreamPrint(NULL, \"Error while writing sparsePattern->index. Expected %d, got %zu\", 1, count);
}
}
fclose(pFile);
}
");
end serializeJacobian;
function serializeColor
input String name;
input Integer size;
input array<Integer> columns;
external "C" serializeC(name, size, columns) annotation(Include="
extern FILE* omc_fopen(const char *filename, const char *mode);
extern size_t omc_fwrite(void *buffer, size_t size, size_t count, FILE *stream);
static void serializeC(const char* name, int size, modelica_metatype columns)
{
unsigned int i, j;
size_t count;
FILE* pFile = fopen(name, \"ab\");
if (pFile == NULL) {
throwStreamPrint(NULL, \"Could not open sparsity pattern file %s.\", name);
}
/* write sparsePattern->colorCols */
for (i = 0; i < size; i++) {
j = (unsigned int) MMC_UNTAGFIXNUM(MMC_STRUCTDATA(columns)[i]);
count = omc_fwrite(&j, sizeof(unsigned int), 1, pFile);
if (count != 1) {
throwStreamPrint(NULL, \"Error while writing sparsePattern->colorCols. Expected %d, got %zu\", 1, count);
}
}
fclose(pFile);
}
");
end serializeColor;
annotation(__OpenModelica_Interface="backend");
end SerializeSparsityPattern;