void deleteObjects() // // Input: none // Output: none // Purpose: frees memory allocated for a project's objects. // // NOTE: care is taken to first free objects that are properties of another // object before the latter is freed (e.g., we must free a // subcatchment's land use factors before freeing the subcatchment). // { int j, k; // --- free memory for landuse factors & groundwater if ( Subcatch ) for (j = 0; j < Nobjects[SUBCATCH]; j++) { for (k = 0; k < Nobjects[LANDUSE]; k++) { FREE(Subcatch[j].landFactor[k].buildup); } FREE(Subcatch[j].landFactor); FREE(Subcatch[j].groundwater); gwater_deleteFlowExpression(j); FREE(Subcatch[j].snowpack); } // --- free memory for buildup/washoff functions if ( Landuse ) for (j = 0; j < Nobjects[LANDUSE]; j++) { FREE(Landuse[j].buildupFunc); FREE(Landuse[j].washoffFunc) } // --- free memory for water quality state variables if ( Subcatch ) for (j = 0; j < Nobjects[SUBCATCH]; j++) { FREE(Subcatch[j].initBuildup); FREE(Subcatch[j].oldQual); FREE(Subcatch[j].newQual); FREE(Subcatch[j].pondedQual); FREE(Subcatch[j].totalLoad); } if ( Node ) for (j = 0; j < Nobjects[NODE]; j++) { FREE(Node[j].oldQual); FREE(Node[j].newQual); } if ( Link ) for (j = 0; j < Nobjects[LINK]; j++) { FREE(Link[j].oldQual); FREE(Link[j].newQual); FREE(Link[j].totalLoad); } // --- free memory used for rainfall infiltration infil_delete(); //// Added for release 5.1.007. //// //(5.1.007) // --- free memory used for storage exfiltration if ( Node ) for (j = 0; j < Nnodes[STORAGE]; j++) { if ( Storage[j].exfil ) { FREE(Storage[j].exfil->btmExfil); FREE(Storage[j].exfil->bankExfil); FREE(Storage[j].exfil); } } // --- free memory used for nodal inflows & treatment functions if ( Node ) for (j = 0; j < Nobjects[NODE]; j++) { inflow_deleteExtInflows(j); inflow_deleteDwfInflows(j); rdii_deleteRdiiInflow(j); treatmnt_delete(j); } // --- delete table entries for curves and time series if ( Tseries ) for (j = 0; j < Nobjects[TSERIES]; j++) table_deleteEntries(&Tseries[j]); if ( Curve ) for (j = 0; j < Nobjects[CURVE]; j++) table_deleteEntries(&Curve[j]); // --- delete cross section transects transect_delete(); // --- delete control rules controls_delete(); // --- delete LIDs lid_delete(); // --- now free each major category of object FREE(Gage); FREE(Subcatch); FREE(Node); FREE(Outfall); FREE(Divider); FREE(Storage); FREE(Link); FREE(Conduit); FREE(Pump); FREE(Orifice); FREE(Weir); FREE(Outlet); FREE(Pollut); FREE(Landuse); FREE(Pattern); FREE(Curve); FREE(Tseries); FREE(Aquifer); FREE(UnitHyd); FREE(Snowmelt); FREE(Shape); }
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; }