⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 units.c

📁 有限元程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *  =============================================================================  *  ALADDIN Version 2.1. *                                                                      *  units.c : Functions to handle engineering units/dimensions *                                                                      *  Copyright (C) 1995-2000 by Mark Austin, Xiaoguang Chen, and Wane-Jang Lin *  Institute for Systems Research,                                            *  University of Maryland, College Park, MD 20742                                    *                                                                      *  This software is provided "as is" without express or implied warranty. *  Permission is granted to use this software on any computer system *  and to redistribute it freely, subject to the following restrictions: *  *  1. The authors are not responsible for the consequences of use of *     this software, even if they arise from defects in the software. *  2. The origin of this software must not be misrepresented, either *     by explicit claim or by omission. *  3. Altered versions must be plainly marked as such and must not *     be misrepresented as being the original software. *  4. This software may not be sold or included in commercial software *     products without a license.  *  5. This notice is to remain intact. *                                                                     *  Written by: Mark Austin, Xiaoguang Chen, and Wane-Jang Lin         March 2000 *  =============================================================================  */#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#ifdef  __STDC__#include <stdarg.h>#else#include <varargs.h>#endif#include "defs.h"#include "miscellaneous.h"#include "units.h"#include "matrix.h"#include "fe_database.h"#include "symbol.h"static enum {UNITS_OFF, UNITS_ON} iFlag = UNITS_ON;static int UNITS_TYPE;/* #define DEBUG *//* *  =========================================================== *  Utility Functions for Units Operations *   *  SetUnitsOn()     : Set flag for units "on" *  SetUnitsOff()    : Set flag for units "off" *  CheckUnits()     : Flag to see if units are "on" or "off" *  CheckUnitsType() : Check type of units *  =========================================================== */int SetUnitsOn(){    iFlag = UNITS_ON;  }int SetUnitsOff(){    iFlag = UNITS_OFF;  }int CheckUnits(){    return(iFlag);}int CheckUnitsType(){    return(UNITS_TYPE);}#ifdef  __STDC__int ChangeUnitsType( int index )#elseint ChangeUnitsType( index )int index;#endif{    switch( index ) {       case 1:          UNITS_TYPE = US;          break;       case 2:          UNITS_TYPE = SI;          break;       default:          UNITS_TYPE = SI_US;          break;    }}/* *  ======================================== *  Do two quantities have the same units ?? *  ======================================== */#ifdef __STDC__int SameUnits(DIMENSIONS *d1, DIMENSIONS *d2)#elseint SameUnits(d1, d2)DIMENSIONS *d1, *d2;#endif{    if((d1->length_expnt  == d2->length_expnt) &&       (d1->mass_expnt    == d2->mass_expnt)   &&       (d1->time_expnt    == d2->time_expnt)   &&       (d1->temp_expnt    == d2->temp_expnt))  {         return TRUE;    }    else {         return FALSE;    }}/* *  ================================================ *  Functions for Mutiply/Divide Operations on Units *  ================================================ *//* *  =============================================================== *  UnitsMult() : Multiply Dimensional Units. *  *  Input : DIMENSIONS *d1 -- pointer to dimensions data structure *          DIMENSIONS *d2 -- pointer to dimensions data structure *  Input : DIMENSIONS  *d -- product of dimensions data structures *  =============================================================== */#ifdef __STDC__DIMENSIONS *UnitsMult(DIMENSIONS *d1, DIMENSIONS *d2)#elseDIMENSIONS *UnitsMult(d1, d2)DIMENSIONS *d1, *d2;#endif{DIMENSIONS         *d;DIMENSIONS *dp1, *dp2;int            length;#ifdef DEBUG       printf("Enter UnitsMult() \n");       UnitsPrint(d1);       UnitsPrint(d2);#endif    if( d1==(DIMENSIONS *)NULL || d2==(DIMENSIONS *)NULL ) {        printf("Warning: d1 or d2 is NULL,   in UnitsMult()\n");        return (DIMENSIONS *)NULL;    }    d   = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    dp1 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    dp2 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    UnitsCopy(dp1,d1);    UnitsCopy(dp2,d2);    if(dp1->units_type == dp2->units_type) {       d->units_type = dp1->units_type;    }    else {       if( dp1->units_type==SI && dp2->units_type==US ) {           d->units_type = SI;           if( dp2->units_name != (char *)NULL )              UnitsTypeConvert( dp2, SI );       }       else if( dp1->units_type==US && dp2->units_type==SI ) {           d->units_type = SI;           if( dp1->units_name != (char *)NULL )              UnitsTypeConvert( dp1, SI );       }       else if(dp1->units_type == SI_US)           d->units_type = dp2->units_type;       else if(dp2->units_type == SI_US)           d->units_type = dp1->units_type;    }	    if( dp1->units_name!=(char *)NULL ) {       length = UnitsLength(dp1->units_name,dp2->units_name,"STOP");       d->units_name = (char *)MyCalloc(length+2,sizeof(char));       d->units_name = strcpy(d->units_name, dp1->units_name);       if( dp2->units_name!=(char *)NULL ) {          d->units_name = strcat(d->units_name, (char *) ".");          d->units_name = strcat(d->units_name, dp2->units_name);       }    }    else if( dp2->units_name!=(char *)NULL )       d->units_name = SaveString(dp2->units_name);    else       d->units_name = (char *)NULL;    d->scale_factor = dp1->scale_factor*dp2->scale_factor;    d->length_expnt = dp1->length_expnt + dp2->length_expnt;    d->mass_expnt   = dp1->mass_expnt + dp2->mass_expnt;    d->time_expnt   = dp1->time_expnt + dp2->time_expnt;    d->temp_expnt   = dp1->temp_expnt + dp2->temp_expnt;    d->radian_expnt = dp1->radian_expnt + dp2->radian_expnt;    free((char *)dp1->units_name);    free((char *)dp1);    free((char *)dp2->units_name);    free((char *)dp2);    return (d);}/* *  ===================================================================== *  UnitsMultRep() : Units multiply with replacement *  *  Input :  DIMENSIONS *d1 -- pointer to dimensions data structure *           DIMENSIONS *d2 -- pointer to dimensions data structure *           DIMENSIONS  *d -- structure for result of dimensions product *  Output : DIMENSIONS  *d -- structure for result of dimensions product *  ===================================================================== */#ifdef __STDC__DIMENSIONS *UnitsMultRep(DIMENSIONS *d, DIMENSIONS *d1, DIMENSIONS *d2)#elseDIMENSIONS *UnitsMultRep(d, d1, d2)DIMENSIONS       *d;DIMENSIONS *d1, *d2;#endif{DIMENSIONS *dp1, *dp2;int            length;#ifdef DEBUG       printf("ENTER UnitsMultRep() \n");       UnitsPrint(d);#endif    if( d1==(DIMENSIONS *)NULL || d2==(DIMENSIONS *)NULL ) {        printf("Warning : Dimensions d1 and d2 cannot be NULL\n");        free((char *)d->units_name);        free((char *)d);        d = (DIMENSIONS *) NULL;        return (DIMENSIONS *) NULL;    }    dp1 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    dp2 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    UnitsCopy(dp1,d1);    UnitsCopy(dp2,d2);    if(dp1->units_type == dp2->units_type) {       d->units_type = dp1->units_type;    }    else {       if( dp1->units_type==SI && dp2->units_type==US ) {           d->units_type = SI;           if( dp2->units_name != (char *)NULL )              UnitsTypeConvert( dp2, SI );       }       else if( dp1->units_type==US && dp2->units_type==SI ) {           d->units_type = SI;           if( dp1->units_name != (char *)NULL )              UnitsTypeConvert( dp1, SI );       }       else if(dp1->units_type == SI_US)           d->units_type = dp2->units_type;       else if(dp2->units_type == SI_US)           d->units_type = dp1->units_type;    }	    if( dp1->units_name != (char *)NULL ) {       free((char *)d->units_name);       length = UnitsLength(dp1->units_name,dp2->units_name,"STOP");       d->units_name = (char *) MyCalloc(length+2,sizeof(char));       d->units_name = strcpy(d->units_name, dp1->units_name);       if( dp2->units_name != (char *)NULL ) {          d->units_name = strcat(d->units_name, (char *) ".");          d->units_name = strcat(d->units_name, dp2->units_name);       }    }    else if( dp2->units_name != (char *)NULL ) {       free((char *) d->units_name);       d->units_name = SaveString(dp2->units_name);    }    else {       free((char *) d->units_name);       d->units_name = (char *)NULL;    }    d->scale_factor = dp1->scale_factor*dp2->scale_factor;    d->length_expnt = dp1->length_expnt + dp2->length_expnt;    d->mass_expnt   = dp1->mass_expnt + dp2->mass_expnt;    d->time_expnt   = dp1->time_expnt + dp2->time_expnt;    d->temp_expnt   = dp1->temp_expnt + dp2->temp_expnt;    d->radian_expnt = dp1->radian_expnt + dp2->radian_expnt;    free((char *)dp1->units_name);    free((char *)dp1);    free((char *)dp2->units_name);    free((char *)dp2);#ifdef DEBUG       printf("Leave UnitsMultRep() \n");#endif    return (d);}/* *  ===================================================================== *  UnitsDiv() : Divide units *  *  Input :  DIMENSIONS *d1 -- pointer to dimensions data structure *           DIMENSIONS *d2 -- pointer to dimensions data structure *           int FLAG       -- YES or NO flag for "()" brackete. *  Output : DIMENSIONS  *d -- structure for result of dimensions product *  ===================================================================== */#ifdef __STDC__DIMENSIONS *UnitsDiv( DIMENSIONS *d1, DIMENSIONS *d2, int FLAG )#elseDIMENSIONS *UnitsDiv( d1, d2, FLAG )DIMENSIONS *d1, *d2;int            FLAG;           /* YES or NO flag : to determine wheather "()" is needed */#endif{DIMENSIONS         *d;DIMENSIONS *dp1, *dp2;int            length;#ifdef DEBUG       printf("Enter UnitsDiv() \n");       UnitsPrint(d1);       UnitsPrint(d2);#endif    if( d2==(DIMENSIONS *)NULL )        FatalError("In UnitsDiv() : unit divider is a NULL pointer",(char *)NULL);    if( d1==(DIMENSIONS *)NULL ) {        printf("Warning: d1 is NULL,   in UnitsDiv()\n");        return (DIMENSIONS *)NULL;    }    d   = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    dp1 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    dp2 = (DIMENSIONS *) MyCalloc(1, sizeof(DIMENSIONS));    UnitsCopy(dp1,d1);    UnitsCopy(dp2,d2);    if(dp1->units_type == dp2->units_type) {       d->units_type = dp1->units_type;    }    else {       if( dp1->units_type==SI && dp2->units_type==US ) {           d->units_type = SI;           if( dp2->units_name != (char *)NULL )              UnitsTypeConvert( dp2, SI );       }       else if( dp1->units_type==US && dp2->units_type==SI ) {           d->units_type = SI;           if( dp1->units_name != (char *)NULL )              UnitsTypeConvert( dp1, SI );       }       else if(dp1->units_type == SI_US)           d->units_type = dp2->units_type;       else if(dp2->units_type == SI_US)           d->units_type = dp1->units_type;    }	    if(dp1->units_name != (char *)NULL) {       if( FLAG == YES) {           length = UnitsLength(dp1->units_name,dp2->units_name,"STOP");          d->units_name = (char *)MyCalloc(length+6,sizeof(char));          d->units_name = strcpy(d->units_name, (char *) "(");          d->units_name = strcat(d->units_name, dp1->units_name);          d->units_name = strcat(d->units_name, (char *) ")");          if( dp2->units_name!=(char *)NULL ) {              d->units_name = strcat(d->units_name, (char *) "/(");              d->units_name = strcat(d->units_name, dp2->units_name);              d->units_name = strcat(d->units_name, (char *) ")");          }       }       else {          length = UnitsLength(dp1->units_name,dp2->units_name,"STOP");          d->units_name = (char *)MyCalloc(length+2,sizeof(char));          d->units_name = strcpy(d->units_name, dp1->units_name);          if( dp2->units_name!=(char *)NULL ) {              d->units_name = strcat(d->units_name, (char *) "/");              d->units_name = strcat(d->units_name, dp2->units_name);          }       }     }    else if(dp2->units_name != (char *)NULL) {       if( FLAG == YES) {           length = UnitsLength(dp2->units_name,"STOP");          d->units_name = (char *)MyCalloc(length+5,sizeof(char));          d->units_name = strcpy(d->units_name, (char *)"1/(");          d->units_name = strcat(d->units_name, dp2->units_name);          d->units_name = strcat(d->units_name, (char *) ")");       }       else {          length = UnitsLength(dp2->units_name,"STOP");          d->units_name = (char *)MyCalloc(length+3,sizeof(char));          d->units_name = strcpy(d->units_name, (char *)"1/");          d->units_name = strcat(d->units_name, dp2->units_name);       }    }    else       d->units_name = (char *)NULL;    d->scale_factor = dp1->scale_factor/dp2->scale_factor;    d->mass_expnt = dp1->mass_expnt - dp2->mass_expnt;    d->length_expnt = dp1->length_expnt - dp2->length_expnt;    d->time_expnt   = dp1->time_expnt - dp2->time_expnt;    d->temp_expnt   = dp1->temp_expnt - dp2->temp_expnt;    d->radian_expnt = dp1->radian_expnt - dp2->radian_expnt;    free((char *)dp1->units_name);    free((char *)dp1);    free((char *)dp2->units_name);    free((char *)dp2);    return (d);}/* *  ===================================================================== *  UnitsDivRep() : Units divide with replacement *  *  Input :  DIMENSIONS *d1 -- pointer to dimensions data structure *           DIMENSIONS *d2 -- pointer to dimensions data structure *           DIMENSIONS  *d -- structure for result of dimensions product *           int FLAG       -- YES or NO flag for "()" brackete. *  Output : DIMENSIONS  *d -- structure for result of dimensions product *  ===================================================================== */#ifdef __STDC__DIMENSIONS *UnitsDivRep( DIMENSIONS *d, DIMENSIONS *d1, DIMENSIONS *d2, int FLAG )#elseDIMENSIONS *UnitsDivRep( d, d1, d2, FLAG )DIMENSIONS       *d;DIMENSIONS *d1, *d2;int            FLAG;#endif{DIMENSIONS *dp1, *dp2;int            length;#ifdef DEBUG       printf(" Enter UnitsDivRep() : ================\n");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -