cimforreduceloss/testHasttable/cimexporter.cpp

320 lines
9.7 KiB
C++
Raw Normal View History

#include "cimexporter.h"
#include <iostream>
CIMExporter::CIMExporter(const QHash<QString, BasicElementInfo *> &eleHT, const QString &loadDir):loadDir(loadDir),eleHT(eleHT)
{
}
void CIMExporter::add(const QPair<QString,QString>& fromTo,Line* line)
{
LineStru lineStru;
lineStru.line=line;
lineStru.fromID=fromTo.first;
lineStru.toID=fromTo.second;
lineStru.id=line->ac->getID();
lineStru.isZeroBranch=std::fabs(line->length)<1e-5;
lineStru.dispose=false;
lineStru.type="AC";
lineStru.length=line->length;
this->line.push_back(lineStru);
}
void CIMExporter::add(const QPair<QString,QString>& fromTo,Switch* sw)
{
SwitchStru switchStru;
switchStru.sw=sw;
switchStru.fromID=fromTo.first;
switchStru.toID=fromTo.second;
switchStru.id=sw->id;
switchStru.isZeroBranch=true;
switchStru.dispose=false;
switchStru.type="SW";
this->sw.push_back(switchStru);
}
void CIMExporter::add(const QPair<QString,QString>& fromTo,Transformer* tf)
{
TransformerStru tfStru;
tfStru.tf=tf;
tfStru.fromID=fromTo.first;
tfStru.toID=fromTo.second;
tfStru.isZeroBranch=false;
tfStru.dispose=false;
tfStru.id=tf->getTF()->getID();
tfStru.type="TF";
this->tf.push_back(tfStru);
}
void CIMExporter::add(const QPair<QString,QString>& fromTo,DG* dg)
{
DGStru dgStru;
dgStru.dg=dg;
dgStru.fromID=fromTo.first;
dgStru.toID=fromTo.second;
dgStru.isZeroBranch=false;
dgStru.dispose=false;
dgStru.id=dg->id;
dgStru.type="DG";
this->dg.push_back(dgStru);
}
void CIMExporter::exportTo(const QString& path,const QString &rootID)
{
//输出到文件
QFile fd(path);
if(fd.open(QFile::WriteOnly))
{
//消减元件
QList<BranchStruc*> elements;
for(int i=0;i<this->line.length();i++)
{
// std::cout<<this->line[i].fromID.toStdString()<<" "<<this->line[i].id.toStdString()<<" "<<this->line[i].toID.toStdString()<<std::endl;
elements.push_back( &(this->line[i]) );
}
for(int i=0;i<this->sw.length();i++)
{
// std::cout<<this->sw[i].fromID.toStdString()<<" "<<this->sw[i].id.toStdString()<<" "<<this->sw[i].toID.toStdString()<<std::endl;
elements.push_back( &(this->sw[i]) );
}
for(int i=0;i<this->tf.length();i++)
{
// std::cout<<this->tf[i].fromID.toStdString()<<" "<<this->tf[i].id.toStdString()<<" "<<this->tf[i].toID.toStdString()<<std::endl;
elements.push_back( &(this->tf[i]) );
}
for(int i=0;i<this->dg.length();i++)
{
// std::cout<<this->dg[i].fromID.toStdString()<<" "<<this->dg[i].id.toStdString()<<" "<<this->dg[i].toID.toStdString()<<std::endl;
elements.push_back( &(this->dg[i]) );
}
ElementReduction elementReduction(elements);
elementReduction.doIt(rootID);//消除0阻抗支路把几段线路连成一段
//先给所有节点都编号
this->idToNumber(this->line);
this->idToNumber(this->sw);
this->idToNumber(this->tf);
this->idToNumber(this->dg);
//开始按要求输出
//先输出线路
QTextStream writer(&fd);
writer<<QStringLiteral("线路")<<QStringLiteral("\r\n");
writer<<QStringLiteral("节点号")<<QStringLiteral("\t")<<QStringLiteral("节点号")<<QStringLiteral("\t")<<QStringLiteral("型号")<<QStringLiteral("\t")<<QStringLiteral("长度(m)")<<QStringLiteral("\r\n");
for(QList<LineStru>::iterator ite=this->line.begin();
ite!=this->line.end();
ite++)
{
LineStru l=*ite;
if(l.dispose)
{
continue;
}
// writer<<elements.first()->length<<"\t"<<elements.first()->id<<"\t";
writer<<l.fromNum<<"\t";
writer<<l.toNum<<"\t";
writer<<"type"<<"\t";
// writer<<l.id<<"\t";
writer<<l.length<<"\r\n";
}
//输出刀闸
for(QList<SwitchStru>::iterator ite=this->sw.begin();
ite!=this->sw.end();
ite++)
{
if(ite->dispose)
{
continue;
}
writer<<ite->fromNum<<"\t";
writer<<ite->toNum<<"\t";
writer<<"type"<<"\t";
// writer<<ite->id<<"\t";
writer<<0<<"\r\n";
}
//输出负荷
RecurseDir recurseDir;
recurseDir.setDir(this->loadDir);
QStringList files;
files=recurseDir.getFiles();
//只取文件名
QStringList baseNames;
foreach(QString file,files)
{
QFileInfo fileInfo(file);
if(fileInfo.suffix().toLower()!="csv")
{
continue;
}
baseNames<<fileInfo.baseName();
// std::cout<<"has "<<fileInfo.baseName().toLocal8Bit().data()<<std::endl;
}
writer<<QStringLiteral("变压器")<<"\r\n";
writer<<QStringLiteral("节点号")<<"\t"<<QStringLiteral("节点号")<<"\t"<<QStringLiteral("型号")<<"\t"<<QStringLiteral("容量(MVA)")<<"\r\n";
QStringList usedLoad;
for(QList<TransformerStru>::iterator ite=this->tf.begin();
ite!=this->tf.end();
ite++)
{
if(ite->dispose)
{
continue;
}
PowerTransformer *tf=ite->tf->getTF();
QString subID=tf->getEquipmentMemberOf_EquipmentContainer();//所属Substation的ID
Substation *sub=static_cast<Substation *>(this->eleHT[subID]);
if(!sub)
{
std::cout<<"CIMExporter:: can not substation of "<<subID.toStdString()<<std::endl;
continue;
}
// std::cout<<"sub id"<< subID.toStdString()<<" "<<sub->getID().toStdString()<<std::endl;
LoadMapping loadMapping;
QVector<QSharedPointer<LoadInfo> > vecLoadInfo=loadMapping.getSubstationLoad(sub->getID());
foreach(QSharedPointer<LoadInfo> p,vecLoadInfo)
{
QFileInfo targetBasename(p->getLoadPath());
// std::cout<<"search for "<<targetBasename.baseName().toLocal8Bit().data()<<std::endl;
if( baseNames.contains( targetBasename.baseName()) and !usedLoad.contains(targetBasename.baseName()))
{
// std::cout<<targetBasename.baseName().toLocal8Bit().data()<<" used "<<std::endl;
usedLoad<<targetBasename.baseName();
writer<<ite->fromNum<<"\t";
writer<<ite->toNum<<"\t";
writer<<"type"<<"\t";
// writer<<ite->id<<"\t";
writer<<tf->getMVA()<<"\r\n";
break;
}
}
}
//列出DG
for(QList<DGStru>::iterator ite=this->dg.begin();
ite!=this->dg.end();
ite++)
{
writer<<ite->fromNum<<"\t";
writer<<ite->toNum<<"\t";
writer<<ite->dg->capacity<<"\r\n";
}
//列出没有用的负荷文件
foreach(QString name, baseNames)
{
if(!usedLoad.contains(name))
{
LoadMatchException except;
if(!except.contains(name))
{
std::cout<<name.toLocal8Bit().data()<<"not used"<<std::endl;
}
else
{
std::cout<<name.toLocal8Bit().data()<<"not used but excepted."<<std::endl;
}
}
}
this->topologyTest();
fd.close();
}
}
template<typename T>
void CIMExporter::idToNumber(QList<T> &s)//把所有元件的标识进行编号
{
for(typename QList<T>::iterator ite=s.begin();
ite!=s.end();
ite++
)
{
if(ite->dispose)
{
// std::cout<<ite->id.toStdString()<<" dispose"<<std::endl;
continue;
}
T _t=*ite;
_t.fromNum=this->numberIt(_t.fromID);
_t.toNum=this->numberIt(_t.toID);
*ite=_t;
}
}
int CIMExporter::numberIt(const QString& id)
{
if(this->number.contains(id))
{
return this->number.value(id);
}
else
{
int n=this->number.keys().length()+1;
this->number[id]=n;
// std::cout<<id.toStdString()<<" "<<n<<std::endl;
return n;
}
}
bool CIMExporter::topologyTest()
{
//检查一下拓扑关系
QList<QPair<int,int> > linkage;
for(QList<LineStru>::iterator ite=this->line.begin();
ite!=this->line.end();
ite++)
{
if(ite->dispose)
{
continue;
}
linkage.push_back(QPair<int,int>(ite->fromNum,ite->toNum));
}
for(QList<SwitchStru>::iterator ite=this->sw.begin();
ite!=this->sw.end();
ite++)
{
if(ite->dispose)
{
continue;
}
linkage.push_back(QPair<int,int>(ite->fromNum,ite->toNum));
}
for(QList<TransformerStru>::iterator ite=this->tf.begin();
ite!=this->tf.end();
ite++)
{
if(ite->dispose)
{
continue;
}
linkage.push_back(QPair<int,int>(ite->fromNum,ite->toNum));
}
for(QList<DGStru>::iterator ite=this->dg.begin();
ite!=this->dg.end();
ite++)
{
if(ite->dispose)
{
continue;
}
linkage.push_back(QPair<int,int>(ite->fromNum,ite->toNum));
}
TopologyTest tpTest(this->number.keys().length());
if(tpTest.start(linkage))
{
std::cout<<"topoloty ok!"<<std::endl;
return true;
}else
{
std::cout<<"not ok topology"<<std::endl;
return false;
}
}