345 lines
12 KiB
C++
345 lines
12 KiB
C++
#include "topologyrecorder.h"
|
||
#include <iostream>
|
||
#include <QList>
|
||
TopologyRecorder::TopologyRecorder(const QHash<QString,BasicElementInfo *>& elementHT, const QVector<QString> &FDSet, const QString& loadFilePath, const QString &rootID, QObject *parent):QObject(parent),eleHT(elementHT),FDSet(FDSet),cimExporter(elementHT,loadFilePath),rootID(rootID)
|
||
{
|
||
}
|
||
|
||
TopologyRecorder::~TopologyRecorder()
|
||
{
|
||
|
||
}
|
||
|
||
bool TopologyRecorder::addDG(const QString& substationID, const QString &fromNodeID)
|
||
{
|
||
// std::cout<<"sub id"<<substationID.toStdString()<<std::endl;
|
||
DGMapping dgMapping;
|
||
QVector<QSharedPointer<DGInfo> > DGs=dgMapping.getDGBySubstationID(substationID);
|
||
foreach(QSharedPointer<DGInfo> _DG,DGs)
|
||
{
|
||
double capacity=_DG->getCapacity();
|
||
QPair<QString,QString> fromTo;
|
||
fromTo.first=fromNodeID;
|
||
fromTo.second=_DG->getName();//虚拟节点
|
||
if(this->usedDG.contains(_DG->getName()))
|
||
{
|
||
break;//这个Substation下面已经添加过DG,不用再添加了。
|
||
}
|
||
// std::cout<<"from node"<<fromNodeID.toStdString()<<std::endl;
|
||
DG *DGP=new DG(fromTo,capacity,_DG->getName(),this);
|
||
this->cimExporter.add(fromTo,DGP);
|
||
std::cout<<"add "<<_DG->getName().toLocal8Bit().data()<<" to "<<substationID.toStdString() <<std::endl;
|
||
this->usedDG[_DG->getName()]=0;//用过了,做个记号
|
||
}
|
||
return true;
|
||
}
|
||
|
||
bool TopologyRecorder::isEquipmentNeeded(const QString& substatinID)
|
||
{
|
||
const QHash<QString,BasicElementInfo*> &ht=this->eleHT;
|
||
Substation *sfd=static_cast<Substation *>(ht[substatinID]);
|
||
if(sfd==NULL)
|
||
{
|
||
// std::cout<<"can not find"<<substatinID.toStdString()<<std::endl;
|
||
}
|
||
else
|
||
{
|
||
QString feederID=sfd->getSubstationMemberOf_Feeder();
|
||
return this->FDSet.contains(feederID) ||sfd->getNamingDescription()=="V馈线站";
|
||
}
|
||
return false;
|
||
}
|
||
|
||
void TopologyRecorder::exportTo(const QString& path)
|
||
{
|
||
this->cimExporter.exportTo(path,this->rootID);
|
||
}
|
||
|
||
void TopologyRecorder::showContainerInfo(QTextStream &stream)
|
||
{
|
||
|
||
const QHash<QString,BasicElementInfo*> &ht=this->eleHT;
|
||
// stream<<"total tf"<<this->tfs.length()<<"\n";
|
||
//统计一下每个Container下有多少个变压器
|
||
QHash<QString,QVector<QString> > s;
|
||
for(QVector<QString>::iterator ite=this->tfs.begin();
|
||
ite!=this->tfs.end();
|
||
ite++)
|
||
{
|
||
PowerTransformer *pf=static_cast<PowerTransformer *>(ht[*ite]);
|
||
QString container;
|
||
container=pf->getEquipmentMemberOf_EquipmentContainer();
|
||
s[container].push_back(*ite);
|
||
}
|
||
// std::cout<<"container "<<s.keys().length()<<std::endl;
|
||
QList<QString> keys=s.keys();
|
||
for(QList<QString>::iterator ite=keys.begin();
|
||
ite!=keys.end();
|
||
ite++)
|
||
{
|
||
QString k=*ite;
|
||
stream<<k;
|
||
Substation *sb=static_cast<Substation *>(ht[k]);
|
||
stream<<"("<<sb->getNamingDescription()<<")";
|
||
stream<<"#####";
|
||
QVector<QString> v=s[k];
|
||
foreach (QString n, v) {
|
||
stream<<", "<<n;
|
||
PowerTransformer *pf=static_cast<PowerTransformer *>(ht[n]);
|
||
stream<<"("<<pf->getNamingDescription()<<")";
|
||
}
|
||
stream<<"\n";
|
||
}
|
||
|
||
}
|
||
|
||
bool TopologyRecorder::startWithNode(const QString& node)
|
||
{
|
||
if(node=="")
|
||
{
|
||
return true;
|
||
}
|
||
const QHash<QString,BasicElementInfo*> &ht=this->eleHT;
|
||
QString anotherTerminal;
|
||
QVector<QString> anotherTerminalVector;
|
||
anotherTerminalVector=this->nodeToTerminal.value(node);//一个Node可以找到多个与之相连的Terminal
|
||
// std::cout<<"find "<<anotherTerminalVector.length()<<"terminal "<<std::endl;
|
||
for(int atI=0;atI<anotherTerminalVector.length();atI++)
|
||
{
|
||
TopologyRecorder::Type::EleType typ;
|
||
BasicElementInfo *foundEle=NULL;
|
||
anotherTerminal=anotherTerminalVector.at(atI);
|
||
if(this->reachedTerminal.contains(anotherTerminal))
|
||
{
|
||
continue;
|
||
}
|
||
// std::cout<<anotherTerminal.toStdString()<<std::endl;
|
||
this->reachedTerminal[anotherTerminal]=0;
|
||
Terminal *anotherTerminalP=static_cast<Terminal *>(ht[anotherTerminal]);
|
||
QString ce;
|
||
ce=anotherTerminalP->getConductingEquipment();
|
||
QPair<QString,QString> fromTo;
|
||
QString terminalA;
|
||
QString terminalB;
|
||
//通过ACSegmentLine的ID来找Terminal。下同。
|
||
if(ce.startsWith("AC"))
|
||
{
|
||
QString acID=ce;
|
||
// fromTo.first=node;
|
||
// fromTo.second=acID;
|
||
ACLineSegment *AC=static_cast<ACLineSegment *>(ht[acID]);
|
||
if(!AC)
|
||
{
|
||
continue;
|
||
}
|
||
// std::cout<<AC->getLength()<<std::endl;
|
||
// std::cout<<AC->getID().toStdString()<<std::endl;
|
||
terminalA=AC->getTerminalA();
|
||
terminalB=AC->getTerminalB();
|
||
foundEle=AC;
|
||
typ=TopologyRecorder::Type::AC;
|
||
}
|
||
if(ce.startsWith("SW"))
|
||
{
|
||
BasicElementInfo *bi=ht[ce];
|
||
if(bi!=NULL)
|
||
{
|
||
|
||
if(bi->getName()=="Breaker")
|
||
{
|
||
// std::cout<<bi->getName().toStdString()<<std::endl;
|
||
QString swID=ce;
|
||
// fromTo.first=node;
|
||
// fromTo.second=swID;
|
||
Breaker *SW=static_cast<Breaker *>(ht[swID]);
|
||
if(!SW)
|
||
{
|
||
continue;
|
||
}
|
||
//显示开关状态
|
||
if(SW->getOpenState()==true)
|
||
{
|
||
// std::cout<<"sw "<<SW->getNamingDescription().toStdString()<<"is open"<<std::endl;
|
||
}
|
||
else
|
||
{
|
||
// std::cout<<"sw "<<SW->getNamingDescription().toStdString()<<"is close"<<std::endl;
|
||
}
|
||
|
||
terminalA=SW->getTerminalA();
|
||
terminalB=SW->getTerminalB();
|
||
// std::cout<<"find"<<SW->getID().toStdString()<<std::endl;
|
||
foundEle=SW;
|
||
typ=TopologyRecorder::Type::BREAKER;
|
||
}
|
||
if(bi->getName()=="Disconnector")
|
||
{
|
||
// std::cout<<bi->getName().toStdString()<<std::endl;
|
||
QString disConID=ce;
|
||
Disconnector *Dis=static_cast<Disconnector *>(ht[disConID]);
|
||
//先判断一下是不是需要的Disconnector
|
||
QString container=Dis->getEquipmentMemberOf_EquipmentContainer();
|
||
if(!this->isEquipmentNeeded(container))
|
||
{
|
||
// std::cout<<"Dissw not belong "<<disConID.toStdString()<<std::endl;
|
||
continue;
|
||
}
|
||
// fromTo.first=node;
|
||
// fromTo.second=disConID;
|
||
|
||
if(!Dis)
|
||
{
|
||
continue;
|
||
}
|
||
//显示开关状态
|
||
if(Dis->getOpenState()==true)
|
||
{
|
||
// std::cout<<"Dissw "<<Dis->getNamingDescription().toStdString()<<"is open"<<std::endl;
|
||
}
|
||
else
|
||
{
|
||
// std::cout<<"Dissw "<<Dis->getNamingDescription().toStdString()<<"is close"<<std::endl;
|
||
}
|
||
|
||
terminalA=Dis->getTerminalA();
|
||
terminalB=Dis->getTerminalB();
|
||
// std::cout<<"find"<<Dis->getID().toStdString()<<std::endl;
|
||
foundEle=Dis;
|
||
typ=TopologyRecorder::Type::DISCONNECTOR;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
continue;
|
||
}
|
||
|
||
|
||
}
|
||
if(ce.startsWith("BS"))
|
||
{
|
||
//看找到那个Substation了
|
||
BusbarSection *bs=static_cast<BusbarSection *>(ht[ce]);
|
||
if(bs==NULL)
|
||
{
|
||
// std::cout<<"not find bussection"<<ce.toStdString()<<std::endl;
|
||
}
|
||
else
|
||
{
|
||
QString container=bs->getEquipmentMemberOf_EquipmentContainer();
|
||
Substation *sfd=static_cast<Substation *>(ht[container]);
|
||
if(sfd==NULL)
|
||
{
|
||
// std::cout<<"can not find"<<container.toStdString()<<std::endl;
|
||
}
|
||
else
|
||
{
|
||
// std::cout<<"encounter "<<sfd->getNamingDescription().toLocal8Bit().data()<<std::endl;
|
||
if(!this->isEquipmentNeeded(container))
|
||
{
|
||
// std::cout<<"not belong"<<std::endl;
|
||
}
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
if(ce.startsWith("TF"))
|
||
{
|
||
QString tfID=ce;
|
||
PowerTransformer *tf=static_cast<PowerTransformer *>(ht[tfID]);
|
||
// std::cout<<"TF"<<tf->getNamingDescription().toStdString()<<std::endl;
|
||
//判断一下这个变压器是不是我们需要的
|
||
QString equipmentContainer;
|
||
equipmentContainer=tf->getEquipmentMemberOf_EquipmentContainer();
|
||
if(this->isEquipmentNeeded(equipmentContainer))
|
||
{
|
||
this->tfs.push_back(tfID);
|
||
|
||
foundEle=tf;
|
||
}
|
||
else
|
||
{
|
||
// std::cout<<tf->getNamingDescription().toStdString()<<"not belong"<<std::endl;
|
||
continue;
|
||
}
|
||
typ=TopologyRecorder::Type::TF;
|
||
|
||
}
|
||
if(foundEle)
|
||
{
|
||
|
||
//有可能会访问到重复的元件,所以这里判断一下,如果是已经访问过的就不添加了。
|
||
if(this->hasAdded.contains(foundEle->getID()))
|
||
{
|
||
continue;
|
||
}
|
||
this->hasAdded[foundEle->getID()]=0;
|
||
|
||
Branch *branch=NULL;
|
||
QString nextTerminal;
|
||
Terminal *nextTerminalP;
|
||
QString nextNode;
|
||
if(typ!=TopologyRecorder::Type::TF)
|
||
{
|
||
nextTerminal=(terminalA==anotherTerminal)?terminalB:terminalA;
|
||
nextTerminalP=static_cast<Terminal *>(ht[nextTerminal]);
|
||
if(!nextTerminalP)
|
||
{
|
||
continue;
|
||
}
|
||
else
|
||
{
|
||
nextNode=nextTerminalP->getConnectivityNode();
|
||
fromTo.first=node;
|
||
fromTo.second=nextNode;
|
||
if(nextNode=="")
|
||
{
|
||
fromTo.second=foundEle->getID();//用虚拟节点
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
nextNode="";
|
||
fromTo.first=node;
|
||
fromTo.second=foundEle->getID();//用变压器编号做虚拟节点
|
||
}
|
||
|
||
// std::cout<<typ<<" "<<ce.toStdString()<<std::endl;
|
||
switch(typ)
|
||
{
|
||
case TopologyRecorder::Type::AC:
|
||
branch=new Line(fromTo,static_cast<ACLineSegment *>(foundEle),this);
|
||
this->cimExporter.add(fromTo,static_cast<Line *>(branch));
|
||
break;
|
||
case TopologyRecorder::Type::BREAKER:
|
||
branch=new Switch(fromTo,foundEle->getID(),this);
|
||
this->cimExporter.add(fromTo,static_cast<Switch *>(branch));
|
||
break;
|
||
case TopologyRecorder::Type::DISCONNECTOR:
|
||
branch=new Switch(fromTo,foundEle->getID(),this);
|
||
this->cimExporter.add(fromTo,static_cast<Switch *>(branch));
|
||
break;
|
||
case TopologyRecorder::Type::TF:
|
||
branch=new Transformer(fromTo,static_cast<PowerTransformer *>(foundEle),this);
|
||
this->cimExporter.add(fromTo,static_cast<Transformer *>(branch));
|
||
//把DG接在变压器旁边
|
||
|
||
this->addDG(static_cast<PowerTransformer *>(foundEle)->getEquipmentMemberOf_EquipmentContainer(),fromTo.first);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
// std::cout<<nextNode.toStdString()<<std::endl;
|
||
if(nextNode!="")
|
||
this->startWithNode(nextNode);
|
||
}
|
||
else
|
||
{
|
||
// std::cout<<"not found cate "<<ce.toStdString()<<std::endl;
|
||
continue;
|
||
}
|
||
|
||
}
|
||
return true;
|
||
}
|