void project_readInput() // // Input: none // Output: none // Purpose: retrieves project data from input file. // { // --- create hash tables for fast retrieval of objects by ID names createHashTables(); // --- count number of objects in input file and create them input_countObjects(); createObjects(); // --- read project data from input file input_readData(); if ( ErrorCode ) return; // --- establish starting & ending date/time StartDateTime = StartDate + StartTime; EndDateTime = EndDate + EndTime; ReportStart = ReportStartDate + ReportStartTime; ReportStart = MAX(ReportStart, StartDateTime); // --- check for valid starting & ending date/times if ( EndDateTime <= StartDateTime ) { report_writeErrorMsg(ERR_START_DATE, ""); } else if ( EndDateTime <= ReportStart ) { report_writeErrorMsg(ERR_REPORT_DATE, ""); } else { //// Following code segment was modified for release 5.1.009. //// //(5.1.009) //// // --- compute total duration of simulation in seconds TotalDuration = floor((EndDateTime - StartDateTime) * SECperDAY); // --- reporting step must be <= total duration if ( (double)ReportStep > TotalDuration ) { ReportStep = (int)(TotalDuration); } // --- reporting step can't be < routing step if ( (double)ReportStep < RouteStep ) { report_writeErrorMsg(ERR_REPORT_STEP, ""); } // --- convert total duration to milliseconds TotalDuration *= 1000.0; } //// }
static return_val initHashTables( void ) { int loop; return_val error; error = createHashTables(); if( error == OKAY ) { for( loop = 0; loop < NUM_ELTS( RecognizedName ); loop++ ) { HashTableInsert( NameRecognitionTable, (hash_value) RecognizedName[loop].name, RecognizedName[loop].type ); } } return( error ); }
void project_readInput() // // Input: none // Output: none // Purpose: retrieves project data from input file. // { // --- create hash tables for fast retrieval of objects by ID names createHashTables(); // --- count number of objects in input file and create them input_countObjects(); createObjects(); // --- read project data from input file input_readData(); if ( ErrorCode ) return; // --- establish starting & ending date/time StartDateTime = StartDate + StartTime; EndDateTime = EndDate + EndTime; ReportStart = ReportStartDate + ReportStartTime; ReportStart = MAX(ReportStart, StartDateTime); // --- check for valid starting & ending date/times if ( EndDateTime <= StartDateTime ) { report_writeErrorMsg(ERR_START_DATE, ""); } else if ( EndDateTime <= ReportStart ) { report_writeErrorMsg(ERR_REPORT_DATE, ""); } else { // --- compute total duration of simulation in milliseconds // (add on 1 msec to account for any roundoff) TotalDuration = (EndDateTime - StartDateTime) * MSECperDAY; TotalDuration += 1.0; // --- reporting step must be <= total duration if ( (double)ReportStep > TotalDuration/1000.0 ) { ReportStep = (int)(TotalDuration/1000.0); } if ( (float)ReportStep < RouteStep ) { report_writeErrorMsg(ERR_REPORT_STEP, ""); } } }
bool InitORL( void ) //****************** // Try and see if we will use ORL. Returns true if we'll use it. { orl_file_flags o_flags; orl_file_format o_format; orl_machine_type o_machine_type; ORLFileHnd = NULL; oFuncs.alloc = AllocMem; oFuncs.free = FreeMem; oFuncs.read = (void * (*) ( void *, int ))buffRead; oFuncs.seek = (long int (*) ( void *, long int, int ))buffSeek; ORLHnd = ORLInit( &oFuncs ); if( !ORLHnd ) { SysError( ERR_OUT_OF_MEM, false ); } initBuffer( &fileBuff, ObjFile ); o_format = ORLFileIdentify( ORLHnd, &fileBuff ); if( o_format != ORL_ELF && o_format != ORL_COFF ) { ORLFini( ORLHnd ); finiBuffer( &fileBuff ); ORLHnd = NULL; return( false ); // Will use ParseObjectOMF } ORLFileHnd = ORLFileInit( ORLHnd, &fileBuff, o_format ); if( !ORLFileHnd ) { ORLFini( ORLHnd ); finiBuffer( &fileBuff ); SysError( ERR_OUT_OF_MEM, false ); } o_machine_type = ORLFileGetMachineType( ORLFileHnd ); if( o_machine_type != ORL_MACHINE_TYPE_I386 ) { FiniORL(); Error( ERR_ORL_INV_MACHINE_TYPE, true ); exit( 1 ); } o_flags = ORLFileGetFlags( ORLFileHnd ); if( !(o_flags & ORL_FILE_FLAG_LITTLE_ENDIAN) ) { FiniORL(); Error( ERR_ORL_INV_BYTE_ORDER, true ); exit( 1 ); } UseORL = true; createHashTables(); return( true ); // Success: will use ORL }
static return_val initHashTables( void ) { int i; return_val error; hash_entry_data key_entry; error = createHashTables(); if( error == RC_OKAY ) { for( i = 0; i < NUM_ELTS( RecognizedName ); i++ ) { key_entry.key.u.string = RecognizedName[i].name; key_entry.data.u.sec_type = RecognizedName[i].type; HashTableInsert( NameRecognitionTable, &key_entry ); } } return( error ); }
extern "C" BOOL PASCAL EXPORT StartSimulation(char* strInputFilePath,char* strBestPopRun) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // normal function body here int nReturn = TRUE; CString str1, str2, strFilePath; str1 = strInputFilePath; str1.TrimLeft(); str1.TrimRight(); str2 = strBestPopRun; str2.TrimLeft(); str2.TrimRight(); // initialize progress bar CProgressWnd wndProgress(NULL, "SUSTAIN_MODEL", TRUE); if(str1.GetLength() < 2) return true; ////////////////////////////////////////////////////////////////////// //SWMM5 initialize conduit parameters initPointers(); setDefaults(); // --- create hash tables for fast retrieval of objects by ID names createHashTables(); ////////////////////////////////////////////////////////////////////// strFilePath = str1; CBMPData bmpData; CBMPRunner bmpRunner(&bmpData); if (!bmpData.ReadInputFile(strFilePath)) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } //optional weather data if (bmpData.nWeatherFile == 1) { if (!bmpData.ReadWeatherFile(bmpData.strInputDir + "weather.inp")) return FALSE; if (!bmpData.MarkWetIntervals(bmpData.startDate,bmpData.endDate)) return FALSE; } if (!bmpData.PrepareDataForModel()) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } // disabled for the first release (under testing) // run PreDeveloped scenario for bufferstrip simulation if necessary // if (!bmpData.RunVFSMOD(RUN_PREDEV)) // { // AfxMessageBox(bmpData.strError); // nReturn = FALSE; // goto L001; // } // run PostDeveloped scenario for bufferstrip simulation if necessary // if (!bmpData.RunVFSMOD(RUN_POSTDEV)) // { // AfxMessageBox(bmpData.strError); // nReturn = FALSE; // goto L001; // } if (!bmpData.OpenOutputFiles("Init"))// open file for time series { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } if (!bmpRunner.OpenOutputFiles("Init", bmpData.nRunOption, RUN_INIT))// open file for evaluation factor { nReturn = FALSE; goto L001; } bmpRunner.pWndProgress = &wndProgress; bmpRunner.RunModel(RUN_INIT); if (!bmpData.CloseOutputFiles()) // close file for time series { nReturn = FALSE; goto L001; } if (!bmpRunner.CloseOutputFiles()) // close file for evaluation factor { nReturn = FALSE; goto L001; } if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); goto L001; } if (!bmpData.OpenOutputFiles("PreDev")) // open file for time series { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } if (!bmpRunner.OpenOutputFiles("PreDev", bmpData.nRunOption, RUN_PREDEV)) // open file for evaluation factor { nReturn = FALSE; goto L001; } bmpRunner.RunModel(RUN_PREDEV); if (!bmpData.CloseOutputFiles()) // close file for time series { nReturn = FALSE; goto L001; } if (!bmpRunner.CloseOutputFiles()) // close file for evaluation factor { nReturn = FALSE; goto L001; } if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); goto L001; } if (!bmpData.OpenOutputFiles("PostDev"))// open file for time series { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } if (!bmpRunner.OpenOutputFiles("PostDev", bmpData.nRunOption, RUN_POSTDEV))// open file for evaluation factor { nReturn = FALSE; goto L001; } bmpRunner.RunModel(RUN_POSTDEV); if (!bmpData.CloseOutputFiles()) // close file for time series { nReturn = FALSE; goto L001; } if (!bmpRunner.CloseOutputFiles()) // close file for evaluation factor { nReturn = FALSE; goto L001; } if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); goto L001; } srand((unsigned)time(NULL)); // initialize seed for random number generator if (bmpData.nRunOption != OPTION_NO_OPTIMIZATION) { if (bmpData.nStrategy == STRATEGY_SCATTER_SEARCH) { // run option MinimizeCost or MaximizeControl CBMPOptimizer bmpOptimizer(&bmpRunner); if (bmpData.nAdjVariable < 5) { bmpOptimizer.problem.b1 = 5; bmpOptimizer.problem.b2 = 5; } else { bmpOptimizer.problem.b1 = bmpData.nAdjVariable; bmpOptimizer.problem.b2 = bmpData.nAdjVariable; } bmpOptimizer.problem.PSize = 3*(bmpOptimizer.problem.b1+bmpOptimizer.problem.b2); bmpOptimizer.problem.LS = FALSE; if (bmpRunner.lInitRunTime != 0) bmpOptimizer.nMaxRun = int((bmpData.lfMaxRunTime*3600000)/bmpRunner.lInitRunTime)+1; else bmpOptimizer.nMaxRun = 1000; if (bmpOptimizer.nMaxRun < 2*bmpOptimizer.problem.PSize) bmpOptimizer.nMaxRun = 2*bmpOptimizer.problem.PSize; bmpOptimizer.nMaxIter = int(bmpOptimizer.nMaxRun / (2*bmpOptimizer.problem.PSize)) + 1; bmpRunner.nMaxRun = bmpOptimizer.nMaxRun; // run optimization for Minimum Cost or MaximumControl options if (bmpData.nRunOption == OPTION_MIMIMIZE_COST || bmpData.nRunOption == OPTION_MAXIMIZE_CONTROL) { CString strOutPath = bmpData.strOutputDir + "\\AllSolutions.out"; bmpOptimizer.m_pAllSolutions = fopen(LPCSTR(strOutPath), "wt"); if (bmpOptimizer.m_pAllSolutions == NULL) { nReturn = FALSE; goto L001; } bmpOptimizer.OutputFileHeader("SS - All solutions", bmpOptimizer.m_pAllSolutions); bmpOptimizer.nRunCounter = 0; bmpOptimizer.InitProblem(bmpData.nAdjVariable, bmpOptimizer.problem.b1, bmpOptimizer.problem.b2, bmpOptimizer.problem.PSize, bmpOptimizer.problem.LS); bmpOptimizer.InitRefSet(); bmpOptimizer.PerformSearch(); fclose(bmpOptimizer.m_pAllSolutions); bmpOptimizer.OutputBestSolutions(); if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); goto L001; } else { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation completed successfully", MB_ICONINFORMATION); goto L001; } } // run optimization for TradeOff Curve options else if (bmpData.nRunOption == OPTION_TRADE_OFF_CURVE) { CString strOutPath = bmpData.strOutputDir + "\\AllSolutions.out"; bmpOptimizer.m_pAllSolutions = fopen(LPCSTR(strOutPath), "wt"); if (bmpOptimizer.m_pAllSolutions == NULL) { nReturn = FALSE; goto L001; } bmpOptimizer.OutputFileHeader("SS - All solutions", bmpOptimizer.m_pAllSolutions); // prepare output file for TradeOff Curve optimization strFilePath = bmpRunner.pBMPData->strOutputDir + "\\CECurve_Solutions.out"; FILE* fp = fopen(LPCSTR(strFilePath), "wt"); if(fp == NULL) { AfxMessageBox("Failed in creating output file "+strFilePath, MB_ICONEXCLAMATION); nReturn = FALSE; goto L001; } bmpOptimizer.OutputFileHeaderForTradeOffCurve(fp); // initialize problem only once bmpOptimizer.InitProblem(bmpData.nAdjVariable, bmpOptimizer.problem.b1, bmpOptimizer.problem.b2, bmpOptimizer.problem.PSize, bmpOptimizer.problem.LS); //update the max. runs bmpRunner.nMaxRun *= (bmpData.nTargetBreak + 1); // run optimization for the target range for (int i=0; i<=bmpData.nTargetBreak; i++) { POSITION pos, pos1; pos = bmpData.routeList.GetHeadPosition(); while (pos != NULL) { CBMPSite* pBMPSite = (CBMPSite*) bmpData.routeList.GetNext(pos); pos1 = pBMPSite->m_factorList.GetHeadPosition(); while (pos1 != NULL) { EVALUATION_FACTOR* pEF = (EVALUATION_FACTOR*) pBMPSite->m_factorList.GetNext(pos1); pEF->m_lfTarget = pEF->m_lfUpperTarget - i*(pEF->m_lfUpperTarget-pEF->m_lfLowerTarget)/bmpData.nTargetBreak; pEF->m_lfNextTarget = pEF->m_lfTarget - (pEF->m_lfUpperTarget-pEF->m_lfLowerTarget)/bmpData.nTargetBreak; } } bmpOptimizer.nRunCounter = 0; if (i == 0) bmpOptimizer.InitRefSet(); else bmpOptimizer.ResetRefSet(); bmpOptimizer.PerformSearch(); bmpOptimizer.OutputBestSolutionsForTradeOffCurve(i, fp); if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); fclose(bmpOptimizer.m_pAllSolutions); fclose(fp); goto L001; } } fclose(bmpOptimizer.m_pAllSolutions); fclose(fp); } } else if (bmpData.nStrategy == STRATEGY_GENETIC_ALGORITHM) { //run the best population scenarios (call from the post-processor spreadsheet) if(str2.GetLength() > 0) { CString strLine, strValue; POSITION pos, pos1; CBMPSite* pBMPSite; EVALUATION_FACTOR* pEF; ADJUSTABLE_PARAM* pAP; CStringToken strToken(str2); int nBestPops = 0; while (strToken.HasMoreTokens()) { str1 = strToken.NextToken(); nBestPops++; } bmpData.nSolution = nBestPops; // prepare output file for TradeOff Curve optimization strFilePath = bmpData.strOutputDir + "\\CECurve_Solutions.out"; FILE* fp = fopen(LPCSTR(strFilePath), "wt"); if(fp == NULL) { AfxMessageBox("Failed in creating output file "+strFilePath, MB_ICONEXCLAMATION); nReturn = FALSE; goto L001; } bmpData.OutputFileHeaderForTradeOffCurve(fp); CStringToken strToken1(str2); for (int i=0; i<nBestPops; i++) { //read best population file int nBestPopId = atoi((LPCSTR)strToken1.NextToken()); if (!bmpData.ReadBestPopFile(nBestPopId)) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } //output time series for the best pop solution strValue.Format("BestPop%d", nBestPopId); if (!bmpData.OpenOutputFiles(strValue)) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } //run model bmpRunner.RunModel(RUN_OUTPUT); //close time series for the best pop solution if (!bmpData.CloseOutputFiles()) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } //output CECurve_Solutions double totalCost = 0.0; double output1 = 0.0; pos = bmpData.routeList.GetHeadPosition(); while (pos != NULL) { pBMPSite = (CBMPSite*) bmpData.routeList.GetNext(pos); totalCost += pBMPSite->m_lfCost; } strLine.Format("%d\t%d", i+1, 1); strValue.Format("\t%lf", totalCost); strLine += strValue; //evaluation factors pos = bmpData.routeList.GetHeadPosition(); while (pos != NULL) { pBMPSite = (CBMPSite*) bmpData.routeList.GetNext(pos); pos1 = pBMPSite->m_factorList.GetHeadPosition(); while (pos1 != NULL) { pEF = (EVALUATION_FACTOR*) pBMPSite->m_factorList.GetNext(pos1); if (pEF->m_nCalcMode == CALC_PERCENT) // if the calculation mode is percentage { if (pEF->m_lfPostDev == 0.0) output1 = pEF->m_lfCurrent; else output1 = pEF->m_lfCurrent/pEF->m_lfPostDev*100; } else if (pEF->m_nCalcMode == CALC_VALUE) // if the calculation mode is value { output1 = pEF->m_lfCurrent; } else // if the calculation mode is scale { if (pEF->m_lfPostDev - pEF->m_lfPreDev > 0) output1 = (pEF->m_lfCurrent - pEF->m_lfPreDev) / (pEF->m_lfPostDev-pEF->m_lfPreDev); else output1 = pEF->m_lfCurrent; } strValue.Format("\t%lf", output1); strLine += strValue; } } //adjust parameters pos = bmpData.routeList.GetHeadPosition(); while (pos != NULL) { pBMPSite = (CBMPSite*) bmpData.routeList.GetNext(pos); pos1 = pBMPSite->m_adjustList.GetHeadPosition(); while (pos1 != NULL) { pAP = (ADJUSTABLE_PARAM*) pBMPSite->m_adjustList.GetNext(pos1); double* pVariable = pBMPSite->GetVariablePointer(pAP->m_strVariable); strValue.Format("\t%lf", *pVariable); strLine += strValue; } } strLine += "\n"; fputs(strLine, fp); if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); fclose(fp); nReturn = FALSE; goto L001; } } bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is completed", MB_ICONINFORMATION); fclose(fp); nReturn = TRUE; goto L001; } else { CBMPOptimizerGA bmpOptimizerGA(&bmpRunner); if (!bmpOptimizerGA.OpenOutputFiles()) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } if (!bmpOptimizerGA.LoadData()) { nReturn = FALSE; goto L001; } if (!bmpOptimizerGA.ValidateParams()) { AfxMessageBox(bmpData.strError); nReturn = FALSE; goto L001; } bmpOptimizerGA.nMaxRun = bmpOptimizerGA.problem.popsize * bmpOptimizerGA.problem.ngen; bmpRunner.nMaxRun = bmpOptimizerGA.nMaxRun; if (!bmpOptimizerGA.InitProblem()) { nReturn = FALSE; goto L001; } bmpOptimizerGA.PerformSearch(); bmpOptimizerGA.OutputBestPopulation(); bmpOptimizerGA.CloseOutputFiles(); if (bmpRunner.pWndProgress->Cancelled()) { bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is cancelled"); nReturn = FALSE; goto L001; } } } } bmpRunner.pWndProgress->DestroyWindow(); AfxMessageBox("BMP simulation is completed", MB_ICONINFORMATION); L001: //SWMM5 release memory deleteHashTables(); transect_delete(); for (int j=0; j<bmpData.nBMPC; j++) { FREE(Link[j].oldQual); FREE(Link[j].newQual); } FREE(Link); FREE(Conduit); FREE(GAInfil); return nReturn; }