Commit 0151749c authored by 刘乐's avatar 刘乐

1, 水力模型

parent 795d3bed
......@@ -23,10 +23,6 @@ bool CivHydrCalc::simulation(char* fileName, char* rptFile, char* binOutFile)
if (!exportInp(fileName))
return false;
// 等待
int index = 10000;
while (index-->0){}
short hydCode = 1;
long t(0), tstep(0);
int iTime(0);
......@@ -73,6 +69,9 @@ bool CivHydrCalc::simulation(char* fileName, char* rptFile, char* binOutFile)
iTime++;
// 获取当前节点和管段数据
getNodeResult(t);
getLinkResult(t);
} while (tstep > 0);
// 获取计算结果
......@@ -101,9 +100,7 @@ bool CivHydrCalc::exportInp(char* fileName)
assemble(table,civInp);
}
mDbConn->close(); // 主动关闭连接
/*
UNITS LPS
......@@ -233,6 +230,42 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
else if (RESIVOIR == table) // 水源
{
//;ID Head Pattern
Civitems coordItems;
Civitems resirvoirItems;
ResivoirFields fields;
int total = comps.size();
for (int i = 0; i < total; i++)
{
Componet comp = comps[i];
Str id = comp.find(fields.ID)->second;
Str elevation = comp.find(fields.Elev)->second;
Str flow = comp.find(fields.Flow)->second;
Str head = comp.find(fields.Head)->second;
Str pattern = comp.find(fields.Pattern)->second;
Str x_coord = comp.find(fields.X_Coord)->second;
Str y_coord = comp.find(fields.Y_Coord)->second;
// 注意顺序
//1, coord
CivItem civItem;
civItem.push_back(id);
civItem.push_back(head);
civItem.push_back(pattern);
resirvoirItems.push_back(civItem);
// 2, 节点坐标
CivItem nodeCoordItem;
nodeCoordItem.push_back(id);
nodeCoordItem.push_back(x_coord);
nodeCoordItem.push_back(y_coord);
coordItems.push_back(nodeCoordItem);
}
inp.writeComponet(coordItems, _COORDS);
inp.writeComponet(resirvoirItems, _RESERVOIRS);
}
else if (PUMP == table) // 水泵
......@@ -256,8 +289,9 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
Str minLevel = comp.find(tkfFileds.MinLevel)->second;
Str maxLevel = comp.find(tkfFileds.MaxLevel)->second;
Str diametor = comp.find(tkfFileds.Diameter)->second;
Str minVol = comp.find(tkfFileds.MinVol)->second;
Str volCurve = comp.find(tkfFileds.VolCurve)->second;
//Str volCurve = comp.find(tkfFileds.VolCurve)->second;
Str x_coord = comp.find(tkfFileds.X_Coord)->second;
Str y_coord = comp.find(tkfFileds.Y_Coord)->second;
......@@ -270,7 +304,8 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
tankNode.push_back(minLevel);
tankNode.push_back(maxLevel);
tankNode.push_back(diametor);
tankNode.push_back(volCurve);
tankNode.push_back(minVol);
// tankNode.push_back(volCurve);
tankItems.push_back(tankNode);
......@@ -281,8 +316,9 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
nodeCoordItem.push_back(y_coord);
nodeCoordItems.push_back(nodeCoordItem);
}
inp.writeComponet(nodeCoordItems, _COORDS);
inp.writeComponet(tankItems, _TANKS);
}
......@@ -378,15 +414,16 @@ void CivHydrCalc::getLinkResult(short time)
bool DLLEXPORT dynamicCompute(std::string uri, std::string netName, std::string flag)
{
char* finalUri = const_cast<char*>(uri.c_str());
CivHydrCalc* calc = new CivHydrCalc(finalUri, netName);
CivHydrCalc calc(finalUri, netName);
char fileName[20];
char rptR[20];
char outBinFile[20];
const char* file = "test.inp";
strcpy_s(fileName, "test.inp");
strcpy_s(rptR, "test.rpt");
strcpy_s(outBinFile, "test.bin");
return calc->simulation(fileName, rptR, outBinFile);
return calc.simulation(fileName, rptR, outBinFile);
}
\ No newline at end of file
#include "CivInp.h"
#include "CivCommonUtils.h"
#include<time.h>
CivInp::CivInp(TimeUnit timetype):mTimeUnit(timetype)
......@@ -14,9 +15,11 @@ bool CivInp::writeToFile(const char* fileName)
if (file = fopen(fileName, "wb"))
{
fprintf(file, mTextStream.str().c_str());
mTextStream.clear();
fclose(file);
return true;
}
mTextStream.clear();
return false;
}
......@@ -246,24 +249,11 @@ void CivInp::writeTags(Civitems items)
writeString("");
}
std::string CivInp::currentTime()
{
time_t timep;
struct tm* p;
time(&timep);
p = gmtime(&timep);
std::string currTime = std::to_string(1900 + p->tm_year) + "/" + std::to_string(1 + p->tm_mon) + \
"/" + std::to_string(p->tm_mday) + " " + std::to_string(p->tm_hour) + ":" + std::to_string(p->tm_min) + ":" + \
std::to_string(p->tm_sec);
return currTime;
}
void CivInp::writeTitles()
{
writeString("[TITLE]"); // дǩ
writeString("Scenario: Base");
writeString("Date:"+currentTime());
writeString("Date:"+CivCommonUtils::currentTime());
writeString("");
writeString("");
}
......
......@@ -152,8 +152,6 @@ private:
void writeOptions(const Civitems& items);
void writePatterns(Civitems items);
void writeDemand(Civitems items);
std::string currentTime();
private:
std::ostringstream mTextStream;
......
......@@ -5,7 +5,7 @@
int main(int argc, char* argv[])
{
const char* uri = "host=192.168.12.7 port=5432 dbname=liuletext user=postgres password=admin";
const char* uri = "host=192.168.12.7 port=5432 dbname=2222 user=postgres password=admin";
char* findUri = const_cast<char*>(uri);
CivHydrCalc* calc = new CivHydrCalc(findUri, std::string("ˮģ"));
char fileName[20];
......@@ -16,8 +16,8 @@ int main(int argc, char* argv[])
strcpy_s(rptR, "test.rpt");
strcpy_s(outBinFile, "test.bin");
//calc->exportInp(fileName);
calc->simulation(fileName, rptR, outBinFile);
// calc->exportInp(fileName);
calc->simulation(fileName, rptR, outBinFile);
delete calc;
return 0;
}
\ No newline at end of file
......@@ -45,4 +45,10 @@ std::string CivCommonUtils::UTF8_To_string(const std::string& str)
pwBuf = NULL;
return strRet;
}
std::string CivCommonUtils::currentTime()
{
return std::string();
}
\ No newline at end of file
......@@ -17,4 +17,9 @@ public:
* 再通过WideCharToMultiByte将宽字符转换为多字节。
*/
static std::string UTF8_To_string(const std::string& str);
/**
*@brief 获取系统当前时间
*/
static std::string currentTime();
};
......@@ -5,7 +5,7 @@
CivDbConnection::CivDbConnection(char* uri)
: mConn(NULL), mUri(uri)
{
}
CivDbConnection::~CivDbConnection()
......@@ -39,6 +39,7 @@ void CivDbConnection::close()
Tables CivDbConnection::getTables(StrQuote netName,StrQuote schema) const
{
if (!isValid())
return Tables();
......@@ -49,8 +50,6 @@ Tables CivDbConnection::getTables(StrQuote netName,StrQuote schema) const
if (result == NULL)
return Tables();
char* error = PQresultErrorMessage(result);
Tables tables;
int rows = PQntuples(result);
int columns = PQnfields(result);
......@@ -70,12 +69,116 @@ Tables CivDbConnection::getTables(StrQuote netName,StrQuote schema) const
bool CivDbConnection::updateNode(const NodeResultItems& nodeItems)
{
if (!isValid())
return false;
// 2, 参数值
int total = nodeItems.size();
for (int i = 0; i < total; i++)
{
NodeResultItem item = nodeItems[i];
char sql[512] = "update public.";
strcat_s(sql, "节点");
strcat_s(sql, " as tb1 set(");
strcat_s(sql, "总水头");
strcat_s(sql, ",压力");
strcat_s(sql, ",实际需水量");
strcat_s(sql, ")");
strcat_s(sql, "=(");
// 拼接值
strcat_s(sql, std::to_string(item.dHead).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dPressure).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dDemand).c_str());
strcat_s(sql, ")");
strcat_s(sql, "where tb1.\"");
strcat_s(sql, "本点号");
strcat_s(sql, "\"='");
strcat_s(sql, item.szNo);
strcat_s(sql, "'");
std::string sqlStr(sql, sql + strlen(sql));
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr);
PGresult* result = PQexec(mConn, utf8Str.c_str());
PQstatus(mConn);
if (PQstatus(mConn) != PGRES_COMMAND_OK) // or conn==NULL
{
std::cout << PQerrorMessage(mConn) << std::endl;
std::cout << PQresultErrorMessage(result) << std::endl;
continue;
}
std::cout << PQerrorMessage(mConn) << std::endl;
std::cout << PQresultErrorMessage(result) << std::endl;
PQclear(result);
}
return true;
}
bool CivDbConnection::updateLink(const LinkResultItems& lineItems)
{
if (!isValid())
return false;
// 2, 参数值
int total = lineItems.size();
for (int i = 0; i < total; i++)
{
LinkResultItem item = lineItems[i];
char sql[512] = "update public.";
strcat_s(sql,"管段");
strcat_s(sql, " as tb1 set(");
strcat_s(sql, "流量");
strcat_s(sql,",流速");
strcat_s(sql, ",单位水头损");
strcat_s(sql, ",水头总损失");
strcat_s(sql, ",摩擦水头损");
strcat_s(sql, ",上点水头");
strcat_s(sql, ",本点水头");
strcat_s(sql, ",局部水头损");
strcat_s(sql, ")");
strcat_s(sql, "=(");
// 拼接值
strcat_s(sql, std::to_string(item.dFlow).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dVelocity).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dUnitHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dFrictionHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dFromNodHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dToNodHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dLocalHeadloss).c_str());
strcat_s(sql, ")where tb1.\"");
strcat_s(sql, "编号");
strcat_s(sql, "\"='");
strcat_s(sql, item.szNo);
strcat_s(sql, "'");
std::string sqlStr(sql, sql + strlen(sql));
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr);
PGresult* result = PQexec(mConn, utf8Str.c_str());
PQstatus(mConn);
if (PQstatus(mConn) != PGRES_COMMAND_OK ) // or conn==NULL
{
std::cout << PQerrorMessage(mConn) << std::endl;
std::cout << PQresultErrorMessage(result) << std::endl;
continue;
}
std::cout << PQerrorMessage(mConn) << std::endl;
std::cout << PQresultErrorMessage(result) << std::endl;
PQclear(result);
}
return true;
}
......
......@@ -55,6 +55,6 @@ private:
bool mIsOpen = false;
char* mUri;
int mNode; // 节点个数
int mPipes; // 管线个数
char mLinkParam[256];
char mNodeParam[256];
};
\ No newline at end of file
......@@ -9,7 +9,7 @@ typedef const std::string& StrQuote;
#define PIPELINE Str("管段")
#define PIPENODE Str("节点")
#define RESIVOIR Str("水")
#define RESIVOIR Str("水")
#define TANK Str("水池")
......@@ -58,6 +58,17 @@ struct Report {
// pump
struct PumpFields {
};
// 水库字段
struct ResivoirFields {
Str ID = "本点号";
Str Elev = "高程";
Str Head = "水头";
Str X_Coord = "横坐标";
Str Y_Coord = "纵坐标";
Str Pattern = "水头模式曲";
Str Flow = "输出流量";
};
//;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
......@@ -95,7 +106,7 @@ struct TanksFields {
Str MaxLevel = "最高水位";
Str Diameter = "直径";
Str MinVol = "容量";
Str VolCurve = "容积取线ID";
Str VolCurve = "容积";
Str X_Coord = "横坐标";
Str Y_Coord = "纵坐标";
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment