00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <SamplingAnalysis.h>
00035 #include <ReliabilityDomain.h>
00036 #include <ReliabilityAnalysis.h>
00037 #include <LimitStateFunction.h>
00038 #include <ProbabilityTransformation.h>
00039 #include <NatafProbabilityTransformation.h>
00040 #include <GFunEvaluator.h>
00041 #include <BasicGFunEvaluator.h>
00042 #include <RandomNumberGenerator.h>
00043 #include <RandomVariable.h>
00044 #include <NormalRV.h>
00045 #include <Vector.h>
00046 #include <Matrix.h>
00047 #include <MatrixOperations.h>
00048 #include <NormalRV.h>
00049 #include <math.h>
00050 #include <stdlib.h>
00051 #include <string.h>
00052
00053 #include <fstream>
00054 #include <iomanip>
00055 #include <iostream>
00056 using std::ifstream;
00057 using std::ios;
00058 using std::setw;
00059 using std::setprecision;
00060 using std::setiosflags;
00061
00062
00063 SamplingAnalysis::SamplingAnalysis( ReliabilityDomain *passedReliabilityDomain,
00064 ProbabilityTransformation *passedProbabilityTransformation,
00065 GFunEvaluator *passedGFunEvaluator,
00066 RandomNumberGenerator *passedRandomNumberGenerator,
00067 int passedNumberOfSimulations,
00068 double passedTargetCOV,
00069 double passedSamplingStdv,
00070 int passedPrintFlag,
00071 TCL_Char *passedFileName,
00072 Vector *pStartPoint,
00073 int passedAnalysisTypeTag)
00074 :ReliabilityAnalysis()
00075 {
00076 theReliabilityDomain = passedReliabilityDomain;
00077 theProbabilityTransformation = passedProbabilityTransformation;
00078 theGFunEvaluator = passedGFunEvaluator;
00079 theRandomNumberGenerator = passedRandomNumberGenerator;
00080 numberOfSimulations = passedNumberOfSimulations;
00081 targetCOV = passedTargetCOV;
00082 samplingStdv = passedSamplingStdv;
00083 printFlag = passedPrintFlag;
00084 fileName = new char[256];
00085 strcpy(fileName,passedFileName);
00086 startPoint = pStartPoint;
00087 analysisTypeTag = passedAnalysisTypeTag;
00088 }
00089
00090
00091
00092
00093 SamplingAnalysis::~SamplingAnalysis()
00094 {
00095 if (fileName != 0)
00096 delete [] fileName;
00097 }
00098
00099
00100
00101 int
00102 SamplingAnalysis::analyze(void)
00103 {
00104
00105
00106 opserr << "Sampling Analysis is running ... " << endln;
00107
00108
00109 double gFunctionValue;
00110 int result;
00111 int I, i, j;
00112 int k = 1;
00113 int seed = 1;
00114 double det_covariance;
00115 double phi;
00116 double h;
00117 double q;
00118 int numRV = theReliabilityDomain->getNumberOfRandomVariables();
00119 Matrix covariance(numRV, numRV);
00120 Matrix chol_covariance(numRV, numRV);
00121 Matrix inv_covariance(numRV, numRV);
00122 Vector startValues(numRV);
00123 Vector x(numRV);
00124 Vector z(numRV);
00125 Vector u(numRV);
00126 Vector randomArray(numRV);
00127 LimitStateFunction *theLimitStateFunction = 0;
00128 NormalRV *aStdNormRV = 0;
00129 aStdNormRV = new NormalRV(1,0.0,1.0,0.0);
00130
00131 bool failureHasOccured = false;
00132 char myString[50];
00133
00134
00135
00136 if (aStdNormRV==0) {
00137 opserr << "SamplingAnalysis::analyze() - out of memory while instantiating internal objects." << endln;
00138 return -1;
00139 }
00140
00141
00142
00143 for (i=0; i<numRV; i++) {
00144 for (j=0; j<numRV; j++) {
00145 if (i==j) {
00146 covariance(i,j) = samplingStdv*samplingStdv;
00147 }
00148 else
00149 covariance(i,j) = 0.0;
00150 }
00151 }
00152
00153
00154
00155 MatrixOperations *theMatrixOperations = 0;
00156 theMatrixOperations = new MatrixOperations(covariance);
00157 if (theMatrixOperations == 0) {
00158 opserr << "SamplingAnalysis::analyze() - could not create" << endln
00159 << " the object to perform matrix operations." << endln;
00160 return -1;
00161 }
00162
00163
00164
00165 result = theMatrixOperations->computeLowerCholesky();
00166 if (result < 0) {
00167 opserr << "SamplingAnalysis::analyze() - could not compute" << endln
00168 << " the Cholesky decomposition of the covariance matrix." << endln;
00169 return -1;
00170 }
00171 chol_covariance = theMatrixOperations->getLowerCholesky();
00172
00173
00174
00175 result = theMatrixOperations->computeInverse();
00176 if (result < 0) {
00177 opserr << "SamplingAnalysis::analyze() - could not compute" << endln
00178 << " the inverse of the covariance matrix." << endln;
00179 return -1;
00180 }
00181 inv_covariance = theMatrixOperations->getInverse();
00182
00183
00184
00185 result = theMatrixOperations->computeTrace();
00186 if (result < 0) {
00187 opserr << "SamplingAnalysis::analyze() - could not compute" << endln
00188 << " the trace of the covariance matrix." << endln;
00189 return -1;
00190 }
00191 det_covariance = theMatrixOperations->getTrace();
00192
00193
00194
00195 double pi = 3.14159265358979;
00196 double factor1 = 1.0 / ( pow((2.0*pi),((double)numRV/2.0)) );
00197 double factor2 = 1.0 / ( pow((2.0*pi),((double)numRV/2.0)) * sqrt(det_covariance) );
00198
00199
00200
00201 int numLsf = theReliabilityDomain->getNumberOfLimitStateFunctions();
00202
00203
00204
00205
00206 Vector sum_q(numLsf);
00207 Vector sum_q_squared(numLsf);
00208 double var_qbar;
00209 double pfIn;
00210 double CovIn;
00211 char restartFileName[256];
00212 sprintf(restartFileName,"%s_%s","restart",fileName);
00213
00214 if (analysisTypeTag == 1) {
00215
00216 if (printFlag == 2) {
00217 ifstream inputFile( restartFileName, ios::in );
00218 inputFile >> k;
00219 inputFile >> seed;
00220 if (k==1 && seed==1) {
00221 }
00222 else {
00223 for (int i=1; i<=numLsf; i++) {
00224 inputFile >> pfIn;
00225 if (pfIn > 0.0) {
00226 failureHasOccured = true;
00227 }
00228 inputFile >> CovIn;
00229 sum_q(i-1) = pfIn*k;
00230 var_qbar = (CovIn*pfIn)*(CovIn*pfIn);
00231 if (k<1.0e-6) {
00232 opserr << "WARNING: Zero number of samples read from restart file" << endln;
00233 }
00234 sum_q_squared(i-1) = k*( k*var_qbar + pow(sum_q(i-1)/k,2.0) );
00235 }
00236 k++;
00237 }
00238 inputFile.close();
00239 }
00240 }
00241
00242
00243
00244
00245 Vector startPointY(numRV);
00246 if (startPoint == 0) {
00247
00248 }
00249 else {
00250 result = theProbabilityTransformation->set_x(*startPoint);
00251 if (result < 0) {
00252 opserr << "SamplingAnalysis::analyze() - could not " << endln
00253 << " set the x-vector for xu-transformation. " << endln;
00254 return -1;
00255 }
00256 result = theProbabilityTransformation->transform_x_to_u();
00257 if (result < 0) {
00258 opserr << "SamplingAnalysis::analyze() - could not " << endln
00259 << " transform x to u. " << endln;
00260 return -1;
00261 }
00262 startPointY = theProbabilityTransformation->get_u();
00263 }
00264
00265
00266
00267 Vector cov_of_q_bar(numLsf);
00268 Vector q_bar(numLsf);
00269 Vector variance_of_q_bar(numLsf);
00270 Vector responseVariance(numLsf);
00271 Vector responseStdv(numLsf);
00272 Vector g_storage(numLsf);
00273 Vector sum_of_g_minus_mean_squared(numLsf);
00274 Matrix crossSums(numLsf,numLsf);
00275 Matrix responseCorrelation(numLsf,numLsf);
00276 char string[60];
00277 Vector pf(numLsf);
00278 Vector cov(numLsf);
00279 double govCov = 999.0;
00280 Vector temp1;
00281 double temp2;
00282 double denumerator;
00283 bool FEconvergence;
00284
00285
00286
00287 ofstream resultsOutputFile( fileName, ios::out );
00288
00289
00290 bool isFirstSimulation = true;
00291 while( (k<=numberOfSimulations && govCov>targetCOV || k<=2) ) {
00292
00293
00294 if (printFlag == 1 || printFlag == 2) {
00295 opserr << "Sample #" << k << ":" << endln;
00296 }
00297
00298
00299
00300 if (isFirstSimulation) {
00301 result = theRandomNumberGenerator->generate_nIndependentStdNormalNumbers(numRV,seed);
00302 }
00303 else {
00304 result = theRandomNumberGenerator->generate_nIndependentStdNormalNumbers(numRV);
00305 }
00306 seed = theRandomNumberGenerator->getSeed();
00307 if (result < 0) {
00308 opserr << "SamplingAnalysis::analyze() - could not generate" << endln
00309 << " random numbers for simulation." << endln;
00310 return -1;
00311 }
00312 randomArray = theRandomNumberGenerator->getGeneratedNumbers();
00313
00314
00315 u = startPointY + chol_covariance * randomArray;
00316
00317
00318 result = theProbabilityTransformation->set_u(u);
00319 if (result < 0) {
00320 opserr << "SamplingAnalysis::analyze() - could not " << endln
00321 << " set the u-vector for xu-transformation. " << endln;
00322 return -1;
00323 }
00324
00325
00326 result = theProbabilityTransformation->transform_u_to_x();
00327 if (result < 0) {
00328 opserr << "SamplingAnalysis::analyze() - could not " << endln
00329 << " transform u to x. " << endln;
00330 return -1;
00331 }
00332 x = theProbabilityTransformation->get_x();
00333
00334
00335 FEconvergence = true;
00336 result = theGFunEvaluator->runGFunAnalysis(x);
00337 if (result < 0) {
00338
00339
00340 FEconvergence = false;
00341 }
00342
00343
00344
00345 for (int lsf=0; lsf<numLsf; lsf++ ) {
00346
00347
00348
00349 theReliabilityDomain->setTagOfActiveLimitStateFunction(lsf+1);
00350
00351
00352
00353 result = theGFunEvaluator->evaluateG(x);
00354 if (result < 0) {
00355 opserr << "SamplingAnalysis::analyze() - could not " << endln
00356 << " tokenize limit-state function. " << endln;
00357 return -1;
00358 }
00359 gFunctionValue = theGFunEvaluator->getG();
00360 if (!FEconvergence) {
00361 gFunctionValue = -1.0;
00362 }
00363
00364
00365
00366
00367 if (analysisTypeTag == 1) {
00368
00369
00370 if (gFunctionValue < 0.0) {
00371 I = 1;
00372 failureHasOccured = true;
00373 }
00374 else {
00375 I = 0;
00376 }
00377
00378
00379
00380 phi = factor1 * exp( -0.5 * (u ^ u) );
00381 temp1 = inv_covariance ^ (u-startPointY);
00382 temp2 = temp1 ^ (u-startPointY);
00383 h = factor2 * exp( -0.5 * temp2 );
00384
00385
00386
00387 q = I * phi / h;
00388 sum_q(lsf) = sum_q(lsf) + q;
00389 sum_q_squared(lsf) = sum_q_squared(lsf) + q*q;
00390
00391
00392
00393 if (sum_q(lsf) > 0.0) {
00394
00395 q_bar(lsf) = 1.0/(double)k * sum_q(lsf);
00396 variance_of_q_bar(lsf) = 1.0/(double)k *
00397 ( 1.0/(double)k * sum_q_squared(lsf) - (sum_q(lsf)/(double)k)*(sum_q(lsf)/(double)k));
00398 if (variance_of_q_bar(lsf) < 0.0) {
00399 variance_of_q_bar(lsf) = 0.0;
00400 }
00401 cov_of_q_bar(lsf) = sqrt(variance_of_q_bar(lsf)) / q_bar(lsf);
00402 }
00403
00404 }
00405 else if (analysisTypeTag == 2) {
00406
00407
00408
00409 q = gFunctionValue;
00410 failureHasOccured = true;
00411
00412 sum_q(lsf) = sum_q(lsf) + q;
00413 sum_q_squared(lsf) = sum_q_squared(lsf) + q*q;
00414
00415 g_storage(lsf) = gFunctionValue;
00416
00417 if (sum_q(lsf) > 0.0) {
00418
00419
00420 q_bar(lsf) = 1.0/(double)k * sum_q(lsf);
00421 variance_of_q_bar(lsf) = 1.0/(double)k *
00422 ( 1.0/(double)k * sum_q_squared(lsf) - (sum_q(lsf)/(double)k)*(sum_q(lsf)/(double)k));
00423 if (variance_of_q_bar(lsf) < 0.0) {
00424 variance_of_q_bar(lsf) = 0.0;
00425 }
00426 cov_of_q_bar(lsf) = sqrt(variance_of_q_bar(lsf)) / q_bar(lsf);
00427
00428
00429 if (k>1) {
00430 responseVariance(lsf) = 1.0/((double)k-1) * ( sum_q_squared(lsf) - 1.0/((double)k) * sum_q(lsf) * sum_q(lsf) );
00431 }
00432 else {
00433 responseVariance(lsf) = 1.0;
00434 }
00435
00436 if (responseVariance(lsf) <= 0.0) {
00437 opserr << "ERROR: Response variance of limit-state function number "<< lsf << endln
00438 << " is zero! " << endln;
00439 }
00440 else {
00441 responseStdv(lsf) = sqrt(responseVariance(lsf));
00442 }
00443 }
00444 }
00445 else if (analysisTypeTag == 3) {
00446
00447 sprintf(myString,"%12.6e",gFunctionValue);
00448 resultsOutputFile << myString << " ";
00449 resultsOutputFile.flush();
00450 }
00451 else {
00452 opserr << "ERROR: Invalid analysis type tag found in sampling analysis." << endln;
00453 }
00454
00455
00456 if ( (printFlag == 1 || printFlag == 2) && analysisTypeTag!=3) {
00457 sprintf(string," GFun #%d, estimate:%15.10f, cov:%15.10f",lsf+1,q_bar(lsf),cov_of_q_bar(lsf));
00458 opserr << string << endln;
00459 }
00460 }
00461
00462
00463
00464
00465 if (analysisTypeTag == 3) {
00466 resultsOutputFile << endln;
00467 }
00468
00469
00470
00471 if (analysisTypeTag == 2) {
00472
00473 for (int i=0; i<numLsf; i++) {
00474 for (int j=i+1; j<numLsf; j++) {
00475
00476 crossSums(i,j) = crossSums(i,j) + g_storage(i) * g_storage(j);
00477
00478 denumerator = (sum_q_squared(i)-1.0/(double)k*sum_q(i)*sum_q(i))
00479 *(sum_q_squared(j)-1.0/(double)k*sum_q(j)*sum_q(j));
00480
00481 if (denumerator <= 0.0) {
00482 responseCorrelation(i,j) = 0.0;
00483 }
00484 else {
00485 responseCorrelation(i,j) = (crossSums(i,j)-1.0/(double)k*sum_q(i)*sum_q(j))
00486 /(sqrt(denumerator));
00487 }
00488 }
00489 }
00490 }
00491
00492
00493
00494 if (!failureHasOccured) {
00495 govCov = 999.0;
00496 }
00497 else {
00498 govCov = 0.0;
00499 for (int mmmm=0; mmmm<numLsf; mmmm++) {
00500 if (cov_of_q_bar(mmmm) > govCov) {
00501 govCov = cov_of_q_bar(mmmm);
00502 }
00503 }
00504 }
00505
00506
00507
00508
00509 if (govCov == 0.0) {
00510 govCov = 999.0;
00511 }
00512
00513
00514
00515 if (printFlag == 2) {
00516 ofstream outputFile( restartFileName, ios::out );
00517 outputFile << k << endln;
00518 outputFile << seed << endln;
00519 for (int lsf=0; lsf<numLsf; lsf++ ) {
00520 sprintf(string,"%15.10f %15.10f",q_bar(lsf),cov_of_q_bar(lsf));
00521 outputFile << string << " " << endln;
00522 }
00523 outputFile.close();
00524 }
00525
00526
00527 k++;
00528 isFirstSimulation = false;
00529
00530 }
00531
00532
00533 k--;
00534 opserr << endln;
00535
00536
00537 if (analysisTypeTag != 3) {
00538
00539 if (!failureHasOccured) {
00540 opserr << "WARNING: Failure did not occur for any of the limit-state functions. " << endln;
00541 }
00542
00543
00544 for (int lsf=1; lsf<=numLsf; lsf++ ) {
00545
00546 if ( q_bar(lsf-1) == 0.0 ) {
00547
00548 resultsOutputFile << "#######################################################################" << endln;
00549 resultsOutputFile << "# SAMPLING ANALYSIS RESULTS, LIMIT-STATEFUNCDTION NUMBER "
00550 <<setiosflags(ios::left)<<setprecision(1)<<setw(4)<<lsf <<" #" << endln;
00551 resultsOutputFile << "# #" << endln;
00552 resultsOutputFile << "# Failure did not occur, or zero response! #" << endln;
00553 resultsOutputFile << "# #" << endln;
00554 resultsOutputFile << "#######################################################################" << endln << endln << endln;
00555 }
00556 else {
00557
00558
00559 double beta_sim, pf_sim, cov_sim;
00560 int num_sim;
00561
00562
00563
00564 theReliabilityDomain->setTagOfActiveLimitStateFunction(lsf);
00565
00566
00567
00568 theLimitStateFunction = 0;
00569
00570 theLimitStateFunction = theReliabilityDomain->getLimitStateFunctionPtr(lsf);
00571 if (theLimitStateFunction == 0) {
00572 opserr << "SamplingAnalysis::analyze() - could not find" << endln
00573 << " limit-state function with tag #" << lsf << "." << endln;
00574 return -1;
00575 }
00576
00577
00578
00579 if (analysisTypeTag == 1) {
00580 beta_sim = -aStdNormRV->getInverseCDFvalue(q_bar(lsf-1));
00581 pf_sim = q_bar(lsf-1);
00582 cov_sim = cov_of_q_bar(lsf-1);
00583 num_sim = k;
00584 theLimitStateFunction->SimulationReliabilityIndexBeta = beta_sim;
00585 theLimitStateFunction->SimulationProbabilityOfFailure_pfsim = pf_sim;
00586 theLimitStateFunction->CoefficientOfVariationOfPfFromSimulation = cov_sim;
00587 theLimitStateFunction->NumberOfSimulations = num_sim;
00588 }
00589
00590
00591
00592 if (analysisTypeTag == 1) {
00593 resultsOutputFile << "#######################################################################" << endln;
00594 resultsOutputFile << "# SAMPLING ANALYSIS RESULTS, LIMIT-STATE FUNCTION NUMBER "
00595 <<setiosflags(ios::left)<<setprecision(1)<<setw(4)<<lsf <<" #" << endln;
00596 resultsOutputFile << "# #" << endln;
00597 resultsOutputFile << "# Reliability index beta: ............................ "
00598 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<beta_sim
00599 << " #" << endln;
00600 resultsOutputFile << "# Estimated probability of failure pf_sim: ........... "
00601 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<pf_sim
00602 << " #" << endln;
00603 resultsOutputFile << "# Number of simulations: ............................. "
00604 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<num_sim
00605 << " #" << endln;
00606 resultsOutputFile << "# Coefficient of variation (of pf): .................. "
00607 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<cov_sim
00608 << " #" << endln;
00609 resultsOutputFile << "# #" << endln;
00610 resultsOutputFile << "#######################################################################" << endln << endln << endln;
00611 }
00612 else {
00613 resultsOutputFile << "#######################################################################" << endln;
00614 resultsOutputFile << "# SAMPLING ANALYSIS RESULTS, LIMIT-STATE FUNCTION NUMBER "
00615 <<setiosflags(ios::left)<<setprecision(1)<<setw(4)<<lsf <<" #" << endln;
00616 resultsOutputFile << "# #" << endln;
00617 resultsOutputFile << "# Estimated mean: .................................... "
00618 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<q_bar(lsf-1)
00619 << " #" << endln;
00620 resultsOutputFile << "# Estimated standard deviation: ...................... "
00621 <<setiosflags(ios::left)<<setprecision(5)<<setw(12)<<responseStdv(lsf-1)
00622 << " #" << endln;
00623 resultsOutputFile << "# #" << endln;
00624 resultsOutputFile << "#######################################################################" << endln << endln << endln;
00625 }
00626 }
00627 }
00628
00629 if (analysisTypeTag == 2) {
00630 resultsOutputFile << "#######################################################################" << endln;
00631 resultsOutputFile << "# RESPONSE CORRELATION COEFFICIENTS #" << endln;
00632 resultsOutputFile << "# #" << endln;
00633 if (numLsf <=1) {
00634 resultsOutputFile << "# Only one limit-state function! #" << endln;
00635 }
00636 else {
00637 resultsOutputFile << "# gFun gFun Correlation #" << endln;
00638 resultsOutputFile.setf(ios::fixed, ios::floatfield);
00639 for (i=0; i<numLsf; i++) {
00640 for (int j=i+1; j<numLsf; j++) {
00641 resultsOutputFile << "# " <<setw(3)<<(i+1)<<" "<<setw(3)<<(j+1)<<" ";
00642 if (responseCorrelation(i,j)<0.0) { resultsOutputFile << "-"; }
00643 else { resultsOutputFile << " "; }
00644 resultsOutputFile <<setprecision(7)<<setw(11)<<fabs(responseCorrelation(i,j));
00645 resultsOutputFile << " #" << endln;
00646 }
00647 }
00648 }
00649 resultsOutputFile << "# #" << endln;
00650 resultsOutputFile << "#######################################################################" << endln << endln << endln;
00651 }
00652
00653 }
00654
00655
00656
00657
00658
00659
00660 opserr << "Simulation Analysis completed." << endln;
00661
00662
00663 resultsOutputFile.close();
00664 delete theMatrixOperations;
00665 delete aStdNormRV;
00666
00667 return 0;
00668 }
00669