1 |
/* ASCEND modelling environment |
2 |
Copyright (C) 2007 Carnegie Mellon University |
3 |
|
4 |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
6 |
the Free Software Foundation; either version 2, or (at your option) |
7 |
any later version. |
8 |
|
9 |
This program is distributed in the hope that it will be useful, |
10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
GNU General Public License for more details. |
13 |
|
14 |
You should have received a copy of the GNU General Public License |
15 |
along with this program; if not, write to the Free Software |
16 |
Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
Boston, MA 02111-1307, USA. |
18 |
*//** |
19 |
Implementation of the dense matrix module, as used by the LSODE Integrator |
20 |
in ASCEND. |
21 |
*//* |
22 |
by John Pye, Jan 2007. Pulled out the routines from lsode.c and added |
23 |
a file export function so that these matrices can be viewed using outside |
24 |
tools. |
25 |
*/ |
26 |
|
27 |
#include "densemtx.h" |
28 |
#include <utilities/ascMalloc.h> |
29 |
#include <utilities/error.h> |
30 |
#include <mmio.h> |
31 |
|
32 |
DenseMatrix densematrix_create_empty(){ |
33 |
DenseMatrix result = {NULL,0,0}; |
34 |
return result; |
35 |
} |
36 |
|
37 |
/** |
38 |
Allocate space for a new dense matrix object of rows x cols |
39 |
*/ |
40 |
DenseMatrix densematrix_create(unsigned nrows, unsigned ncols){ |
41 |
unsigned c; |
42 |
DenseMatrix result; |
43 |
assert(nrows>0); |
44 |
assert(ncols>0); |
45 |
result.data = ASC_NEW_ARRAY(double *, nrows); |
46 |
for (c=0;c<nrows;c++) { |
47 |
result.data[c] = ASC_NEW_ARRAY_CLEAR(double, ncols); |
48 |
} |
49 |
result.nrows = nrows; |
50 |
result.ncols = ncols; |
51 |
return result; |
52 |
} |
53 |
|
54 |
/** |
55 |
Deallocate space used by the dense matrix |
56 |
*/ |
57 |
void densematrix_destroy(DenseMatrix matrix){ |
58 |
unsigned c; |
59 |
if(matrix.data != NULL){ |
60 |
for(c=0;c<matrix.nrows;c++){ |
61 |
if(matrix.data[c]){ |
62 |
ascfree((char *)matrix.data[c]); |
63 |
} |
64 |
} |
65 |
ASC_FREE((char *)matrix.data); |
66 |
matrix.data = NULL; |
67 |
} |
68 |
} |
69 |
|
70 |
void densematrix_write_mmio(DenseMatrix matrix, FILE *fp){ |
71 |
int i,j; |
72 |
|
73 |
MM_typecode matcode; |
74 |
|
75 |
mm_initialize_typecode(&matcode); |
76 |
mm_set_matrix(&matcode); |
77 |
mm_set_array(&matcode); |
78 |
mm_set_real(&matcode); |
79 |
|
80 |
mm_write_banner(fp, matcode); |
81 |
fprintf(fp,"%% Dense matrix output from ASCEND modelling environment\n"); |
82 |
fprintf(fp,"%% ASCEND (c) 2007 Carnegie Mellon University\n"); |
83 |
fprintf(fp,"%% rows = %d, cols = %d\n", matrix.nrows, matrix.ncols); |
84 |
|
85 |
mm_write_mtx_array_size(fp, matrix.nrows, matrix.ncols); |
86 |
|
87 |
for(j=0;j<matrix.nrows;++j){ |
88 |
for(i=0;i<matrix.ncols;++i){ |
89 |
fprintf(fp,"%0.20g\n",DENSEMATRIX_ELEM(matrix,i,j)); |
90 |
} |
91 |
} |
92 |
|
93 |
CONSOLE_DEBUG("Wrote dense matrix (%u x %u) to file", matrix.nrows, matrix.ncols); |
94 |
} |
95 |
|