[swmm]调用存储数据的“结构指针数组”

目的:

  swmm读取.inp文件后,会进行一定计算(如计算conduit.slope)并将信息存储在各个指针变量中,如:TConduit* Conduit等。通过在python中调用该指针,能更快捷的编程。

思路步骤:

  1. 根据swmm5.c,编写Swmm5Extend.c;

    1. 删去读取文件后的模拟函数;
    2. 重写便于python使用的swmm5_extend_run();
    3. 编写将object数量存储在全局变量中的函数count_object();
  2. 将函数与全局变量都放在Swmm5Extend.h中;
  3. 编写swig输入文件swmm.i
  4. 编写swig编译文件setup.py
  5. 在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

[swmm]调用存储数据的“结构指针数组”的相关文章

绝对好文C#调用C++DLL传递结构体数组的终极解决方案

C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/article/details/11781003 在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型可以对应.这时怎么办,第一反应是C#也定义结构体,然后当

hashSet和List集合存储数据的结构

List集合存储数据的结构 堆栈:先进后出 例如担架 先进去的 后出来 --------------------------------------------------------------------------------------------------------- 队列:先进的先出 后进的后出 ---------------------------------------------------------------------------------------------

C# 调用C++DLL 传结构体数组

C# 调用C++DLL 传结构体数组,注意C#和C++数据类型占用字节数要对应.否则传进去的数组会错位.C++ BOOL 对应C#bool. 1.c++代码 //MyDLL.h #ifndef MYDLL_H_ #define MYDLL_H_ #include <iostream> #include <windows.h> #ifdef EXTERN_EXPORT #define EXTERN_EXPORT extern "C" _declspec(dllim

二维数据和一维指针数组

1.二维数组名a的含义: 定义一个二维数组a : int a[3][4] = {{1,3,5,7}, {9,11,13,15}, {17,19,21,23}}; a数组包含3个行元素,a[0],a[1],a[2]. 每个行元素a[i] 又是一个一维数组,它包含4个元素. a == &a[0] a + i == &a[i] a[0] == &a[0][0] a[i] == &a[ i ][0] 2.二维数组地址的关系:(地址由行至列以元素类型递增) 3.二维数组与一维指针数组

HashSet集合存储数据的结构

原文地址:https://www.cnblogs.com/roadlandscape/p/12091524.html

C语言基础学习8:指针数组

一个数组,若其元素均为指针类型数据,称为指针数组,指针数组中的每一个元素都相当于一个指针变量. 一维指针数组的定义形式为: 类型名 *数组名[数组长度]: 例如:int *p[4]; []的优先级比*高,因此p先与[4]结合,形成p[4]形式,这是数组形式,然后再与p前面的"*"结合,"*"表示此数组是指针类型的,每个数组元素都可指向一个整型变量. 为什么要用到指针数组呢? 因为它比较适合于用来指向若干个字符串,使得字符串处理更加灵活. 1 #include <

数据存储的常用结构 堆栈、队列、数组、链表

数据存储的常用结构有:堆栈.队列.数组.链表.我们分别来了解一下: 堆栈,采用该结构的集合,对元素的存取有如下的特点: 先进后出(即,存进去的元素,要在后它后面的元素依次取出后,才能取出该元素).例如,子弹压进弹夹,先压进去的子弹在下面,后压进去的子弹在上面,当开枪时,先弹出上面的子弹,然后才能弹出下面的子弹. 栈的入口.出口的都是栈的顶端位置 压栈:就是存元素.即,把元素存储到栈的顶端位置,栈中已有元素依次向栈底方向移动一个位置. 弹栈:就是取元素.即,把栈的顶端位置元素取出,栈中已有元素依次

71. C++ 分别用指针数组和二维数组生成二维空间,存储数据并释放。 练习new/delete, new[]/delete[]

分别用指针数组和二维数组生成二维空间,存储数据并释放.比如,数据如下:  //使用了下fgetc() 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 5 6 int main() 7 { 8 #if 0 //生成二维数组存储 9 FILE* fp = fopen("G:\\qtcode\\temp.txt","r"); 10 11 //char buf[3

用结构体指针存储数据__正序_逆序下的输入

逆序输入 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <malloc.h> 4 #define maxn 1000 5 6 //邻接表(点很多,边较少) 7 //共有n个点,点编号为1~n,m条边 8 //无向图 9 10 struct node 11 { 12 long value; 13 struct node *next; 14 }*d[maxn+1]; 15 16 int main() 17 { 1