OpenSeesGFunEvaluator.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 2001, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** Reliability module developed by:                                   **
00020 **   Terje Haukaas (haukaas@ce.berkeley.edu)                          **
00021 **   Armen Der Kiureghian (adk@ce.berkeley.edu)                       **
00022 **                                                                    **
00023 ** ****************************************************************** */
00024                                                                         
00025 // $Revision: 1.10 $
00026 // $Date: 2006/09/05 23:36:48 $
00027 // $Source: /usr/local/cvs/OpenSees/SRC/reliability/analysis/gFunction/OpenSeesGFunEvaluator.cpp,v $
00028 
00029 
00030 //
00031 // Written by Terje Haukaas (haukaas@ce.berkeley.edu)
00032 //
00033 
00034 #include <OpenSeesGFunEvaluator.h>
00035 #include <Vector.h>
00036 #include <GFunEvaluator.h>
00037 #include <ReliabilityDomain.h>
00038 #include <LimitStateFunction.h>
00039 #include <RandomVariablePositioner.h>
00040 
00041 #include <tcl.h>
00042 #include <string.h>
00043 
00044 #include <fstream>
00045 #include <iomanip>
00046 #include <iostream>
00047 using std::ifstream;
00048 using std::ios;
00049 using std::setw;
00050 using std::setprecision;
00051 
00052 OpenSeesGFunEvaluator::OpenSeesGFunEvaluator(Tcl_Interp *passedTclInterp,
00053                                         ReliabilityDomain *passedReliabilityDomain,
00054                                         TCL_Char *passedFileName)
00055 :GFunEvaluator(passedTclInterp, passedReliabilityDomain)
00056 {
00057         fileName = new char[256];
00058         strcpy(fileName,passedFileName);
00059         nsteps = 0;
00060         dt = 0.0;
00061         // (here the user has provided a file with the analysis commmands)
00062 
00063         createRecorders();
00064 
00065 }
00066 
00067 OpenSeesGFunEvaluator::OpenSeesGFunEvaluator(Tcl_Interp *passedTclInterp,
00068                                         ReliabilityDomain *passedReliabilityDomain,
00069                                         int p_nsteps, double p_dt)
00070 :GFunEvaluator(passedTclInterp, passedReliabilityDomain)
00071 {
00072         fileName = 0;
00073         nsteps = p_nsteps;
00074         dt = p_dt;
00075         // (here the user has specified number of steps and possibly dt)
00076 
00077         createRecorders();
00078 }
00079 
00080 OpenSeesGFunEvaluator::~OpenSeesGFunEvaluator()
00081 {
00082         if (fileName != 0)
00083                 delete [] fileName;
00084 }
00085 
00086 
00087 int
00088 OpenSeesGFunEvaluator::runGFunAnalysis(Vector x)
00089 {
00090         // Zero out the response in the structural domain to make ready for next analysis
00091         char theRevertToStartCommand[10] = "reset";
00092         Tcl_Eval( theTclInterp, theRevertToStartCommand );
00093 
00094 
00095         // Put random variables into the structural domain according to the RandomVariablePositioners
00096         int numberOfRandomVariablePositioners = theReliabilityDomain->getNumberOfRandomVariablePositioners();
00097         RandomVariablePositioner *theRandomVariablePositioner;
00098         int rvNumber;
00099         int i;
00100         for ( i=1 ; i<=numberOfRandomVariablePositioners ; i++ )  {
00101                 theRandomVariablePositioner = theReliabilityDomain->getRandomVariablePositionerPtr(i);
00102                 rvNumber                                = theRandomVariablePositioner->getRvNumber();
00103                 theRandomVariablePositioner->update(x(rvNumber-1));
00104         }
00105 
00106 
00107         // Run the structural analysis according to user specified scheme
00108         double result = 0;
00109         if (dt==0.0 && nsteps==0 && !strcmp(fileName,"0") ) {
00110                 // Run up to max time in fFuncs
00111                 opserr << "OpenSeesGFunEvaluator: The option -runToMaxTimeInGFun " << endln
00112                         << " is not yet implemented." << endln;
00113         }
00114         else if (dt==0.0 && nsteps==0) {
00115                 // Read commands from file and execute them
00116                 // (The reason for doing it like this is that we want to check
00117                 // the flag returned by the analysis to see if it converged). 
00118                 char theAnalyzeCommand[30];
00119                 sprintf(theAnalyzeCommand,"[source %s]",fileName);
00120                 Tcl_ExprDouble( theTclInterp, theAnalyzeCommand, &result);
00121         }
00122         else {
00123                 // User has given "nsteps" and possibly "dt"
00124                 char theAnalyzeCommand[30];
00125                 if (dt == 0.0) {
00126                         sprintf(theAnalyzeCommand,"[analyze %d]",nsteps);
00127                         Tcl_ExprDouble( theTclInterp, theAnalyzeCommand, &result);
00128                 }
00129                 else {
00130                         sprintf(theAnalyzeCommand,"[analyze %d %10.5f]", nsteps, dt);
00131                         Tcl_ExprDouble( theTclInterp, theAnalyzeCommand, &result);
00132                 }
00133         
00134         }
00135 
00136         return ((int)result);
00137 }
00138 
00139 
00140 
00141 
00142 int
00143 OpenSeesGFunEvaluator::tokenizeSpecials(TCL_Char *theExpression)
00144 {
00145         // Set value of OpenSees finite element response quantities 
00146         // appearing in the limit-state function in the Tcl domain
00147         char buf[500]="";
00148         char tempchar[100]="";
00149         int i;
00150         double fileValue = 0.0;
00151         char tclAssignment[500]="";
00152         char temp[120];
00153 
00154 
00155         char separators[5] = "}{";
00156         char *lsf_forTokenizing = new char[1000];
00157         strcpy(lsf_forTokenizing,theExpression);
00158         char lsf_expression[1000] = "";
00159         char *dollarSign = "$";
00160         char *underscore = "_";
00161         char *tokenPtr = strtok( lsf_forTokenizing, separators);
00162         while ( tokenPtr != NULL ) {
00163 
00164                 strcpy(tempchar,tokenPtr);
00165 
00166                 // If a nodal velocity is detected
00167                 if ( strncmp(tokenPtr, "ud", 2) == 0) {
00168 
00169                         // Get node number and dof number
00170                         int nodeNumber, direction;
00171                         sscanf(tempchar,"ud_%i_%i", &nodeNumber, &direction);
00172 
00173                         // Assign value to the displacement quantity
00174                         char tclAssignment[100];
00175                         sprintf(tclAssignment,"set ud_%d_%d [nodeVel %d %d ]",nodeNumber,direction,nodeNumber,direction);
00176                         Tcl_Eval( theTclInterp, tclAssignment);
00177                 }
00178                 // If a nodal displacement is detected
00179                 else if ( strncmp(tokenPtr, "u", 1) == 0) {
00180 
00181                         // Get node number and dof number
00182                         int nodeNumber, direction;
00183                         sscanf(tempchar,"u_%i_%i", &nodeNumber, &direction);
00184 
00185                         // Assign value to the displacement quantity
00186                         char tclAssignment[100];
00187                         sprintf(tclAssignment,"set u_%d_%d [nodeDisp %d %d ]",nodeNumber,direction,nodeNumber,direction);
00188                         Tcl_Eval( theTclInterp, tclAssignment);
00189 
00190                 }
00191                 else if ( strncmp(tokenPtr, "rec",3) == 0) {
00192 
00193                         // Determine file name 
00194                         int lineNum = 0;
00195                         int colNum = 0;
00196                         char *variableName;
00197                         if ( strncmp(tokenPtr, "rec_node",8) == 0) {
00198                                 variableName = rec_node_occurrence(tempchar, false, lineNum, colNum);
00199                         }
00200                         else if ( strncmp(tokenPtr, "rec_element",11) == 0) {
00201                                 variableName = rec_element_occurrence(tempchar, false, lineNum, colNum);
00202                         }
00203                         char fileName[100];
00204                         sprintf(fileName,"%s.out",variableName);
00205 
00206                         // Open the file
00207                         ifstream inputFile(fileName,ios::in);
00208                         if (!inputFile) {
00209                                 opserr << "Could not open file with quantities for limit-state function." << endln;
00210                         }
00211 
00212                         // Count number of lines
00213                         int numberOfLines = 0;
00214                         while (inputFile.getline(buf,120)) {
00215                                 numberOfLines++;
00216                         }
00217                         
00218                         // Close and re-open file
00219                         inputFile.close();
00220                         ifstream inputFile2(fileName,ios::in);
00221                         
00222                         // Now pick last line and the right column
00223                         for (i=1; i<numberOfLines; i++) {
00224                                 inputFile2.getline(buf,120);
00225                         }
00226                         for (i=1; i<=colNum; i++) {
00227                                 inputFile2 >> temp;
00228                         }
00229                         fileValue = (double)atof(temp);
00230                         if (fileValue == 0.0) {
00231                                 opserr << "ERROR: Could not find quantity in recorder file." << endln;
00232                                 return -1;
00233                         }
00234                         inputFile2.close();
00235 
00236 
00237                         // Make the variable assignment
00238                         sprintf(tclAssignment, "set %s  %25.20e", variableName, fileValue);
00239                         Tcl_Eval( theTclInterp, tclAssignment);
00240 
00241                         delete variableName;
00242 
00243                 }
00244 
00245                 tokenPtr = strtok( NULL, separators);
00246         }
00247 
00248         delete [] lsf_forTokenizing;
00249 
00250         // Re-create possible recorders for subsequent analyses
00251         createRecorders();
00252 
00253         return 0;
00254 }
00255 
00256 
00257 void
00258 OpenSeesGFunEvaluator::setNsteps(int p_nsteps)
00259 {
00260         nsteps = p_nsteps;
00261 }
00262 
00263 double
00264 OpenSeesGFunEvaluator::getDt()
00265 {
00266         return dt;
00267 }
00268 
00269 
00270 
00271 
00272 
00273 int
00274 OpenSeesGFunEvaluator::createRecorders()
00275 {
00276 
00277 
00278 //      removeRecorders();
00279 
00280 
00281 
00282         // Download active limit-state function
00283         int lsf = theReliabilityDomain->getTagOfActiveLimitStateFunction();
00284         LimitStateFunction *theLimitStateFunction = theReliabilityDomain->getLimitStateFunctionPtr(lsf);
00285         char *theExpression = theLimitStateFunction->getExpression();
00286 
00287         // Initial declarations
00288         char tempchar[100]="";
00289         char separators[5] = "}{";
00290         char *lsf_forTokenizing = new char[500];
00291         strcpy(lsf_forTokenizing,theExpression);
00292 
00293         // Go through the limit-state function
00294         char *tokenPtr = strtok( lsf_forTokenizing, separators);
00295         while ( tokenPtr != NULL ) {
00296 
00297                 strcpy(tempchar,tokenPtr);
00298 
00299                 if ( strncmp(tokenPtr, "rec_node",8) == 0) {
00300                         int dummy1, dummy2;
00301                         rec_node_occurrence(tempchar, true, dummy1, dummy2);
00302                 }
00303                 else if ( strncmp(tokenPtr, "rec_element",11) == 0) {
00304                         int dummy1, dummy2;
00305                         rec_element_occurrence(tempchar, true, dummy1, dummy2);
00306                 }
00307 
00308                 tokenPtr = strtok( NULL, separators);
00309         }
00310 
00311         delete [] lsf_forTokenizing;
00312 
00313         return 0;
00314 }
00315 
00316 
00317 
00318 
00319 int
00320 OpenSeesGFunEvaluator::removeRecorders()
00321 {
00322         char tclAssignment[50] = "";
00323         sprintf(tclAssignment , "remove recorders");
00324         Tcl_Eval( theTclInterp, tclAssignment);
00325 
00326         return 0;
00327 }
00328 
00329 char *
00330 OpenSeesGFunEvaluator::rec_node_occurrence(char tempchar[100], bool createRecorder, int &line, int &col)
00331 {
00332         // Initial declarations
00333         char tclAssignment[500]="";
00334 
00335 
00336         // Get node and dof number etc.
00337         int nodeNumber, direction;
00338         char dispOrWhat[10];
00339         char restString[100];
00340         sscanf(tempchar,"rec_node_%s",restString);
00341         if ( strncmp(restString, "disp",4) == 0) {
00342                 // recorder Node fileName.out disp  -time -node 7 -dof 1  {rec_node_disp_7_1}
00343                 strcpy(dispOrWhat,"disp");
00344                 sscanf(restString,"disp_%i_%i", &nodeNumber, &direction);
00345         }
00346         else if ( strncmp(restString, "vel",3) == 0) {
00347                 // recorder Node fileName.out vel   -time -node 7 -dof 1  {rec_node_vel_7_1}
00348                 strcpy(dispOrWhat,"vel");
00349                 sscanf(restString,"vel_%i_%i", &nodeNumber, &direction);
00350         }
00351         else if ( strncmp(restString, "accel",5) == 0) {
00352                 // recorder Node fileName.out accel -time -node 7 -dof 1  {rec_node_accel_7_1}
00353                 strcpy(dispOrWhat,"accel");
00354                 sscanf(restString,"accel_%i_%i", &nodeNumber, &direction);
00355         }
00356         else {
00357                 opserr << "ERROR in syntax of limit-state function with recorder." << endln;
00358         }
00359 
00360         // Determine variable name 
00361         char *variableName;
00362         char tempString[100];
00363         sprintf(tempString, "rec_node_%s_%d_%d", dispOrWhat, nodeNumber, direction);
00364         variableName = new char[100];
00365         strcpy(variableName,tempString);
00366 
00367         if (createRecorder) {
00368 
00369                 // Create the recorder
00370                 sprintf(tclAssignment , "recorder Node %s.out %s -time -node %d -dof %d",variableName,dispOrWhat,nodeNumber,direction);
00371                 Tcl_Eval( theTclInterp, tclAssignment);
00372 
00373                 // Possibly create DDM gradient recorders                               
00374                 sprintf(tclAssignment , "recorder NodeGrad %s_ddm.out %s -time -node %d -dof %d",variableName,dispOrWhat,nodeNumber,direction);
00375 //              Tcl_Eval( theTclInterp, tclAssignment);
00376 
00377                 delete variableName;
00378                 return 0;
00379         }
00380 
00381         line = 0;
00382         col = 2;
00383         return variableName;
00384 }
00385 
00386 char *
00387 OpenSeesGFunEvaluator::rec_element_occurrence(char tempchar[100], bool createRecorder, int &line, int &col)
00388 {
00389 
00390         // Initial declarations
00391         char tclAssignment[500]="";
00392         char restString[100];
00393         char tempString[100];
00394 
00395         // Start obtaining information about element number etc. 
00396         int eleNumber;
00397         sscanf(tempchar,"rec_element_%i_%s", &eleNumber, restString);
00398 
00399         if ( strncmp(restString, "globalForce",11) == 0) {
00400                 // recorder Element 5 fileName.out -time globalForce {rec_element_5_globalForce_2}
00401                 int globalForceComponent;
00402                 sscanf(restString,"globalForce_%i", &globalForceComponent);
00403                 sprintf(tempString, "rec_element_%d_globalForce_%d", eleNumber, globalForceComponent);
00404                 line = 0;
00405                 col = globalForceComponent+1;
00406                 if (createRecorder) {
00407                         sprintf(tclAssignment , "recorder Element -ele %d -file %s.out -time globalForce",eleNumber, tempString);
00408                         Tcl_Eval( theTclInterp, tclAssignment);
00409                 }
00410         }
00411         else if ( strncmp(restString, "localForce",10) == 0) {
00412                 // recorder Element 5 fileName.out -time localForce {rec_element_5_localForce_2}
00413                 int localForceComponent;
00414                 sscanf(restString,"localForce_%i", &localForceComponent);
00415                 sprintf(tempString, "rec_element_%d_localForce_%d", eleNumber, localForceComponent);
00416                 line = 0;
00417                 col = localForceComponent+1;
00418                 if (createRecorder) {
00419                         sprintf(tclAssignment , "recorder Element -ele %d -file %s.out -time localForce",eleNumber, tempString);
00420                         Tcl_Eval( theTclInterp, tclAssignment);
00421                 }
00422         }
00423         else if ( strncmp(restString, "plasticDeformation",18) == 0) {
00424                 // recorder Element 5 fileName.out -time localForce {rec_element_5_localForce_2}
00425                 int localForceComponent;
00426                 sscanf(restString,"plasticDeformation_%i", &localForceComponent);
00427                 sprintf(tempString, "rec_element_%d_plasticDeformation_%d", eleNumber, localForceComponent);
00428                 line = 0;
00429                 col = localForceComponent+1;
00430                 if (createRecorder) {
00431                         sprintf(tclAssignment , "recorder Element -ele %d -file %s.out -time plasticDeformation",eleNumber, tempString);
00432                         Tcl_Eval( theTclInterp, tclAssignment);
00433                 }
00434         }
00435         else if ( strncmp(restString, "section",7) == 0) {
00436                 int sectionNumber;
00437                 sscanf(restString,"section_%i_%s", &sectionNumber, restString);
00438                 if ( strncmp(restString, "force",5) == 0) {
00439                         // recorder Element 5 fileName.out -time section 2 force {rec_element_5_section_2_force_1}
00440                         int sectionForceComponent;
00441                         sscanf(restString,"force_%i", &sectionForceComponent);
00442                         sprintf(tempString, "rec_element_%d_section_%d_force_%d", eleNumber, sectionNumber, sectionForceComponent);
00443                         line = 0;
00444                         col = sectionForceComponent+1;
00445                         if (createRecorder) {
00446                                 sprintf(tclAssignment , "recorder Element %d -file %s.out -time section %d force",eleNumber, tempString, sectionNumber);
00447                                 Tcl_Eval( theTclInterp, tclAssignment);
00448                         }
00449                 }
00450                 else if ( strncmp(restString, "deformation",11) == 0) {
00451                         // recorder Element 5 fileName.out -time section 2 deformation {rec_element_5_section_2_deformation_1}
00452                         int sectionDeformationComponent;
00453                         sscanf(restString,"deformation_%i", &sectionDeformationComponent);
00454                         sprintf(tempString, "rec_element_%d_section_%d_deformation_%d", eleNumber, sectionNumber, sectionDeformationComponent);
00455                         line = 0;
00456                         col = sectionDeformationComponent+1;
00457                         if (createRecorder) {
00458                                 sprintf(tclAssignment , "recorder Element %d -file %s.out -time section %d deformation",eleNumber, tempString, sectionNumber);
00459                                 Tcl_Eval( theTclInterp, tclAssignment);
00460                         }
00461                 }
00462                 else if ( strncmp(restString, "stiffness",9) == 0) {
00463                         // recorder Element 5 fileName.out -time section 2 stiffness {rec_element_5_section_2_stiffness_1}
00464                         int sectionStiffnessComponent;
00465                         sscanf(restString,"stiffness_%i", &sectionStiffnessComponent);
00466                         sprintf(tempString, "rec_element_%d_section_%d_stiffness_%d", eleNumber, sectionNumber, sectionStiffnessComponent);
00467                         line = 0;
00468                         col = sectionStiffnessComponent+1;
00469                         if (createRecorder) {
00470                                 sprintf(tclAssignment , "recorder Element %d -file %s.out -time section %d stiffness",eleNumber, tempString, sectionNumber);
00471                                 Tcl_Eval( theTclInterp, tclAssignment);
00472                         }
00473                 }
00474                 else if ( strncmp(restString, "fiber",5) == 0) {
00475                         // recorder Element 5 fileName.out -time section 2 fiber $y $z stressStrain {rec_element_5_section_2_fiber_2_36_6_54_stress}
00476                         int ya, yb, za, zb;
00477                         sscanf(restString,"fiber_%i_%i_%i_%i_%s", &ya, &yb, &za, &zb, restString);
00478                         sprintf(tempString, "rec_element_%d_section_%d_fiber_%d_%d_%d_%d_stressStrain", eleNumber, sectionNumber, ya, yb, za, zb);
00479                         line = 0;
00480                         if (createRecorder) {
00481                                 sprintf(tclAssignment , "recorder Element %d -file %s.out -time section %d fiber %d.%d %d.%d stressStrain",eleNumber, tempString, sectionNumber, ya, yb, za, zb);
00482                                 Tcl_Eval( theTclInterp, tclAssignment);
00483                         }
00484                         if (strcmp(restString,"stress")==0) {
00485                                 col = 2;
00486                         }
00487                         else if (strcmp(restString,"strain")==0) {
00488                                 col = 3;
00489                         }
00490                         else {
00491                                 opserr << "ERROR in syntax of limit-state function with recorder." << endln;
00492                         }
00493                 }
00494         }
00495         else {
00496                 opserr << "ERROR in syntax of limit-state function with recorder." << endln;
00497         }
00498 
00499 
00500         char *variableName;
00501         variableName = new char[100];
00502         strcpy(variableName,tempString);
00503         return variableName;
00504 }

Generated on Mon Oct 23 15:05:25 2006 for OpenSees by doxygen 1.5.0