NEURON
spdefs.h
Go to the documentation of this file.
1 /*
2  * DATA STRUCTURE AND MACRO DEFINITIONS for Sparse.
3  *
4  * Author: Advising professor:
5  * Kenneth S. Kundert Alberto Sangiovanni-Vincentelli
6  * UC Berkeley
7  *
8  * This file contains common type definitions and macros for the sparse
9  * matrix routines. These definitions are of no interest to the user.
10  */
11 
12 /*
13  * Revision and copyright information.
14  *
15  * Copyright (c) 1985,86,87,88
16  * by Kenneth S. Kundert and the University of California.
17  *
18  * Permission to use, copy, modify, and distribute this software and
19  * its documentation for any purpose and without fee is hereby granted,
20  * provided that the copyright notices appear in all copies and
21  * supporting documentation and that the authors and the University of
22  * California are properly credited. The authors and the University of
23  * California make no representations as to the suitability of this
24  * software for any purpose. It is provided `as is', without express
25  * or implied warranty.
26  *
27  * $Date: 2004-04-24 23:28:33 +0200 (Sat, 24 Apr 2004) $
28  * $Revision: 616 $
29  */
30 
31 /*
32  * IMPORTS
33  */
34 
35 #include <stdio.h>
36 
37 /*
38  * If running lint, change some of the compiler options to get a more
39  * complete inspection.
40  */
41 
42 #ifdef lint
43 #undef REAL
44 #undef EXPANDABLE
45 #undef TRANSLATE
46 #undef INITIALIZE
47 #undef DELETE
48 #undef STRIP
49 #undef MODIFIED_NODAL
50 #undef QUAD_ELEMENT
51 #undef TRANSPOSE
52 #undef SCALING
53 #undef DOCUMENTATION
54 #undef MULTIPLICATION
55 #undef DETERMINANT
56 #undef CONDITION
57 #undef PSEUDOCONDITION
58 #undef FORTRAN
59 #undef DEBUG
60 
61 #define REAL YES
62 #define EXPANDABLE YES
63 #define TRANSLATE YES
64 #define INITIALIZE YES
65 #define DELETE YES
66 #define STRIP YES
67 #define MODIFIED_NODAL YES
68 #define QUAD_ELEMENT YES
69 #define TRANSPOSE YES
70 #define SCALING YES
71 #define DOCUMENTATION YES
72 #define MULTIPLICATION YES
73 #define DETERMINANT YES
74 #define CONDITION YES
75 #define PSEUDOCONDITION YES
76 #define FORTRAN YES
77 #define DEBUG YES
78 
79 #define LINT YES
80 #else /* not lint */
81 #define LINT NO
82 #endif /* not lint */
83 
84 /*
85  * MACRO DEFINITIONS
86  *
87  * Macros are distinguished by using solely capital letters in their
88  * identifiers. This contrasts with C defined identifiers which are strictly
89  * lower case, and program variable and procedure names which use both upper
90  * and lower case.
91  */
92 
93 /* Begin macros. */
94 
95 /* Boolean data type */
96 #define BOOLEAN int
97 #define NO 0
98 #define YES 1
99 #define NOT !
100 #define AND &&
101 #define OR ||
102 
103 /* NULL pointer */
104 #ifndef NULL
105 #define NULL 0
106 #endif
107 
108 #define SPARSE_ID 0x772773 /* Arbitrary (is Sparse on phone). */
109 #define IS_SPARSE(matrix) ((matrix) != NULL && (matrix)->ID == SPARSE_ID)
110 #define IS_VALID(matrix) ((matrix) != NULL && (matrix)->ID == SPARSE_ID && (matrix)->Error >= spOKAY && (matrix)->Error < spFATAL)
111 #define IS_FACTORED(matrix) ((matrix)->Factored && !(matrix)->NeedsOrdering)
112 
113 /* Macro commands */
114 /* Macro functions that return the maximum or minimum independent of type. */
115 #define MAX(a, b) ((a) > (b) ? (a) : (b))
116 #define MIN(a, b) ((a) < (b) ? (a) : (b))
117 
118 /* Macro function that returns the absolute value of a floating point number. */
119 #define ABS(a) ((a) < 0.0 ? -(a) : (a))
120 
121 /* Macro function that returns the square of a number. */
122 #define SQR(a) ((a) * (a))
123 
124 /* Macro procedure that swaps two entities. */
125 #define SWAP(type, a, b) \
126  { \
127  type swapx; \
128  swapx = a; \
129  a = b; \
130  b = swapx; \
131  }
132 
133 /* Macro function that returns the approx absolute value of a complex number. */
134 #define ELEMENT_MAG(ptr) ((ptr)->Real < 0.0 ? -(ptr)->Real : (ptr)->Real)
135 
136 /*
137  * ASSERT and ABORT
138  *
139  * Macro used to assert that if the code is working correctly, then
140  * a condition must be true. If not, then execution is terminated
141  * and an error message is issued stating that there is an internal
142  * error and giving the file and line number. These assertions are
143  * not evaluated unless the DEBUG flag is true.
144  */
145 
146 #if DEBUG
147 #define ASSERT(condition) \
148  if (NOT(condition)) \
149  ABORT()
150 #else
151 #define ASSERT(condition)
152 #endif
153 
154 #if DEBUG
155 #define ABORT() \
156  { \
157  (void)fflush(stdout); \
158  (void)fprintf(stderr, "sparse: panic in file `%s' at line %d.\n", \
159  __FILE__, __LINE__); \
160  (void)fflush(stderr); \
161  abort(); \
162  }
163 #else
164 #define ABORT()
165 #endif
166 
167 /*
168  * MEMORY ALLOCATION
169  */
170 #include "spmatrix.h"
171 #include <stdlib.h>
172 
173 #define ALLOC(type, number) ((type*)malloc((unsigned)(sizeof(type) * (number))))
174 #define REALLOC(ptr, type, number) \
175  ptr = (type*)realloc((char*)ptr, (unsigned)(sizeof(type) * (number)))
176 /* prevent setting an already freed value to NULL */
177 #define FREE(ptr) \
178  { \
179  if ((ptr) != NULL) { \
180  char* p = (char*)(ptr); \
181  (ptr) = NULL; \
182  free(p); \
183  } \
184  }
185 
186 /* Calloc that properly handles allocating a cleared vector. */
187 #define CALLOC(ptr, type, number) \
188  { \
189  int i; \
190  ptr = ALLOC(type, number); \
191  if (ptr != (type*)NULL) \
192  for (i = (number)-1; i >= 0; i--) \
193  ptr[i] = (type)0; \
194  }
195 
196 /*
197  * REAL NUMBER
198  */
199 
200 /* Begin `RealNumber'. */
201 
203 
204 /*
205  * MATRIX ELEMENT DATA STRUCTURE
206  *
207  * Every nonzero element in the matrix is stored in a dynamically allocated
208  * MatrixElement structure. These structures are linked together in an
209  * orthogonal linked list. Two different MatrixElement structures exist.
210  * One is used when only real matrices are expected, it is missing an entry
211  * for imaginary data. The other is used if complex matrices are expected.
212  * It contains an entry for imaginary data.
213  *
214  * >>> Structure fields:
215  * Real (RealNumber)
216  * The real portion of the value of the element. Real must be the first
217  * field in this structure.
218  * Row (int)
219  * The row number of the element.
220  * Col (int)
221  * The column number of the element.
222  * NextInRow (struct MatrixElement *)
223  * NextInRow contains a pointer to the next element in the row to the
224  * right of this element. If this element is the last nonzero in the
225  * row then NextInRow contains NULL.
226  * NextInCol (struct MatrixElement *)
227  * NextInCol contains a pointer to the next element in the column below
228  * this element. If this element is the last nonzero in the column then
229  * NextInCol contains NULL.
230  * pInitInfo (char *)
231  * Pointer to user data used for initialization of the matrix element.
232  * Initialized to NULL.
233  *
234  * >>> Type definitions:
235  * ElementPtr
236  * A pointer to a MatrixElement.
237  * ArrayOfElementPtrs
238  * An array of ElementPtrs. Used for FirstInRow, FirstInCol and
239  * Diag pointer arrays.
240  */
241 
242 /* Begin `MatrixElement'. */
243 
246  int Row;
247  int Col;
250 #if INITIALIZE
251  char* pInitInfo;
252 #endif
253 };
254 
255 typedef struct MatrixElement* ElementPtr;
257 
258 /*
259  * ALLOCATION DATA STRUCTURE
260  *
261  * The sparse matrix routines keep track of all memory that is allocated by
262  * the operating system so the memory can later be freed. This is done by
263  * saving the pointers to all the chunks of memory that are allocated to a
264  * particular matrix in an allocation list. That list is organized as a
265  * linked list so that it can grow without a priori bounds.
266  *
267  * >>> Structure fields:
268  * AllocatedPtr (char *)
269  * Pointer to chunk of memory that has been allocated for the matrix.
270  * NextRecord (struct AllocationRecord *)
271  * Pointer to the next allocation record.
272  */
273 
274 /* Begin `AllocationRecord'. */
278 };
279 
281 
282 /*
283  * FILL-IN LIST DATA STRUCTURE
284  *
285  * The sparse matrix routines keep track of all fill-ins separately from
286  * user specified elements so they may be removed by spStripFills(). Fill-ins
287  * are allocated in bunched in what is called a fill-in lists. The data
288  * structure defined below is used to organize these fill-in lists into a
289  * linked-list.
290  *
291  * >>> Structure fields:
292  * pFillinList (ElementPtr)
293  * Pointer to a fill-in list, or a bunch of fill-ins arranged contiguously
294  * in memory.
295  * NumberOfFillinsInList (int)
296  * Seems pretty self explanatory to me.
297  * Next (struct FillinListNodeStruct *)
298  * Pointer to the next fill-in list structures.
299  */
300 
301 /* Begin `FillinListNodeStruct'. */
306 };
307 
308 /*
309  * MATRIX FRAME DATA STRUCTURE
310  *
311  * This structure contains all the pointers that support the orthogonal
312  * linked list that contains the matrix elements. Also included in this
313  * structure are other numbers and pointers that are used globally by the
314  * sparse matrix routines and are associated with one particular matrix.
315  *
316  * >>> Type definitions:
317  * MatrixPtr
318  * A pointer to MatrixFrame. Essentially, a pointer to the matrix.
319  *
320  * >>> Structure fields:
321  * AbsThreshold (RealNumber)
322  * The absolute magnitude an element must have to be considered as a
323  * pivot candidate, except as a last resort.
324  * AllocatedExtSize (int)
325  * The allocated size of the arrays used to translate external row and
326  * column numbers to their internal values.
327  * AllocatedSize (int)
328  * The currently allocated size of the matrix; the size the matrix can
329  * grow to when EXPANDABLE is set true and AllocatedSize is the largest
330  * the matrix can get without requiring that the matrix frame be
331  * reallocated.
332  * Complex (BOOLEAN)
333  * The flag which indicates whether the matrix is complex (true) or
334  * real.
335  * CurrentSize (int)
336  * This number is used during the building of the matrix when the
337  * TRANSLATE option is set true. It indicates the number of internal
338  * rows and columns that have elements in them.
339  * Diag (ArrayOfElementPtrs)
340  * Array of pointers that points to the diagonal elements.
341  * DoCmplxDirect (BOOLEAN *)
342  * Array of flags, one for each column in matrix. If a flag is true
343  * then corresponding column in a complex matrix should be eliminated
344  * in spFactor() using direct addressing (rather than indirect
345  * addressing).
346  * DoRealDirect (BOOLEAN *)
347  * Array of flags, one for each column in matrix. If a flag is true
348  * then corresponding column in a real matrix should be eliminated
349  * in spFactor() using direct addressing (rather than indirect
350  * addressing).
351  * Elements (int)
352  * The number of original elements (total elements minus fill ins)
353  * present in matrix.
354  * Error (int)
355  * The error status of the sparse matrix package.
356  * ExtSize (int)
357  * The value of the largest external row or column number encountered.
358  * ExtToIntColMap (int [])
359  * An array that is used to convert external columns number to internal
360  * external column numbers. Present only if TRANSLATE option is set true.
361  * ExtToIntRowMap (int [])
362  * An array that is used to convert external row numbers to internal
363  * external row numbers. Present only if TRANSLATE option is set true.
364  * Factored (BOOLEAN)
365  * Indicates if matrix has been factored. This flag is set true in
366  * spFactor() and spOrderAndFactor() and set false in spCreate()
367  * and spClear().
368  * Fillins (int)
369  * The number of fill-ins created during the factorization the matrix.
370  * FirstInCol (ArrayOfElementPtrs)
371  * Array of pointers that point to the first nonzero element of the
372  * column corresponding to the index.
373  * FirstInRow (ArrayOfElementPtrs)
374  * Array of pointers that point to the first nonzero element of the row
375  * corresponding to the index.
376  * ID (unsigned long int)
377  * A constant that provides the sparse data structure with a signature.
378  * When DEBUG is true, all externally available sparse routines check
379  * this signature to assure they are operating on a valid matrix.
380  * Intermediate (RealVector)
381  * Temporary storage used in the spSolve routines. Intermediate is an
382  * array used during forward and backward substitution. It is
383  * commonly called y when the forward and backward substitution process is
384  * denoted Ax = b => Ly = b and Ux = y.
385  * InternalVectorsAllocated (BOOLEAN)
386  * A flag that indicates whether the markowitz vectors and the
387  * Intermediate vector have been created.
388  * These vectors are created in CreateInternalVectors().
389  * IntToExtColMap (int [])
390  * An array that is used to convert internal column numbers to external
391  * external column numbers.
392  * IntToExtRowMap (int [])
393  * An array that is used to convert internal row numbers to external
394  * external row numbers.
395  * MarkowitzCol (int [])
396  * An array that contains the count of the non-zero elements excluding
397  * the pivots for each column. Used to generate and update MarkowitzProd.
398  * MarkowitzProd (long [])
399  * The array of the products of the Markowitz row and column counts. The
400  * element with the smallest product is the best pivot to use to maintain
401  * sparsity.
402  * MarkowitzRow (int [])
403  * An array that contains the count of the non-zero elements excluding
404  * the pivots for each row. Used to generate and update MarkowitzProd.
405  * MaxRowCountInLowerTri (int)
406  * The maximum number of off-diagonal element in the rows of L, the
407  * lower triangular matrix. This quantity is used when computing an
408  * estimate of the roundoff error in the matrix.
409  * NeedsOrdering (BOOLEAN)
410  * This is a flag that signifies that the matrix needs to be ordered
411  * or reordered. NeedsOrdering is set true in spCreate() and
412  * spGetElement() or spGetAdmittance() if new elements are added to the
413  * matrix after it has been previously factored. It is set false in
414  * spOrderAndFactor().
415  * NumberOfInterchangesIsOdd (BOOLEAN)
416  * Flag that indicates the sum of row and column interchange counts
417  * is an odd number. Used when determining the sign of the determinant.
418  * Partitioned (BOOLEAN)
419  * This flag indicates that the columns of the matrix have been
420  * partitioned into two groups. Those that will be addressed directly
421  * and those that will be addressed indirectly in spFactor().
422  * PivotsOriginalCol (int)
423  * Column pivot was chosen from.
424  * PivotsOriginalRow (int)
425  * Row pivot was chosen from.
426  * PivotSelectionMethod (char)
427  * Character that indicates which pivot search method was successful.
428  * PreviousMatrixWasComplex (BOOLEAN)
429  * This flag in needed to determine how to clear the matrix. When
430  * dealing with real matrices, it is important that the imaginary terms
431  * in the matrix elements be zero. Thus, if the previous matrix was
432  * complex, then the current matrix will be cleared as if it were complex
433  * even if it is real.
434  * RelThreshold (RealNumber)
435  * The magnitude an element must have relative to others in its row
436  * to be considered as a pivot candidate, except as a last resort.
437  * Reordered (BOOLEAN)
438  * This flag signifies that the matrix has been reordered. It
439  * is cleared in spCreate(), set in spMNA_Preorder() and
440  * spOrderAndFactor() and is used in spPrint().
441  * RowsLinked (BOOLEAN)
442  * A flag that indicates whether the row pointers exist. The AddByIndex
443  * routines do not generate the row pointers, which are needed by some
444  * of the other routines, such as spOrderAndFactor() and spScale().
445  * The row pointers are generated in the function spcLinkRows().
446  * SingularCol (int)
447  * Normally zero, but if matrix is found to be singular, SingularCol is
448  * assigned the external column number of pivot that was zero.
449  * SingularRow (int)
450  * Normally zero, but if matrix is found to be singular, SingularRow is
451  * assigned the external row number of pivot that was zero.
452  * Singletons (int)
453  * The number of singletons available for pivoting. Note that if row I
454  * and column I both contain singletons, only one of them is counted.
455  * Size (int)
456  * Number of rows and columns in the matrix. Does not change as matrix
457  * is factored.
458  * TrashCan (MatrixElement)
459  * This is a dummy MatrixElement that is used to by the user to stuff
460  * data related to the zero row or column. In other words, when the user
461  * adds an element in row zero or column zero, then the matrix returns
462  * a pointer to TrashCan. In this way the user can have a uniform way
463  * data into the matrix independent of whether a component is connected
464  * to ground.
465  *
466  * >>> The remaining fields are related to memory allocation.
467  * TopOfAllocationList (AllocationListPtr)
468  * Pointer which points to the top entry in a list. The list contains
469  * all the pointers to the segments of memory that have been allocated
470  * to this matrix. This is used when the memory is to be freed on
471  * deallocation of the matrix.
472  * RecordsRemaining (int)
473  * Number of slots left in the list of allocations.
474  * NextAvailElement (ElementPtr)
475  * Pointer to the next available element which has been allocated but as
476  * yet is unused. Matrix elements are allocated in groups of
477  * ELEMENTS_PER_ALLOCATION in order to speed element allocation and
478  * freeing.
479  * ElementsRemaining (int)
480  * Number of unused elements left in last block of elements allocated.
481  * NextAvailFillin (ElementPtr)
482  * Pointer to the next available fill-in which has been allocated but
483  * as yet is unused. Fill-ins are allocated in a group in order to keep
484  * them physically close in memory to the rest of the matrix.
485  * FillinsRemaining (int)
486  * Number of unused fill-ins left in the last block of fill-ins
487  * allocated.
488  * FirstFillinListNode (FillinListNodeStruct *)
489  * A pointer to the head of the linked-list that keeps track of the
490  * lists of fill-ins.
491  * LastFillinListNode (FillinListNodeStruct *)
492  * A pointer to the tail of the linked-list that keeps track of the
493  * lists of fill-ins.
494  */
495 
496 /* Begin `MatrixFrame'. */
497 struct MatrixFrame {
506  int Elements;
507  int Error;
508  int ExtSize;
512  int Fillins;
515  unsigned long ID;
537  int Size;
538  struct MatrixElement TrashCan;
539 
548 };
549 typedef struct MatrixFrame* MatrixPtr;
#define BOOLEAN
Definition: spdefs.h:96
struct MatrixFrame * MatrixPtr
Definition: spdefs.h:549
spREAL RealNumber
Definition: spdefs.h:202
spREAL * RealVector
Definition: spdefs.h:202
struct MatrixElement * ElementPtr
Definition: spdefs.h:255
ElementPtr * ArrayOfElementPtrs
Definition: spdefs.h:256
struct AllocationRecord * AllocationListPtr
Definition: spdefs.h:280
#define spREAL
Definition: spmatrix.h:106
char * AllocatedPtr
Definition: spdefs.h:276
struct AllocationRecord * NextRecord
Definition: spdefs.h:277
int NumberOfFillinsInList
Definition: spdefs.h:304
ElementPtr pFillinList
Definition: spdefs.h:303
struct FillinListNodeStruct * Next
Definition: spdefs.h:305
RealNumber Real
Definition: spdefs.h:245
struct MatrixElement * NextInCol
Definition: spdefs.h:249
struct MatrixElement * NextInRow
Definition: spdefs.h:248
int SingularCol
Definition: spdefs.h:534
BOOLEAN Partitioned
Definition: spdefs.h:526
int ExtSize
Definition: spdefs.h:508
int SingularRow
Definition: spdefs.h:535
int MaxRowCountInLowerTri
Definition: spdefs.h:523
int * IntToExtRowMap
Definition: spdefs.h:519
BOOLEAN * DoRealDirect
Definition: spdefs.h:505
BOOLEAN Complex
Definition: spdefs.h:501
ElementPtr NextAvailFillin
Definition: spdefs.h:544
BOOLEAN InternalVectorsAllocated
Definition: spdefs.h:517
int Fillins
Definition: spdefs.h:512
BOOLEAN PreviousMatrixWasComplex
Definition: spdefs.h:530
struct FillinListNodeStruct * FirstFillinListNode
Definition: spdefs.h:546
int Singletons
Definition: spdefs.h:536
BOOLEAN NumberOfInterchangesIsOdd
Definition: spdefs.h:525
struct FillinListNodeStruct * LastFillinListNode
Definition: spdefs.h:547
int AllocatedSize
Definition: spdefs.h:499
BOOLEAN * DoCmplxDirect
Definition: spdefs.h:504
ArrayOfElementPtrs FirstInCol
Definition: spdefs.h:513
int PivotsOriginalCol
Definition: spdefs.h:527
RealNumber AbsThreshold
Definition: spdefs.h:498
int ElementsRemaining
Definition: spdefs.h:543
char PivotSelectionMethod
Definition: spdefs.h:529
int Elements
Definition: spdefs.h:506
int CurrentSize
Definition: spdefs.h:502
int AllocatedExtSize
Definition: spdefs.h:500
int * ExtToIntColMap
Definition: spdefs.h:509
BOOLEAN RowsLinked
Definition: spdefs.h:533
int * IntToExtColMap
Definition: spdefs.h:518
ArrayOfElementPtrs FirstInRow
Definition: spdefs.h:514
BOOLEAN Factored
Definition: spdefs.h:511
long * MarkowitzProd
Definition: spdefs.h:522
int Error
Definition: spdefs.h:507
RealNumber RelThreshold
Definition: spdefs.h:531
unsigned long ID
Definition: spdefs.h:515
int FillinsRemaining
Definition: spdefs.h:545
AllocationListPtr TopOfAllocationList
Definition: spdefs.h:540
BOOLEAN NeedsOrdering
Definition: spdefs.h:524
ElementPtr NextAvailElement
Definition: spdefs.h:542
int Size
Definition: spdefs.h:537
int PivotsOriginalRow
Definition: spdefs.h:528
int RecordsRemaining
Definition: spdefs.h:541
int * ExtToIntRowMap
Definition: spdefs.h:510
int * MarkowitzRow
Definition: spdefs.h:520
ArrayOfElementPtrs Diag
Definition: spdefs.h:503
RealVector Intermediate
Definition: spdefs.h:516
int * MarkowitzCol
Definition: spdefs.h:521
BOOLEAN Reordered
Definition: spdefs.h:532
struct MatrixElement TrashCan
Definition: spdefs.h:538