找回密码
 成为会员
查看: 1681|回复: 0

AVL Cruise C-function模块用法及注意事项官方文档

[复制链接]

394

帖子

8

精华

5883

铜板

管理员

Rank: 9Rank: 9Rank: 9

积分
75917
发表于 2021-11-10 21:28:35 | 显示全部楼层 |阅读模式

We are leading you to the next level!

您需要 登录 才可以下载或查看,没有帐号?成为会员

x
1. SUMMARY

The code should be written as a part of a C-function.
Following contents are allowed:
• Input Channels : a[0]-a[98]
• Output Channels : y[0]-y[98]
• Numerical constants : e.g. 1, 3.14, 0.05
• Standard "C" numerical functions :
• sqrt,exp,log10,log,asin,acos,atan,sinh
• cosh,tanh,sin,cos,tan,ceil,fabs,floor,atan2,pow,fmod
• Other standard "C" functions and key words: printf, if, else, while etc.
• Operators : +,-,*,/,&&(AND),||(OR),!(NOT),%(module of),>,<,>=,<=
• Brackets : (,),[,],{,}...
Function header ( double cFunctionXY(double a[], double y[]){ ) and function ending ( ;return y;} ) are automatically added.

One simple example of a partial formula would be e.g.
"y[0] = sin(2*a[0])/pow(a[1],1.5) - 2.3"
More info and more complex applications can be found in the text below or in the "C" language literature.

2. ADDITIONAL CODE

CRUISE C Interpreter works as follows:
The pieces of ‘Additional Code’ and of the main function from all the ‘Function’ components are joined together and sent to the C Interpreter within the initialization loop. During run-time the calls of main functions from each component are being executed in each time step.
//’Function’ Component 1
#include “Additional Code 1”
#include “Additional Code 2”
#include “Additional Code 3”
.
.
double cMainFunction_xy1(double realTime, double a[], double y[]){
#include “C-Function”
;return y;}

//Function 2
#include “Additional Code 1”
#include “Additional Code 2”
#include “Additional Code 3”
.
.
double cMainFunction_xy1(double realTime, double a[], double y[]){
#include “C-Function”
;return y;}
.
.
Etc.
The function names beginning with ‘cMainFunction_’ are different for each component and unique in the model.

Since the code from all components is merged together, the names of variables and user-defined functions must be different in order to avoid naming conflicts.

3. DETAILED DESCRIPTION

The syntax of the formula source code is based on C (ANSI C).

SOME FUNDAMENTALS / PECULIARITIES OF C SYNTAX

SUPPORTED DATA TYPES
• char          one byte;
• int             integer value, usually 4 bytes
• float          floating point value, usually 4 bytes
• double      floating point value, usually 8 bytes

GENERAL
• Variable and function names are case sensitive.
• Every statement has to be terminated by a SEMICOLON ';'.

• There may be more than one statements per line. One statement may extend over several lines (char terms between "" must be on one line, however).

• All variables MUST be DECLARED EXPLICITLY before the first executable statements.

• ARRAY ELEMENTS are accessed by the operator []. Note that arrays start with index 0! Example:
double a[3];
a[0] = 1; // assign 1 to first element in array
a[2] = 2; // assign 2 to last element in array
a[3] = 3; // ERROR!!! beyond array bounds!
• LOOPS over arrays thus are typically written like this
int i;
double a[3];
for(i = 0; i < 3; i++){
a = 10*i+0.3;
}• ORDER of ARRAY INDICES as stored in memory is different from FORTRAN,
e.g.
FORTRAN:
INTEGER LF(2,NFACE)
I = LF(1,327)
would be in C-Interpreter if the same memory is shared:
int LF[NFACE][2];
i = LF[326][0]; // (note that indices start with zero in C)

• The BUILT-IN MATH-FUNCTIONS include (returning double and taking one double argument; argument in radiants for trigonometric functions):sqrt, sin, cos, tan, asin, acos, atan, log, log10, exp
and taking two double arguments:
pow(x, exponent), atan2(y, x), fmod(x, div)
Note that the integer-modulo function is provided by the % operator,
e.g.:
int remainder;
remainder = 100 % 11;
Integer random numbers in the range [0,RAND_MAX] are returned by rand(). (RAND_MAX is in general the largest 4 Byte integer, i.e. 2147483647).
A new seed can be set by srand(int seed).
A floating point (double) random value in the range [0,1) is returned by drand().

• OUTPUT TO TERMINAL is done using the printf function, input by scanf, output/input to char-array by sprintf and sscanf, output/input to file by fprintf, fscanf (and also fputs, fgets).
For more information on the C-Language see the rich literature on the Internet (search e.g. for "C Tutorial" or "ANSI C"). Note however the differences between ANSI C and AST_SCI C listed below.

EXTENSIONS TO C SYNTAX
• C++ style comments "//" are allowed.

• Function arguments are PASSED BY REFERENCE! E.g., the interpreter code double f(int i, float[] f) { f = i; i = 3; }
would be written in C:
double f(int *i, float[] f) { f[*i] = *i; *i = 3; }

• A SIMPLIFIED PRINT STATEMENT is supported, eg:
double d =3.14;
int i = 123;
print "d is ", d, ", and 2 times i is ", 2*i;

• DYNAMIC ALLOCATION to arrays, e.g.:
int n = 20;
int dynArr[10*n][3];

• Dynamic reallocation of arrays:
int resize(array[], int newSize);
newSize is number of elements allocated (number of char's, int's,double's etc.), not number of bytes!
resize(array, 0) frees memory. ATTENTION: after resizing to zero it can not be resized again! use resize(array, 1) instead!

• DYNAMIC INITILIZATIONS supported, e.g.:
double pi = acos(-1.0);
double pi2 = 2.0*pi;
• VECTOR OPERATIONS supported for char, int, float and double [], e.g.:
(vectors of different length are handled by wrapping around indices,length of result vector will be length of longer vector, except for assignments of course)
double a[3] = {1, 2, 3.7e-5}; // list initialization
double b[3] = a + 1.0; // b = a + 1.0; (i=0,1,2)
double c[3] = a * b; // c = a * b; (i=0,1,2)
double d[3] = 2.7; // d = 2.7; (i=0,1,2)
double e[3] = {2.7}; // e[0] = 2.7;
double m;
e = {-1, 2, 3.2}; // list assignment is allowed
print "vector e:", e; // print supports vector output! (printf does not!)
• Additional operator for CROSS AND DOT PRODUCT and vector magnitude, e.g. continuing above code:
c = a^b; // vector cross product, definition:
// c = a[(i+1)%sa]*b[(i+2)%sb] - a[(i+2)%sa]*b[(i+1)%sb]
// is sa and sb are length of vectors a and b;
m = a.b; // vector dot product, returns double
m = |a|; // vector magnitude (same as sqrt(a.a))
Note that the ^ operator is the cross product for vectors of length three.
For vectors of length 2 the usual cross product will be the second component:
double a[2] = {1, -2};
double b[2] = {-1, 5};
double c[2] = a^b;
double crossp2 = c[1]; // 2D cross product; (c[0] will be -c[1]);
For vectors of other length the ^ operator will probably not be useful.

• A FLOATING POINT (double) RANDOM VALUE in the range [0,1) is returned by drand().

LIMITATIONS
• No pointers except FILE*.
• No call by value.
• No structs.
• No typedefs.
• No block scope variables. Only global scope and function scope.
• No goto or labels.
• No switch/case statements.
• Only one declaration per statement., i.e.
int i, j = 2; // will generate syntax error!!!
is not allowed, but
int i; int j = 2;
is.
• No short, long, unsigned.
• Argument names must be provided in function declarations.
• Empty argument list must be used instead of "void".
• Max 10 arguments in calls to external functions.
• Only call by reference in external functions.
• Recursion not supported yet.
• Can't print the character " (neither with \" nor "").
• strchr, strrchr, strstr not available, substituted by istrchr, istrrchr and istrstr; istrchr, istrrchr and istrstr return int instead of pointer, which is index of
corresponding position in char-array.
• Casting (explicit or implicit) of vectors not supported, e.g.
int i, j = 2; // will generate syntax error!!!
is not allowed, but
int i; int j = 2;
is.
double a[3] = {1, 2, 3}; // OK: scalars cast to double components
int b[3] = {1, 2.0, 3}; // OK: scalars cast to int components
a += b; // not supported because b would have to be cast to double[] first!
b += 1; // supported: same effect as "for(i=0;i<3;i++) b += 1;"
b += 1.0; // supported (1.0 would be cast to scalar int), otherwise as above
b = b + 1.0; // not suported because term (b+1.0) would have to be cast to float[]!
b = b + 1; // supported, no cast needed

HINTS AND TRICKS
• Loop overhead: avoid short loops; better use
a[0] = x[0];
a[1] = x[1];
a[2] = x[2];
instead of
for(j = 0; j < 3; j++) a[j] = x[j];
or even better use vector assignment:
a = x;
• Arrays are allocated dynamically at run time if the dimension is not a constant integer but any integer expression. Otherwise memory is allocated at parse time.
• Use dynamic allocation for large arrays:
double a[1*1000]; // dynamic allocation
double a[1000]; // allocation at parse time!
or
double a[1]; // allocation of 1
resize(a, 1000); // reallocation at run time;
...
resize(a, 1); // reallocation to 1 at end of function;
// if reallocation to 0,
// it can not be reallocated again!

KNOWN BUGS
A few syntax errors may not be reported correctly at parse time.

LIST OF KEYWORDS/TOKENS
• "while"
• "for"
• "break"
• "continue"
• "return"
• "if"
• "else"
• "fprintf"
• "sprintf"
• "printf"
• "fscanf"
• "sscanf"
• "scanf"
• "print"
• "FILE"
• "fopen"
• "fclose"
• "fgetc"
• "fputc"
• "fgets"
• "fputs"
• "ferror"
• "feof"
• "NULL"
• "EOF"
• "exit"
• "system"
• "resize"
• "strcpy"
• "strncpy"
• "strcat"

• "strncat"
• "strcmp"
• "strncmp"
• "strlen"
• "istrchr"
• "istrrchr"
• "istrstr"
• "sqrt"
• "exp"
• "log10"
• "log"
• "asin"
• "acos"
• "atan"
• "sinh"
• "cosh"
• "tanh"
• "sin"
• "cos"
• "tan"
• "ceil"
• "fabs"
• "floor"
• "atan2"
• "pow"
• "fmod"
• "extern"
• "void"
• "char"
• "int"
• "float"
• "double"

• {letter}[A-Za-z_0-9]* variable/function name
• "++" unary increment (postfix or prefix)
• "--" unary decrement (postfix or prefix)
• ">=" comparator greater equal
• "<=" comparator
• "==" comparator
• "!=" comparator
• "+=" binary add
• "-=" binary substract
• "*=" binary multiplication
• "/=" binary division
• "&&" logical AND
• "||" locical OR
• "!" logical NOT
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

Archiver|手机版|AutoSim仿真教程 ( 皖ICP备15024617号-9 )

GMT+8, 2024-4-25 18:38 , Processed in 0.031961 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc. Design AutoSim

快速回复 返回顶部 返回列表