📄 units.c
字号:
d3 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS));
ZeroUnits(d3);
d3->units_type = SI_US;
}
if( d->temp_expnt!=0 ) {
if( d->temp_expnt==1 ) {
d4 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS));
UnitsCopy(d4,dp4);
}
else
d4 = UnitsPower(dp4,d->temp_expnt, NO);
}
else {
d4 = (DIMENSIONS *)MyCalloc(1,sizeof(DIMENSIONS));
ZeroUnits(d4);
d4->units_type = SI_US;
}
dd1 = UnitsMult(d1,d2);
dd2 = UnitsMult(d3,d4);
UnitsMultRep(d,dd1,dd2);
free((char *)d1->units_name);
free((char *)d1);
free((char *)d2->units_name);
free((char *)d2);
free((char *)d3->units_name);
free((char *)d3);
free((char *)d4->units_name);
free((char *)d4);
free((char *)dp1->units_name);
free((char *)dp1);
free((char *)dp2->units_name);
free((char *)dp2);
free((char *)dp3->units_name);
free((char *)dp3);
free((char *)dp4->units_name);
free((char *)dp4);
free((char *)dd1->units_name);
free((char *)dd1);
free((char *)dd2->units_name);
free((char *)dd2);
#ifdef DEBUG
printf(" Leaving UnitsSimplify() \n");
#endif
return(d);
}
}
/*
* ============================================================
* RadUnitsSimplify() : Simplify Radian Units
*
* Input : DIMENSIONS *d : Pointer to dimensions data structure
* Ouput : DIMENSIONS *d : Pointer to "simplified" dimensions
* ============================================================
*/
#ifdef __STDC__
DIMENSIONS *RadUnitsSimplify( DIMENSIONS *d )
#else
DIMENSIONS *RadUnitsSimplify( d )
DIMENSIONS *d;
#endif
{
SYMBOL *hp;
#ifdef DEBUG
printf(" Enter RadUnitsSimplify() \n");
#endif
/* [a] Check waether unit names are in the symbol table */
if( d==(DIMENSIONS *)NULL ) {
printf("Warning: d is NULL, in RadUnitsSimplify()\n");
return (DIMENSIONS *)NULL;
}
hp = (SYMBOL *)NULL;
if(d->units_name != NULL)
hp = lookup(d->units_name);
if(hp != NULL)
return(d);
/* [b] Check weather the units are rad units */
if(d->length_expnt == 0 && d->time_expnt == 0 &&
d->mass_expnt == 0 && d->temp_expnt == 0 ) {
free((char *)d->units_name);
d->units_name = SaveString( (char *) "rad" );
d->scale_factor = 1.0;
d->units_type = SI_US;
return(d);
}
#ifdef DEBUG
printf(" Leaving RadUnitsSimplify() \n");
#endif
return(d);
}
/*
* =====================
* Print Units
* =====================
*/
#ifdef __STDC__
void UnitsPrint(DIMENSIONS *d)
#else
void UnitsPrint(d)
DIMENSIONS *d;
#endif
{
#ifdef DEBUG
printf("\n Enter UnitsPrint()\n");
#endif
if( d==(DIMENSIONS *)NULL ) {
printf("Unit d is NULL, in UnitsPrint()\n");
return;
}
if( d->units_name != (char *)NULL )
printf("\n units_name = %s\n", d->units_name);
else
printf("\n units_name = %s\n", (char *)"NULL");
printf(" d->units_type = %d \n",d->units_type);
printf(" d->scale_factor = %lg\n",d->scale_factor);
printf(" d->length_expnt = %g \n",d->length_expnt);
printf(" d->time_expnt = %g \n",d->time_expnt );
printf(" d->mass_expnt = %g \n",d->mass_expnt );
printf(" d->temp_expnt = %g \n",d->temp_expnt );
#ifdef DEBUG
printf("\n Leaving UnitsPrint()\n");
#endif
}
/*
* ==================================================================
* Usage :
* int : L ; char * name1, * name2, ...
* L = UnitsLength(name1, "STOP");
* or L = UnitsLength(name1, name2, ..., "STOP");
* ==================================================================
*/
#ifdef __STDC__
int UnitsLength( char *first_name, ... )
#else
int UnitsLength(va_alist)
va_dcl
#endif
{
char *name;
int length;
#ifdef __STDC__
va_list arg_ptr;
#else
va_list arg_ptr;
char *first_name;
#endif
#ifdef DEBUG
printf(" Enter UnitsLength() \n");
#endif
#ifdef __STDC__
va_start(arg_ptr, first_name);
#else
va_start(arg_ptr);
first_name = va_arg(arg_ptr, char *);
#endif
for (name = first_name, length = (int) 0; ; name = va_arg(arg_ptr, char * ) ) {
if(name != (char *)NULL && !strcmp(name, "STOP") )
break;
if (name != (char *)NULL) {
length += strlen(name);
}
else
length += 0;
}
va_end(arg_ptr);
#ifdef DEBUG
printf(" Leaving UnitsLength() length = %d\n", length);
#endif
return(length);
}
/*
* ==================================================
* BufferPrint() : Print contents of matrix buffer
* ==================================================
*/
#ifdef __STDC__
void BufferPrint(char *name, DIMENSIONS *array, int no_col)
#else
void BufferPrint(name, array, no_col)
char *name;
DIMENSIONS *array;
int no_col;
#endif
{
int i;
#ifdef DEBUG
printf("\n Enter BufferPrint()\n");
#endif
printf("\n in BufferPrint() no_of col = %d \n", no_col);
printf(" \n buffer_name = %s\n", name);
if(array != (DIMENSIONS *)NULL) {
for (i = 1; i <= no_col; i++) {
printf("\n col/row[%d] \n", i);
UnitsPrint(&array[i-1]);
}
}
else
printf("\n Buffer = NULL \n");
#ifdef DEBUG
printf("\n Leaving BufferPrint()\n");
#endif
}
/*
* ==================================================
* BufferInit() : Allocate and zero Matrix Buffer
*
* Input : int no_col : number of elements in buffer
* Output : pointer to array of DIMENSIONS.
* ==================================================
*/
#ifdef __STDC__
DIMENSIONS *BufferInit(int no_col)
#else
DIMENSIONS *BufferInit(no_col)
int no_col;
#endif
{
DIMENSIONS *array;
int i;
#ifdef DEBUG
printf("\n Enter BufferInit()\n");
#endif
array = (DIMENSIONS *) MyCalloc(no_col, sizeof(DIMENSIONS));
for( i = 1; i <= no_col; i++)
ZeroUnits(&array[i-1]);
#ifdef DEBUG
BufferPrint(" no name ", array, no_col);
printf("\n Leaving BufferInit()\n");
#endif
return (array);
}
/*
* ========================================================================
* ConvertTempUnits() : Convert temperature units
*
* Input :
* Output :
* ========================================================================
*/
#ifdef __STDC__
double ConvertTempUnits(char *name, double value,int units_type)
#else
double ConvertTempUnits(name, value, units_type)
char *name;
double value;
int units_type;
#endif
{
#ifdef DEBUG
printf("**** Enter ConvertTempUnits \n");
printf(" : name = %s \n", name);
printf(" : value= %g \n", value);
printf(" : type = %d \n", units_type);
#endif
switch(units_type) {
case SI: /* convert to SI */
if(name != (char *)NULL) {
if( !strcmp(name, "deg_C") || !strcmp(name, "deg_F") )
value = 5.0*(value - 32.0)/9.0;
else {
printf("*** In ConvertTempUnits(): units_name = %s", name);
FatalError("Try to convert a non temperature units",
(char *) NULL);
}
}
break;
case US: /* convert to US */
if(name != (char *)NULL) {
if( !strcmp(name, "deg_C") || !strcmp(name, "deg_F") )
value = 9.0*value/5.0 + 32.0;
else {
printf("*** In ConvertTempUnits(): units_name = %s", name);
FatalError("Try to convert a non temperature units",
(char *) NULL);
}
}
break;
}
return(value);
}
/*
* ========================================================================
* UnitsScaleConvert() :
*
* Input :
* Output :
* ========================================================================
*/
#ifdef __STDC__
DIMENSIONS UnitsScaleConvert(DIMENSIONS eng_units, int units_type)
#else
DIMENSIONS UnitsScaleConvert(eng_units, units_type)
DIMENSIONS eng_units;
int units_type;
#endif
{
switch(units_type) {
case SI:
/* convert from US to SI */
/* [a] length units */
if(eng_units.units_name != (char *)NULL && !strcmp("in", eng_units.units_name))
eng_units.scale_factor = 0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("ft", eng_units.units_name))
eng_units.scale_factor = 12.0*0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("mil", eng_units.units_name))
eng_units.scale_factor = 1E-3*0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("micro_in", eng_units.units_name))
eng_units.scale_factor = 1E-3*0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("yard", eng_units.units_name))
eng_units.scale_factor = 36.0*0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("mile", eng_units.units_name))
eng_units.scale_factor = 63360*0.0254;
/* [b] volumn units */
if(eng_units.units_name != (char *)NULL && !strcmp("gallon", eng_units.units_name))
eng_units.scale_factor = 3.785412E-3;
else if(eng_units.units_name != (char *)NULL && !strcmp("barrel", eng_units.units_name))
eng_units.scale_factor = 0.1589873;
/* [c] mass units */
if(eng_units.units_name != (char *)NULL && !strcmp("lb", eng_units.units_name))
eng_units.scale_factor = 0.4535924;
else if(eng_units.units_name != (char *)NULL && !strcmp("grain", eng_units.units_name))
eng_units.scale_factor = 0.4535924/7E+3;
else if(eng_units.units_name != (char *)NULL && !strcmp("ton", eng_units.units_name))
eng_units.scale_factor = 2E+3*0.4535924;
else if(eng_units.units_name != (char *)NULL && !strcmp("klb", eng_units.units_name))
eng_units.scale_factor = 1E+3*0.4535924;
/* [d] force units */
if(eng_units.units_name != (char *)NULL && !strcmp("lbf", eng_units.units_name))
eng_units.scale_factor = 0.4535924*9.80665;
else if(eng_units.units_name != (char *)NULL && !strcmp("kips", eng_units.units_name))
eng_units.scale_factor = 1E+3*0.4535924*9.80665;
/* [e] pressure units */
if(eng_units.units_name != (char *)NULL && !strcmp("psi", eng_units.units_name))
eng_units.scale_factor = 0.4535924*9.80665/0.0254/0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("ksi", eng_units.units_name))
eng_units.scale_factor = 1E+3*0.4535924*9.80665/0.0254/0.0254;
/* [f] incremental temperature units */
if(eng_units.units_name != (char *)NULL && !strcmp("DEG_F", eng_units.units_name))
eng_units.scale_factor = 5.0/9.0;
break;
case US:
/* convert from SI to US */
/* [a] length units */
if(eng_units.units_name != (char *)NULL && !strcmp("micron", eng_units.units_name))
eng_units.scale_factor = 1E-6/0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("mm", eng_units.units_name))
eng_units.scale_factor = 1E-3/0.0254;
else if(eng_units.units_name != (char *)NULL && !strcmp("cm", eng_units.units_name))
eng_units.scale_fa
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -