Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/post/matc/src/elmer/matc.h
3206 views
1
/***********************************************************************
2
|
3
| MATC.H - Last Edited 7. 8. 1988
4
|
5
************************************************************************/
6
7
8
/*
9
* $Id: matc.h,v 1.2 2007/06/08 08:12:19 jpr Exp $
10
*
11
* $Log: matc.h,v $
12
* Revision 1.2 2007/06/08 08:12:19 jpr
13
* *** empty log message ***
14
*
15
* Revision 1.1 2005/05/27 12:26:22 vierinen
16
* changed header install location
17
*
18
* Revision 1.1.1.1 2005/04/14 13:29:14 vierinen
19
* initial matc automake package
20
*
21
* Revision 1.3 2001/06/08 09:20:29 jpr
22
* *** empty log message ***
23
*
24
* Revision 1.2 1998/08/01 12:34:49 jpr
25
*
26
* Added Id, started Log.
27
*
28
*
29
*/
30
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <ctype.h>
34
#include <math.h>
35
#include <setjmp.h>
36
#include <string.h>
37
#include <signal.h>
38
#include <stdlib.h>
39
#include <stdarg.h>
40
#include <time.h>
41
#include <unistd.h>
42
#include <sys/types.h>
43
44
45
#ifdef MODULE_MATC
46
#define EXT
47
#else
48
#define EXT extern
49
#endif
50
51
/*******************************************************************
52
LIST HANDLING DEFINITIONS
53
*******************************************************************/
54
55
typedef struct list {
56
struct list *next; /* pointer to next item in list */
57
char *name; /* name of list item */
58
} LIST;
59
60
/*
61
pointers to start of global lists
62
*/
63
#ifdef MODULE_MATC
64
#ifdef _OPENMP
65
/* Move initialization to matc.c::mtc_init() for thread safety */
66
EXT LIST * listheaders;
67
#pragma omp threadprivate(listheaders)
68
69
#else
70
EXT LIST listheaders[5] = {
71
{
72
NULL, "Allocations" /* memory allocations */
73
}, {
74
NULL, "Constants" /* global CONSTANTS */
75
}, {
76
NULL, "Currently defined VARIABLES" /* global VARIABLES */
77
}, {
78
NULL, "Builtin Functions" /* internal commands */
79
}, {
80
NULL, "User Functions" /* user defined functions */
81
}
82
};
83
#endif /* _OPENMP */
84
#else
85
#ifdef _OPENMP
86
/* Move initialization to matc.c::mtc_init() for thread safety */
87
EXT LIST * listheaders;
88
#pragma omp threadprivate(listheaders)
89
#else
90
EXT LIST listheaders[];
91
#endif /* _OPENMP */
92
#endif /* MODULE_MATC */
93
94
#define ALLOCATIONS 0
95
#define CONSTANTS 1
96
#define VARIABLES 2
97
#define COMMANDS 3
98
#define FUNCTIONS 4
99
100
#define MAX_HEADERS 5
101
102
#define ALLOC_HEAD listheaders[ALLOCATIONS].next
103
#define CONST_HEAD listheaders[CONSTANTS].next
104
#define VAR_HEAD listheaders[VARIABLES].next
105
#define COM_HEAD listheaders[COMMANDS].next
106
#define FUNC_HEAD listheaders[FUNCTIONS].next
107
108
#define NEXT(lst) (lst)->next
109
#define NAME(lst) (lst)->name
110
111
/*******************************************************************
112
MEMORY HANDLING
113
********************************************************************/
114
115
/*
116
memory allocation and deallocation routines
117
*/
118
#define ALLOCMEM(size) mem_alloc(size)
119
#define FREEMEM(ptr) mem_free(ptr)
120
121
/*
122
we use a lot of string copying.
123
*/
124
#define STRCOPY(str) strcpy((char *)ALLOCMEM(strlen(str)+1),(str))
125
126
typedef struct alloc_list {
127
struct alloc_list *next;
128
char *mem;
129
} ALLOC_LIST;
130
131
#define ALLOC_LST(mem) (ALLOC_LIST *)((char *)mem-sizeof(ALLOC_LIST))
132
#define ALLOC_PTR(lst) (char *)((char *)lst+sizeof(ALLOC_LIST))
133
134
/*******************************************************************
135
VARIABLES
136
*******************************************************************/
137
138
/*
139
* MATC matrix is internally represented by this structure.
140
*/
141
typedef struct MATRIX
142
{
143
int type, /* TYPE_DOUBLE or TYPE_STRING */
144
refcount, /* reference count */
145
nrow, ncol; /* number of rows and columns */
146
double *data; /* pointer to double array */
147
} MATRIX;
148
149
150
/*
151
* list of VARIABLES
152
*/
153
154
typedef struct variable
155
{
156
struct variable *next; /* pointer to next item in list */
157
char *name; /* name of the item */
158
int changed;
159
MATRIX *this;
160
} VARIABLE;
161
162
/*
163
shortcuts for accessing structure MATRIX
164
*/
165
#define MATR(ptr) (ptr)->this->data
166
#define TYPE(ptr) (ptr)->this->type
167
#define NROW(ptr) (ptr)->this->nrow
168
#define NCOL(ptr) (ptr)->this->ncol
169
#define REFCNT(ptr) (ptr)->this->refcount
170
#define M(ptr,i,j) (ptr)->this->data[(i) * NCOL(ptr) + (j)]
171
172
#define VARIABLESIZE sizeof(VARIABLE)
173
#define MATRIXSIZE sizeof(MATRIX)
174
#define MATSIZE(ptr) NROW(ptr)*NCOL(ptr)*sizeof(double)
175
176
#define TYPE_DOUBLE 0
177
#define TYPE_COMPLEX 1 /* this is not */
178
#define TYPE_STRING 2
179
180
/*******************************************************************
181
INTERNAL COMMANDS AND USER FUNCTIONS
182
*******************************************************************/
183
184
typedef struct command
185
{
186
struct command *next; /* pointer to next item in list */
187
char *name; /* name of the item */
188
int flags, /* CMDFLAG_PW & CMDFLAG_CE */
189
minp, maxp; /* min. and max. no. of parameters */
190
VARIABLE *(*sub)(); /* function to execute */
191
char *help; /* help string... */
192
} COMMAND;
193
194
#define COMSIZE sizeof(COMMAND)
195
196
#define CMDFLAG_PW 1 /* element by element operation */
197
#define CMDFLAG_CE 2 /* command can be executed when
198
preprocessing if constant
199
arguments. */
200
201
/*******************************************************************
202
USER DEFINED FUNCTIONS
203
*******************************************************************/
204
205
typedef struct function
206
{
207
struct function *next; /* pointer to next function in list */
208
char *name, /* name of the function */
209
**parnames, /* function parameter names (if any) */
210
**exports, /* functions exported variables */
211
**imports, /* functions imported variables */
212
*help; /* functions help text */
213
int parcount; /* defined number of parameters */
214
struct clause *body; /* function body */
215
} FUNCTION;
216
217
#define FUNCSIZE sizeof(FUNCTION)
218
219
/*******************************************************************
220
MISC DEFINITIONS FOR PARSER
221
*******************************************************************/
222
223
typedef enum symbols {
224
nullsym, leftpar, rightpar, indopen, indclose, power, times, ptimes, divide,
225
plus, minus, reduction, transpose, eq, neq, lt, gt, le, ge, and, or, not,
226
assignsym, apply, resize, vector, statemend, argsep, name, number, string,
227
funcsym, import, export, ifsym, thensym, elsesym, whilesym, forsym,
228
beginsym, endsym, breaksym, comment, systemcall
229
} SYMTYPE;
230
231
#ifdef MODULE_MATC
232
233
/*--------------------------------------------------------------------*/
234
235
SYMTYPE ssymbols[] = {
236
leftpar, rightpar, indopen, indclose, beginsym, endsym, power, times, ptimes,
237
divide, plus, minus, reduction, transpose, lt, gt, and, or, not,
238
assignsym, apply, resize, vector, statemend, argsep, comment, systemcall
239
};
240
241
char csymbols[] = {
242
'(', ')', '[', ']', '{', '}', '^', '*', '#', '/', '+', '-', '\?',
243
'\'', '<', '>', '&', '|', '~', '=', '@', '%', ':', ';', ',', '!', '$'
244
};
245
246
/*--------------------------------------------------------------------*/
247
248
/*--------------------------------------------------------------------*/
249
250
char *reswords[] = {
251
"function", "import", "export", "if", "then", "else", "while", "for",
252
"begin", "end", "break", NULL
253
};
254
255
SYMTYPE rsymbols[] = {
256
funcsym, import, export, ifsym, thensym, elsesym, whilesym, forsym,
257
beginsym, endsym, breaksym
258
};
259
260
/*--------------------------------------------------------------------*/
261
262
char *symchars = "`._";
263
264
#else
265
266
EXT SYMTYPE ssymbols[];
267
EXT char csymbols[];
268
EXT char *reswords[];
269
EXT SYMTYPE rsymbols[];
270
EXT char *symchars;
271
/* #pragma omp threadprivate(ssymbols, csymbols, reswords, rsymbols, symchars)
272
* TODO: this should be added when GCC 4.8 arrives if needed */
273
#endif
274
275
/*
276
dataentry for expression trees
277
*/
278
typedef struct treeentry
279
{
280
struct tree *args, /* parameters for functions */
281
*subs; /* indexes for VARIABLES, equation results */
282
int entrytype; /* type of entrydata */
283
284
union data_entry
285
{
286
char *s_data; /* function or VARIABLE names or string constants */
287
double d_data; /* numeric constant */
288
VARIABLE *c_data; /* real constant, with no name references */
289
MATRIX *(*v_data)(); /* function address (for builtin operations) */
290
} entrydata;
291
292
} TREEENTRY;
293
294
#define ETYPE_NAME 0
295
#define ETYPE_NUMBER 1
296
#define ETYPE_STRING 2
297
#define ETYPE_OPER 3
298
#define ETYPE_CONST 4
299
#define ETYPE_EQUAT 5
300
301
/*
302
* four leaf tree, isn't that odd
303
*/
304
typedef struct tree {
305
struct tree *next;
306
struct tree *link;
307
struct tree *left, *right;
308
TREEENTRY tentry;
309
} TREE;
310
311
/*
312
shortcuts for accessing above structures
313
*/
314
#define SDATA(ptr) (ptr)->tentry.entrydata.s_data
315
#define DDATA(ptr) (ptr)->tentry.entrydata.d_data
316
#define CDATA(ptr) (ptr)->tentry.entrydata.c_data
317
#define VDATA(ptr) (ptr)->tentry.entrydata.v_data
318
#define ETYPE(ptr) (ptr)->tentry.entrytype
319
#define SUBS(ptr) (ptr)->tentry.subs
320
#define ARGS(ptr) (ptr)->tentry.args
321
#define LEFT(ptr) (ptr)->left
322
#define RIGHT(ptr) (ptr)->right
323
324
/*
325
this is an operations list. data can be one of
326
the following:
327
328
ifsym, elsesym, whilesym, forsym, assignsym, funcsym
329
330
every input line is compiled to this type of list,
331
and it is used to hold function bodies.
332
*/
333
334
typedef struct clause
335
{
336
struct clause *link;
337
struct clause *jmp;
338
TREE *this;
339
SYMTYPE data;
340
} CLAUSE;
341
342
#define LINK(ptr) (ptr)->link
343
344
/*******************************************************************
345
THIS AND THAT
346
*******************************************************************/
347
348
#ifdef sign
349
# undef sign
350
#endif
351
#ifdef max
352
# undef max
353
#endif
354
#ifdef min
355
# undef min
356
#endif
357
#ifdef abs
358
# undef abs
359
#endif
360
361
#define sign(x) ((x) > 0 ? 1 : ((x) < 0 ? -1 : 0))
362
#define max(x,y) ((x) > (y) ? (x) : (y))
363
#define min(x,y) ((x) > (y) ? (y) : (x))
364
#define abs(x) ((x) > 0 ? (x) : -(x))
365
366
#define FOREVER for(;;)
367
#ifndef TRUE
368
#define TRUE 1
369
#endif
370
#ifndef FALSE
371
#define FALSE 0
372
#endif
373
374
/*
375
promptmode flags:
376
377
PMODE_MAIN ===> MATC>
378
PMODE_BLOCK ===> ....>
379
*/
380
#define PMODE_MAIN "MATC> "
381
#define PMODE_BLOCK "....> "
382
#define PMODE_CONT "####> "
383
384
EXT FILE *math_in, *math_out, *math_err;
385
#pragma omp threadprivate(math_in, math_out, math_err)
386
/*
387
see doread(), error() in matc.c
388
*/
389
EXT jmp_buf *jmpbuf;
390
#pragma omp threadprivate(jmpbuf)
391
392
EXT int term;
393
#pragma omp threadprivate(term)
394
395
#ifdef VAX
396
struct desc
397
{
398
int length;
399
char *addr;
400
} ;
401
#endif
402
403
#define COMMENT '!' /* comment introducer */
404
#define SYSTEM '$' /* system()-call introducer */
405
406
#define STRING_OUTPUT
407
408
#ifdef STRING_OUTPUT
409
#ifdef MODULE_MATC
410
static char *math_out_str = NULL;
411
static int math_out_count;
412
#pragma omp threadprivate (math_out_str, math_out_count)
413
#endif
414
#endif
415
416
void mem_free_all(void);
417
418
419
#ifdef MODULE_MATC
420
static int math_out_allocated = 0;
421
#pragma omp threadprivate (math_out_allocated)
422
void error_matc( char *format, ... )
423
{
424
va_list args;
425
426
va_start( args, format );
427
#ifdef STRING_OUTPUT
428
if ( math_out_count+512 > math_out_allocated )
429
{
430
math_out_allocated += 512;
431
math_out_str = (char *)realloc( math_out_str, math_out_allocated );
432
}
433
math_out_count += sprintf( &math_out_str[math_out_count], "MATC ERROR: " );
434
math_out_count += vsprintf( &math_out_str[math_out_count], format, args );
435
#else
436
fprintf( math_err, "MATC ERROR: " );
437
vfprintf( math_err, format, args );
438
#endif
439
va_end( args );
440
441
(void)mem_free_all();
442
longjmp( *jmpbuf, 2 );
443
}
444
445
void PrintOut( char *format, ... )
446
{
447
448
va_list args;
449
450
va_start( args, format );
451
#ifdef STRING_OUTPUT
452
if ( math_out_count+512 > math_out_allocated )
453
{
454
math_out_allocated += 512;
455
math_out_str = (char *)realloc( math_out_str, math_out_allocated );
456
}
457
math_out_count += vsprintf( &math_out_str[math_out_count], format, args );
458
#else
459
vfprintf( math_out, format, args );
460
#endif
461
va_end( args );
462
}
463
#else
464
extern void error_matc( char *format, ... );
465
extern void PrintOut( char *format, ... );
466
#endif
467
468
#define error error_matc
469
470
/*******************************************************************
471
function prototypes
472
*******************************************************************/
473
#include "fnames.h"
474
475