NEURON
spmatrix.h
Go to the documentation of this file.
1 /*
2  * EXPORTS for sparse matrix routines.
3  *
4  * Author: Advising professor:
5  * Kenneth S. Kundert Alberto Sangiovanni-Vincentelli
6  * UC Berkeley
7  *
8  * This file contains definitions that are useful to the calling
9  * program. In particular, this file contains error keyword
10  * definitions, some macro functions that are used to quickly enter
11  * data into the matrix and the type definition of a data structure
12  * that acts as a template for entering admittances into the matrix.
13  * Also included is the type definitions for the various functions
14  * available to the user.
15  */
16 
17 /*
18  * Revision and copyright information.
19  *
20  * Copyright (c) 1985,86,87,88
21  * by Kenneth S. Kundert and the University of California.
22  *
23  * Permission to use, copy, modify, and distribute this software and
24  * its documentation for any purpose and without fee is hereby granted,
25  * provided that the copyright notices appear in all copies and
26  * supporting documentation and that the authors and the University of
27  * California are properly credited. The authors and the University of
28  * California make no representations as to the suitability of this
29  * software for any purpose. It is provided `as is', without express
30  * or implied warranty.
31  *
32  * $Date: 2003-02-11 19:36:05 +0100 (Tue, 11 Feb 2003) $
33  * $Revision: 3 $
34  */
35 
36 #ifndef spOKAY
37 
38 /*
39  * IMPORTS
40  *
41  * >>> Import descriptions:
42  * spConfig.h
43  * Macros that customize the sparse matrix routines.
44  */
45 #include "spconfig.h"
46 #include <optional>
47 
48 /*
49  * ERROR KEYWORDS
50  *
51  * The actual numbers used in the error codes are not sacred, they can be
52  * changed under the condition that the codes for the nonfatal errors are
53  * less than the code for spFATAL and similarly the codes for the fatal
54  * errors are greater than that for spFATAL.
55  *
56  * >>> Error descriptions:
57  * spOKAY
58  * No error has occurred.
59  * spSMALL_PIVOT
60  * When reordering the matrix, no element was found which satisfies the
61  * absolute threshold criteria. The largest element in the matrix was
62  * chosen as pivot. Non-fatal.
63  * spZERO_DIAG
64  * Fatal error. A zero was encountered on the diagonal the matrix. This
65  * does not necessarily imply that the matrix is singular. When this
66  * error occurs, the matrix should be reconstructed and factored using
67  * spOrderAndFactor().
68  * spSINGULAR
69  * Fatal error. Matrix is singular, so no unique solution exists.
70  * spNO_MEMORY
71  * Fatal error. Indicates that not enough memory is available to handle
72  * the matrix.
73  * spPANIC
74  * Fatal error indicating that the routines are not prepared to
75  * handle the matrix that has been requested. This may occur when
76  * the matrix is specified to be real and the routines are not
77  * compiled for real matrices, or when the matrix is specified to
78  * be complex and the routines are not compiled to handle complex
79  * matrices.
80  * spFATAL
81  * Not an error flag, but rather the dividing line between fatal errors
82  * and warnings.
83  */
84 
85 /* Begin error macros. */
86 #define spOKAY 0
87 #define spSMALL_PIVOT 1
88 #define spZERO_DIAG 2
89 #define spSINGULAR 3
90 #define spNO_MEMORY 4
91 #define spPANIC 5
92 
93 #define spFATAL 2
94 
95 /*
96  * KEYWORD DEFINITIONS
97  *
98  * Here we define what precision arithmetic Sparse will use. Double
99  * precision is suggested as being most appropriate for circuit
100  * simulation and for C. However, it is possible to change spREAL
101  * to a float for single precision arithmetic. Note that in C, single
102  * precision arithmetic is often slower than double precision. Sparse
103  * internally refers to spREALs as RealNumbers.
104  */
105 
106 #define spREAL double
107 
108 /*
109  * PARTITION TYPES
110  *
111  * When factoring a previously ordered matrix using spFactor(), Sparse
112  * operates on a row-at-a-time basis. For speed, on each step, the row
113  * being updated is copied into a full vector and the operations are
114  * performed on that vector. This can be done one of two ways, either
115  * using direct addressing or indirect addressing. Direct addressing
116  * is fastest when the matrix is relatively dense and indirect addressing
117  * is quite sparse. The user can select which partitioning mode is used.
118  * The following keywords are passed to spPartition() and indicate that
119  * Sparse should use only direct addressing, only indirect addressing, or
120  * that it should choose the best mode on a row-by-row basis. The time
121  * required to choose a partition is of the same order of the cost to factor
122  * the matrix.
123  *
124  * If you plan to factor a large number of matrices with the same structure,
125  * it is best to let Sparse choose the partition. Otherwise, you should
126  * choose the partition based on the predicted density of the matrix.
127  */
128 
129 /* Begin partition keywords. */
130 
131 #define spDEFAULT_PARTITION 0
132 #define spDIRECT_PARTITION 1
133 #define spINDIRECT_PARTITION 2
134 #define spAUTO_PARTITION 3
135 
136 /*
137  * MACRO FUNCTION DEFINITIONS
138  *
139  * >>> Macro descriptions:
140  * spADD_REAL_ELEMENT
141  * Macro function that adds data to a real element in the matrix by a
142  * pointer.
143  * spADD_IMAG_ELEMENT
144  * Macro function that adds data to a imaginary element in the matrix by
145  * a pointer.
146  * spADD_COMPLEX_ELEMENT
147  * Macro function that adds data to a complex element in the matrix by a
148  * pointer.
149  * spADD_REAL_QUAD
150  * Macro function that adds data to each of the four real matrix elements
151  * specified by the given template.
152  * spADD_IMAG_QUAD
153  * Macro function that adds data to each of the four imaginary matrix
154  * elements specified by the given template.
155  * spADD_COMPLEX_QUAD
156  * Macro function that adds data to each of the four complex matrix
157  * elements specified by the given template.
158  */
159 
160 /* Begin Macros. */
161 #define spADD_REAL_ELEMENT(element, real) *(element) += real
162 
163 #define spADD_IMAG_ELEMENT(element, imag) *(element + 1) += imag
164 
165 #define spADD_COMPLEX_ELEMENT(element, real, imag) \
166  { \
167  *(element) += real; \
168  *(element + 1) += imag; \
169  }
170 
171 #define spADD_REAL_QUAD(template, real) \
172  { \
173  *((template).Element1) += real; \
174  *((template).Element2) += real; \
175  *((template).Element3Negated) -= real; \
176  *((template).Element4Negated) -= real; \
177  }
178 
179 #define spADD_IMAG_QUAD(template, imag) \
180  { \
181  *((template).Element1 + 1) += imag; \
182  *((template).Element2 + 1) += imag; \
183  *((template).Element3Negated + 1) -= imag; \
184  *((template).Element4Negated + 1) -= imag; \
185  }
186 
187 #define spADD_COMPLEX_QUAD(template, real, imag) \
188  { \
189  *((template).Element1) += real; \
190  *((template).Element2) += real; \
191  *((template).Element3Negated) -= real; \
192  *((template).Element4Negated) -= real; \
193  *((template).Element1 + 1) += imag; \
194  *((template).Element2 + 1) += imag; \
195  *((template).Element3Negated + 1) -= imag; \
196  *((template).Element4Negated + 1) -= imag; \
197  }
198 
199 /*
200  * TYPE DEFINITION FOR COMPONENT TEMPLATE
201  *
202  * This data structure is used to hold pointers to four related elements in
203  * matrix. It is used in conjunction with the routines
204  * spGetAdmittance
205  * spGetQuad
206  * spGetOnes
207  * These routines stuff the structure which is later used by the spADD_QUAD
208  * macro functions above. It is also possible for the user to collect four
209  * pointers returned by spGetElement and stuff them into the template.
210  * The spADD_QUAD routines stuff data into the matrix in locations specified
211  * by Element1 and Element2 without changing the data. The data is negated
212  * before being placed in Element3 and Element4.
213  */
214 
215 /* Begin `spTemplate'. */
216 struct spTemplate {
221 };
222 
223 /*
224  * FUNCTION TYPE DEFINITIONS
225  *
226  * The type of every user accessible function is declared here.
227  */
228 
229 /* Begin function declarations. */
230 extern void spClear(char*);
231 extern spREAL spCondition(char*, spREAL, int*);
232 extern char* spCreate(int, int, int*);
233 extern void spDeleteRowAndCol(char*, int, int);
234 extern void spDestroy(char*);
235 extern int spElementCount(char*);
236 extern int spError(char*);
237 extern int spFactor(char*);
238 extern int spFileMatrix(char*, char*, char*, int, int, int);
239 extern int spFileStats(char*, char*, char*);
240 extern int spFillinCount(char*);
241 extern int spGetAdmittance(char*, int, int, struct spTemplate*);
242 extern spREAL* spGetElement(char*, int, int);
243 extern char* spGetInitInfo(spREAL*);
244 extern int spGetOnes(char*, int, int, int, struct spTemplate*);
245 extern int spGetQuad(char*, int, int, int, int, struct spTemplate*);
246 extern int spGetSize(char*, int);
247 extern int spInitialize(char*, int (*)());
248 extern void spInstallInitInfo(spREAL*, char*);
249 extern spREAL spLargestElement(char*);
250 extern void spMNA_Preorder(char*);
251 extern spREAL spNorm(char*);
252 extern int spOrderAndFactor(char*, spREAL[], spREAL, spREAL, int);
253 extern void spPartition(char*, int);
254 extern void spPrint(char*, int, int, int);
255 extern spREAL spPseudoCondition(char*);
256 extern spREAL spRoundoff(char*, spREAL);
257 extern void spScale(char*, spREAL[], spREAL[]);
258 extern void spSetReal(char*);
259 extern void spStripFills(char*);
260 extern void spWhereSingular(char*, int*, int*);
261 
262 /* Functions with argument lists that are dependent on options. */
263 extern void spDeterminant(char*, int*, spREAL*, std::optional<spREAL*> = std::nullopt);
264 extern int spFileVector(char*, char*, spREAL*, std::optional<spREAL*> = std::nullopt);
265 extern void spMultiply(char*, spREAL*, spREAL*, std::optional<spREAL*> = std::nullopt, std::optional<spREAL*> = std::nullopt);
266 extern void spMultTransposed(char* eMatrix, spREAL* RHS, spREAL* Solution, std::optional<spREAL*> iRHS = std::nullopt, std::optional<spREAL*> iSolution = std::nullopt);
267 extern void spSolve(char* eMatrix, spREAL* RHS, spREAL* Solution, std::optional<spREAL*> iRHS = std::nullopt, std::optional<spREAL*> iSolution = std::nullopt);
268 extern void spSolveTransposed(char*, spREAL*, spREAL*, std::optional<spREAL*> = std::nullopt, std::optional<spREAL*> = std::nullopt);
269 
270 #endif /* spOKAY */
#define RHS(i)
Definition: multisplit.cpp:57
spREAL spRoundoff(char *, spREAL)
Definition: sputils.cpp:1172
int spGetOnes(char *, int, int, int, struct spTemplate *)
Definition: spbuild.cpp:429
void spScale(char *, spREAL[], spREAL[])
void spSetReal(char *)
Definition: spalloc.cpp:658
void spPartition(char *, int)
Definition: spfactor.cpp:434
spREAL spPseudoCondition(char *)
Definition: sputils.cpp:718
void spDestroy(char *)
Definition: spalloc.cpp:533
void spDeleteRowAndCol(char *, int, int)
int spOrderAndFactor(char *, spREAL[], spREAL, spREAL, int)
void spSolveTransposed(char *, spREAL *, spREAL *, std::optional< spREAL * >=std::nullopt, std::optional< spREAL * >=std::nullopt)
Definition: spsolve.cpp:225
int spGetAdmittance(char *, int, int, struct spTemplate *)
Definition: spbuild.cpp:294
char * spGetInitInfo(spREAL *)
void spClear(char *)
Definition: spbuild.cpp:82
spREAL spNorm(char *)
Definition: sputils.cpp:1001
spREAL spLargestElement(char *)
Definition: sputils.cpp:1097
int spGetSize(char *, int)
void spSolve(char *eMatrix, spREAL *RHS, spREAL *Solution, std::optional< spREAL * > iRHS=std::nullopt, std::optional< spREAL * > iSolution=std::nullopt)
Definition: spsolve.cpp:104
void spInstallInitInfo(spREAL *, char *)
void spStripFills(char *)
Definition: sputils.cpp:629
int spFileStats(char *, char *, char *)
Definition: spoutput.cpp:550
void spPrint(char *, int, int, int)
Definition: spoutput.cpp:126
void spDeterminant(char *, int *, spREAL *, std::optional< spREAL * >=std::nullopt)
Definition: sputils.cpp:549
int spFileMatrix(char *, char *, char *, int, int, int)
Definition: spoutput.cpp:366
int spGetQuad(char *, int, int, int, int, struct spTemplate *)
Definition: spbuild.cpp:368
char * spCreate(int, int, int *)
void spWhereSingular(char *, int *, int *)
Definition: spalloc.cpp:607
int spFillinCount(char *)
Definition: spalloc.cpp:678
void spMNA_Preorder(char *)
Definition: sputils.cpp:162
int spError(char *)
Definition: spalloc.cpp:580
#define spREAL
Definition: spmatrix.h:106
int spFileVector(char *, char *, spREAL *, std::optional< spREAL * >=std::nullopt)
spREAL * spGetElement(char *, int, int)
Definition: spbuild.cpp:151
int spInitialize(char *, int(*)())
void spMultiply(char *, spREAL *, spREAL *, std::optional< spREAL * >=std::nullopt, std::optional< spREAL * >=std::nullopt)
Definition: sputils.cpp:401
int spFactor(char *)
Definition: spfactor.cpp:307
int spElementCount(char *)
Definition: spalloc.cpp:686
spREAL spCondition(char *, spREAL, int *)
Definition: sputils.cpp:799
void spMultTransposed(char *eMatrix, spREAL *RHS, spREAL *Solution, std::optional< spREAL * > iRHS=std::nullopt, std::optional< spREAL * > iSolution=std::nullopt)
Definition: sputils.cpp:468
spREAL * Element4Negated
Definition: spmatrix.h:220
spREAL * Element1
Definition: spmatrix.h:217
spREAL * Element3Negated
Definition: spmatrix.h:219
spREAL * Element2
Definition: spmatrix.h:218