/[ascend]/trunk/base/generic/compiler/units.h
ViewVC logotype

Contents of /trunk/base/generic/compiler/units.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 966 - (show annotations) (download) (as text)
Thu Dec 14 14:04:54 2006 UTC (13 years, 10 months ago) by johnpye
File MIME type: text/x-chdr
File size: 8371 byte(s)
Cleaned up code comments in units.h
Fixed a bug with starting timesteps in LSODE (i think)
Added function to output a SampleList to the console (for debugging)
Removed debug output from base/generic/test/SConscript.
Removed 'custom' headers for UnitsM in ascpy.i
Made a sane default ctor for UnitsM
Added some more test cases to the Python unittest suite (still a problem running multiple solver-based tests in a single run)
1 /* ASCEND modelling environment
2 Copyright (C) 2006 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 @file
20 Ascend Units Type definitions.
21
22 Requires:
23 #include "fractions.h"
24 #include "compiler.h"
25 #include "dimen.h"
26 *//*
27 by Tom Epperly 8/18/89
28 Last in CVS: $Revision: 1.10 $ $Date: 1998/02/05 16:38:40 $ $Author: ballan $
29 */
30
31 #ifndef ASC_UNITS_H
32 #define ASC_UNITS_H
33
34 #include <stdio.h>
35 #include <utilities/ascConfig.h>
36
37 #ifdef _HPUX_SOURCE
38 #define ACAST char *
39 #define BCAST char *
40 #else
41 #define ACAST const char *
42 #define BCAST char *
43 #endif
44 /**< to shut up the hp lexer */
45
46 /**
47 Value in system units = value in given units * conversion_factor.
48 */
49 struct Units {
50 double conversion_factor; /**< to convert from units to system units */
51 symchar *description; /**< description of units */
52 CONST dim_type *dim; /**< dimenions of units */
53 struct Units *next; /**< not for human consumption */
54 };
55
56 /**
57 Temporary structure for parsing unit definitions in ascParse.y.
58 */
59 struct UnitDefinition {
60 symchar *new_name;
61 CONST char *unitsexpr;
62 CONST char *filename;
63 int linenum;
64 };
65
66 #define UNITS_HASH_SIZE (1023)
67 /**<
68 size of the hash table for unit structs
69 */
70
71 /*
72 * Name of the basic SI units for the 10 dimensions.
73 * If you have better names for them, fix that here.
74 */
75 #define UNIT_BASE_MASS "kilogram"
76 /**< Name of basic SI unit for mass. */
77 #define UNIT_BASE_QUANTITY "mole"
78 /**< Name of basic SI unit for quantity. */
79 #define UNIT_BASE_LENGTH "meter"
80 /**< Name of basic SI unit for length. */
81 #define UNIT_BASE_TIME "second"
82 /**< Name of basic SI unit for time. */
83 #define UNIT_BASE_TEMPERATURE "Kelvin"
84 /**< Name of basic SI unit for temperature. */
85 #define UNIT_BASE_CURRENCY "currency"
86 /**< Name of basic SI unit for currency. */
87 #define UNIT_BASE_ELECTRIC_CURRENT "ampere"
88 /**< Name of basic SI unit for current. */
89 #define UNIT_BASE_LUMINOUS_INTENSITY "candela"
90 /**< Name of basic SI unit for luminosity. */
91 #define UNIT_BASE_PLANE_ANGLE "radian"
92 /**< Name of basic SI unit for plane angle. */
93 #define UNIT_BASE_SOLID_ANGLE "steradian"
94 /**< Name of basic SI unit for solid angle. */
95
96 ASC_DLLSPEC(struct Units *) g_units_hash_table[];
97 /**<
98 * The hash table for unit structs.
99 */
100
101 extern void InitUnitsTable(void);
102 /**<
103 This routine initializes some internal variables, so that all the
104 other units functions may be called. It must be called once and
105 only once when the program is starting up.
106 Must be called after dimensions table initiatialization.
107 */
108
109 extern void DestroyUnitsTable(void);
110 /**<
111 This routine can be called to deallocate all of the units in the table.
112 */
113
114 extern struct UnitDefinition *CreateUnitDef(symchar *new_name,
115 CONST char *unitsexpr,
116 CONST char *filename,
117 int linenum);
118 /**<
119 * Create a new unit definition.
120 * @param new_name Should be from the symbol table.
121 * @param unitsexpr A string that we will copy, so it does not
122 * need to be persistent.
123 * @param filename Should be persistent.
124 * @param linenum Line number.
125 */
126
127 extern void DestroyUnitDef(struct UnitDefinition *udp);
128 /**<
129 * Destroys udp and its unitsexpr.
130 */
131
132 extern void ProcessUnitDef(struct UnitDefinition *udp);
133 /**<
134 * Attempts to add the info in udp to the units table.
135 * messages to ascerr if not possible.
136 */
137
138 ASC_DLLSPEC(CONST struct Units*) LookupUnits(CONST char *c);
139 /**<
140 * Check the units library for units with a description string which
141 * matches c. If it is found, this function will return a non-NULL pointer;
142 * otherwise, it returns NULL to indicate that units c are undefined.
143 * c should not contain any blanks.
144 */
145
146 extern CONST struct Units *DefineUnits(symchar *c, double conv, CONST dim_type *dim);
147 /**<
148 * Define the units c with conversion factor conv and dimensions *dim.
149 * This assumes that *dim was the value returned by FindOrAddDim. This
150 * will check to prevent duplicate entries. The resulting unit structure is
151 * returned. If you enter a duplicate entry and the dimensions or conversion
152 * factor don't match, this function will return NULL. In addition to
153 * the user defined units there is a wild units type which conversion equal
154 * to one and wild dimensions. It is given the name "?".<br><br>
155 *
156 * c should not contain any spaces! It may add c to the symbol table if
157 * it is not already stored there.
158 *
159 * @bug Memory leak if hitting an empty hash bucket. buckets never deallocated
160 * except at shutdown anyway, so not a big deal. 7bytes/hit. Don't off hand
161 * know where to fix it. BAA 6-94
162 */
163
164 ASC_DLLSPEC(CONST struct Units*) FindOrDefineUnits(CONST char *c,
165 unsigned long * CONST pos,
166 int * CONST error_code);
167 /**<
168 * This function will attempt to parse the string c into a units
169 * description. If the unit type has been defined before, the corresponding
170 * units pointer will be returned. If this type hasn't been defined before,
171 * it will be defined and that pointer will be returned. If it is
172 * unable to parse this string it will return NULL. CheckUnitStr, below,
173 * can be used to diagnose why the unit didn't parse.
174 * This will not modify c in any way. It may add c to the symbol table.
175 * <pre>
176 * RETURN VALUE OF error_code
177 * 0 string is okay, value of pos not specified
178 * 1 undefined unit used in string, pos indicates the first
179 * letter of the first occurence of the undefined unit
180 * 2 unbalanced parenthesis, pos indicates the opening parenthesis
181 * that wasn't closed
182 * 3 illegal character, pos indicates the position of the offending
183 * character
184 * 4 illegal real value, pos indicates the first character of the
185 * real value
186 * 5 oversized identifier or real, pos indicates the start of the
187 * offending token
188 * 6 operator left out real followed by identifier or vice
189 * versa, pos indicates where an operator should have been
190 * inserted
191 * 7 term missing after *,/, or (.
192 * 8 missing term before *,/. pos is left at the operator
193 * 9 too many closing parens. pos is left at the extra paren.
194 * 10 bad fraction exponent. pos is at the left of the fraction field
195 * 11 incompatible unit redefinition. (internal use only; not seen).
196 * </pre>
197 */
198
199 ASC_DLLSPEC(char**) UnitsExplainError(CONST char *unitsexpr, int code, int pos);
200 /**<
201 * Returns an array of strings which may be helpful in
202 * explaining the error.
203 * - errv[0] is a message.
204 * - errv[1] is the unitsexpr.
205 * - errv[2] is a pointer to the error -------^ line.
206 * aligned with unitsexpr given.
207 * The user should never change or free errv or its content,
208 * nor should the user keep the pointers.
209 */
210
211 #define UnitsDescription(u) ((u)->description)
212 /**<
213 * Returns the string description attribute of a units structure.
214 */
215
216 #define UnitsConvFactor(u) ((u)->conversion_factor)
217 /**<
218 * Returns the conversion factor for a given units structure.
219 */
220
221 #define UnitsDimensions(u) ((u)->dim)
222 /**<
223 * Returns the dimensions of the units structure.
224 */
225
226 ASC_DLLSPEC(char *) UnitsStringSI(struct Units *up);
227 /**<
228 * Returns the SI form of the units for the dimensionality of up.
229 * Wild = *, Dimensionless = "", NULL up --> NULL return.
230 * Caller is responsible for freeing the string returned.
231 */
232
233 ASC_DLLSPEC(void ) DumpUnits(FILE *f);
234 /**< Dump all defined units to f. */
235
236 #endif /* ASC_UNITS_H */
237

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22