目的:
swmm读取.inp文件后,会进行一定计算(如计算conduit.slope)并将信息存储在各个指针变量中,如:TConduit* Conduit等。通过在python中调用该指针,能更快捷的编程。
思路步骤:
- 根据swmm5.c,编写Swmm5Extend.c;
- 删去读取文件后的模拟函数;
- 重写便于python使用的swmm5_extend_run();
- 编写将object数量存储在全局变量中的函数count_object();
- 将函数与全局变量都放在Swmm5Extend.h中;
- 编写swig输入文件swmm.i
- 编写swig编译文件setup.py
- 在powershell中运行
具体代码:
Swmm5Extend.c
1 #include <stdlib.h> 2 #include <string.h> 3 #include <math.h> 4 #include <time.h> 5 #include <float.h> 6 #include "swmm5\headers.h"//include #include <stdio.h> 7 #include "swmm5\swmm5.h" 8 #include "Swmm5Extend.h" 9 10 //#define _CRT_SECURE_NO_DEPRECATE 11 #define MAX_EXCEPTIONS 100 // max. number of exceptions handled 12 13 14 //----------------------------------------------------------------------------- 15 // Unit conversion factors 16 //----------------------------------------------------------------------------- 17 /* 18 const double Ucf[10][2] = 19 {// US SI 20 {43200.0, 1097280.0 }, // RAINFALL (in/hr, mm/hr --> ft/sec) 21 {12.0, 304.8 }, // RAINDEPTH (in, mm --> ft) 22 {1036800.0, 26334720.0}, // EVAPRATE (in/day, mm/day --> ft/sec) 23 {1.0, 0.3048 }, // LENGTH (ft, m --> ft) 24 {2.2956e-5, 0.92903e-5}, // LANDAREA (ac, ha --> ft2) 25 {1.0, 0.02832 }, // VOLUME (ft3, m3 --> ft3) 26 {1.0, 1.608 }, // WINDSPEED (mph, km/hr --> mph) 27 {1.0, 1.8 }, // TEMPERATURE (deg F, deg C --> deg F) 28 {2.203e-6, 1.0e-6 }, // MASS (lb, kg --> mg) 29 {43560.0, 3048.0 } // GWFLOW (cfs/ac, cms/ha --> ft/sec) 30 }; 31 const double Qcf[6] = // Flow Conversion Factors: 32 { 1.0, 448.831, 0.64632, // cfs, gpm, mgd --> cfs 33 0.02832, 28.317, 2.4466 }; // cms, lps, mld --> cfs 34 */ 35 //----------------------------------------------------------------------------- 36 // Shared variables 37 //----------------------------------------------------------------------------- 38 static int IsOpenFlag; // TRUE if a project has been opened 39 static int IsStartedFlag; // TRUE if a simulation has been started 40 static int SaveResultsFlag; // TRUE if output to be saved to binary file 41 static int ExceptionCount; // number of exceptions handled 42 static int DoRunoff; // TRUE if runoff is computed 43 static int DoRouting; // TRUE if flow routing is computed 44 45 46 int main(){ 47 char *inputFile= "iofiles\\dalizi_rain1.inp";//Kimi: can edit by hand 48 char *reportFile= "iofiles\\swmm5_extend.rpt"; 49 char *binaryFile; 50 char blank[] = ""; 51 time_t start; 52 double runTime; 53 54 // --- initialize flags 55 //Kimi: put it in swmm5_extend_run() 56 57 // --- check for proper number of command line arguments 58 start = time(0); 59 if (0) ; 60 else 61 { 62 binaryFile = blank; 63 writecon(FMT02); 64 65 // --- run SWMM 66 swmm5_extend_run(inputFile, reportFile, binaryFile); 67 68 // Display closing status on console 69 runTime = difftime(time(0), start); 70 //sprintf(Msg, "\n\n... EPA-SWMM completed in %.2f seconds.", runTime); 71 writecon(Msg); 72 if (ErrorCode) writecon(FMT03); 73 else if (Warnings) writecon(FMT04); //(5.1.011) 74 else writecon(FMT05); 75 } 76 77 //Kimi: close and free memory 78 swmm_close(); 79 //Kimi: finish 80 printf("Press ENTER key to Continue\n"); 81 getchar(); 82 return 0; 83 } 84 85 //functions 86 void swmm5_extend_run(char* f1, char* f2, char* f3) { 87 IsOpenFlag = FALSE; 88 IsStartedFlag = FALSE; 89 SaveResultsFlag = TRUE; 90 /* 91 long newHour, oldHour = 0; 92 long theDay, theHour; 93 double elapsedTime = 0.0; //(5.1.011) 94 */ 95 // --- open the files & read input data 96 ErrorCode = 0; 97 swmm_open(f1, f2, f3); 98 99 // --- run the simulation if input data OK 100 /* 101 if (!ErrorCode) 102 { 103 // --- initialize values 104 swmm_start(TRUE); 105 106 // --- execute each time step until elapsed time is re-set to 0 107 if (!ErrorCode) 108 { 109 writecon("\n o Simulating day: 0 hour: 0"); 110 do 111 { 112 swmm_step(&elapsedTime); 113 newHour = (long)(elapsedTime * 24.0); 114 if (newHour > oldHour) 115 { 116 theDay = (long)elapsedTime; 117 theHour = (long)((elapsedTime - floor(elapsedTime)) * 24.0); 118 writecon("\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); 119 sprintf(Msg, "%-5d hour: %-2d", theDay, theHour); 120 writecon(Msg); 121 oldHour = newHour; 122 } 123 } while (elapsedTime > 0.0 && !ErrorCode); 124 writecon("\b\b\b\b\b\b\b\b\b\b\b\b\b\b" 125 "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); 126 writecon("Simulation complete "); 127 } 128 129 // --- clean up 130 swmm_end(); 131 } 132 133 // --- report results 134 if (Fout.mode == SCRATCH_FILE) swmm_report(); 135 */ 136 // --- close the system 137 138 //conduit_slope=ConduitSlope(Conduit); 139 140 //swmm_close(); //Kimi: move to main() 141 142 143 //----------------------------------------------------------------------------- 144 //Kimi: utilize the information we needed in here 145 count_object(); 146 //----------------------------------------------------------------------------- 147 148 return error_getCode(ErrorCode); 149 } 150 151 void* count_object() { 152 NumConduit = Nlinks[CONDUIT];//length of array 153 //NumLink= 154 }
Swmm5Extend.h
1 #ifndef SWMM5EXTEND_H 2 #define SWMM5EXTEND_H 3 4 //functions 5 void swmm5_extend_run(char* f1, char* f2, char* f3); 6 void* count_object(); 7 8 //global varibles 9 int NumConduit; 10 int NumLink;//useless now 11 #endif // !SWMM5EXTEND_H
swmm.i(精华所在)
1 %module SWMM5ReadInpFile 2 3 //Kimi: to covert the pointer to the array type automatically 4 %pythonappend Conduit %{ 5 # Wrap it automatically 6 newval = TConduitArray.frompointer(val) 7 newval.ptr_retain = val 8 val = newval 9 %} 10 11 %{ 12 #define SWIG_FILE_WITH_INIT 13 #include "swmm5/consts.h" // defined constants 14 #include "swmm5/macros.h" // macros used throughout SWMM 15 #include "swmm5/enums.h" // enumerated variables 16 #include "swmm5/error.h" // error message codes 17 #include "swmm5/datetime.h" // date/time functions 18 #include "swmm5/objects.h" // definitions of SWMM‘s data objects 19 #include "swmm5/funcs.h" // declaration of all global functions 20 #include "swmm5/text.h" // listing of all text strings 21 #define EXTERN // defined as ‘extern‘ in headers.h 22 #include "swmm5/globals.h" // declaration of all global variables 23 #include "swmm5/swmm5.h" // declaration of exportable functions 24 #include "Swmm5Extend.h" 25 #define MAX_EXCEPTIONS 100 // max. number of exceptions handled 26 %} 27 28 //directives 29 %include "Swmm5Extend.h" 30 //global variables 31 #define EXTERN 32 %include "swmm5/objects.h" 33 %include "swmm5/globals.h" 34 35 //Kimi: to convert TConduit type pointer to list in python 36 %include <carrays.i> 37 %array_class(TConduit, TConduitArray); 38 //"TConduit" is equal to "struct foo"
其他过程略去,见其他随笔。
1 #用python调用swmm数据 2 3 import SWMM5ReadInpFile as swmm 4 5 inputFile=‘iofiles\\dalizi_rain1.inp‘ 6 reportFile= "iofiles\\swmm5_extend.rpt" 7 binaryFile=‘‘ 8 9 #run swmm to get input summary 10 swmm.swmm5_extend_run(inputFile, reportFile, binaryFile); 11 12 pointer=swmm.cvar.Conduit 13 conduit=swmm.TConduitArray.frompointer(pointer) 14 ConduitLen=swmm.cvar.NumConduit 15 print(ConduitLen) 16 for i in range(0,ConduitLen): 17 print(conduit[i].slope)
运行成功:
重要参考链接:
https://stackoverflow.com/questions/11998369/swig-python-structure-array
原文地址:https://www.cnblogs.com/kimilin/p/10362198.html
时间: 2024-10-09 10:40:57