Beispiel #1
0
    def __setDcopfData(self,msg,dispatchNo,destination,\
    fname=dirName+'/data/multiperiod_dcopf_res.json',maxSolar=95348.0):
        """Reads dcopf data from matpower in .json format and packs the data in a 
		format that is understood by PFLOW (contained in msg) for a given dispatch."""
        try:
            data = json.load(open(fname))
            if destination.lower() == 'pflow':
                dispatchData = data['dispatch_' + str(dispatchNo)]
                msg['mpc']['set']['Pg'] = dispatchData['Pg']
            elif destination.lower() == 'gld':
                #data['loadShape'][dispatchNo-1] is total load seen at T side i.e.
                # load-solar. data['dispatch_'+str(dispatchNo)]['solar'] is
                # solar power.
                msg['mpc']['set']['loadShape']=data['loadShape'][dispatchNo-1]+\
                data['solarShape'][dispatchNo-1]

                ind=np.where(self.solarData[:,1]>=data['solarShape']\
                [dispatchNo-1]*(1/max(data['solarShape']))*maxSolar)[0][0]
                msg['mpc']['set']['solarShape'] = self.solarData[ind, 0]
        except:
            PrintException()
Beispiel #2
0
    def run(self, Vpu, tol=10**-8, ID=5, monitor=False, iterMax=20):
        """Gets the complex power injection at PCC for a given PCC voltage setpoint."""
        try:
            Sinj = [0, 1]
            iteration = 0
            while abs(Sinj[0] - Sinj[1]) > tol and iteration < iterMax:
                convergence_flg = self.setV(Vpu, ID=ID)
                iteration += 1
                if convergence_flg == 1:
                    temp, status = self.getS(Vpu, ID=ID)
                    if status == 1:
                        Sinj[1] = Sinj[0]
                        Sinj[0] = temp

            if monitor == True:
                res = self.monitor(ID)
            else:
                res = {}

            return Sinj[0], res, convergence_flg
        except:
            PrintException()
Beispiel #3
0
    def setQvCurve(self,
                   pcc_volt,
                   inv_volt,
                   inv_Q,
                   sensitivity_info,
                   minV=0.95,
                   maxV=1.05,
                   tol=0.01):
        try:
            # check current voltage levels
            setPoint = {}
            setPoint['flg'] = 0
            # TODO: json.encoder.FLOAT_REPR did not work. need a better workaround.
            float_repr = lambda x: [round(entry, 2) for entry in x]

            for ID in pcc_volt:
                if pcc_volt[ID] < minV:
                    violation_lower = minV - pcc_volt[ID]
                    Q = inv_Q[ID]  # current Q. All phases produce same Q

                    if Q < 1:  # if current Q gen is already at max inverter rating nothing more can be done
                        Vreq = inv_volt[ID] + (
                            sensitivity_info['inv'][ID] /
                            sensitivity_info['pcc'][ID]) * violation_lower
                        V1, V2, V3, V4 = inv_volt[
                            ID] - tol, Vreq + tol, Vreq + tol, 1.1
                        Qsetpoint = min(
                            1.0,
                            (violation_lower / sensitivity_info['pcc'][ID]) +
                            Q)
                        Q1, Q2, Q3, Q4 = Qsetpoint, Qsetpoint, Qsetpoint, -0.25
                        setPoint[ID]=[float_repr([V1,Q1]),float_repr([V2,Q2]),\
                        float_repr([V3,Q3]),float_repr([V4,Q4])]
                        setPoint['flg'] = 1

            return setPoint
        except:
            PrintException()
Beispiel #4
0
    def setup(self, fedName='pyGld', portNum=12000, ID=5):
        try:
            fedName += '_' + str(ID)
            self.fedName = fedName

            # setup helics
            self.fi = fi = h.helicsFederateInfoCreate()  # create info obj
            status = h.helicsFederateInfoSetFederateName(fi, fedName)
            status = h.helicsFederateInfoSetCoreTypeFromString(fi, "zmq")
            status = h.helicsFederateInfoSetCoreInitString(fi, "--federates=1")
            status = h.helicsFederateInfoSetTimeDelta(
                fi, 300)  # 5 min dispatch interval
            self.vf = vf = h.helicsCreateValueFederate(fi)
            self.pub = h.helicsFederateRegisterGlobalPublication(
                vf, fedName, "string", "")
            self.sub = h.helicsFederateRegisterSubscription(
                vf, "adaptive_volt_var", "string", "")
            status = h.helicsFederateEnterExecutionMode(vf)

            # setup worker
            self.pyGldWorker.setup(portNum=portNum, ID=ID)
        except:
            PrintException()
Beispiel #5
0
 def abc2seq(self, x_abc):
     try:
         x_seq = self.T.dot(x_abc)  # T*x_abc
         return x_seq  # seq
     except:
         PrintException()
Beispiel #6
0
    def run(self,dt=300.0,ID=5,\
    scale={5:0.0000009, 7:0.000001, 9:0.00000125},monitor=False,\
    inv_nominalV={5:480.0, 7:480.0, 9:480.0},msgSize=1024):
        try:
            Res = []
            res = {}
            simTime = 0.0
            comm_end = 0
            while comm_end == 0:
                grantedTime = h.helicsFederateRequestTime(self.vf, simTime)
                status, msg = h.helicsSubscriptionGetString(
                    self.sub)  # receive from pyPflow
                simTime += dt

                if 'setpoint' in msg:
                    msg = eval(msg)

                    if ID in msg['setpoint']:
                        propVal = []
                        for entry in msg['setpoint'][ID]:
                            propVal.append(entry[0])
                            propVal.append(entry[1])

                        objName = ['solar_inv'] * 8
                        propName = [
                            'V1', 'Q1', 'V2', 'Q2', 'V3', 'Q3', 'V4', 'Q4'
                        ]
                        self.pyGldWorker.objVal(ID,
                                                objName,
                                                propName,
                                                propVal,
                                                flg='send')  # set new QV curve
                        self.pyGldWorker.objVal(ID,
                                                objName,
                                                propName,
                                                ['none'] * len(propVal),
                                                flg='recv')  # set new QV curve

                    status = h.helicsPublicationPublishString(
                        self.pub, 'Received QV curve')
                    grantedTime = h.helicsFederateRequestTime(
                        self.vf, simTime)  # sync at this point
                    simTime += dt
                else:
                    msg = eval(msg)
                    if 'comm_end' in msg:
                        if msg['comm_end'] == 1:
                            comm_end = 1
                            if len(res) > 0:
                                Res.append(res)

                    if comm_end == 0:
                        # set load if requested
                        if 'set' in msg['mpc'].keys():
                            if 'loadShape' in msg['mpc']['set'].keys():
                                self.pyGldWorker.setLoad(
                                    msg['mpc']['set']['loadShape'], ID=ID)
                                self.pyGldWorker.setSolar(
                                    msg['mpc']['set']['solarShape'], ID=ID)
                                if len(res) > 0:
                                    Res.append(res)

                        Vpu = 0
                        for entry in msg['mpc']['get']['V']:
                            if entry[0] == ID:
                                Vpu = entry[1]

                        Pd = 0
                        Qd = 0
                        if Vpu != 0:
                            Sinj, res, convergence_flg = self.pyGldWorker.run(
                                Vpu, ID=ID,
                                monitor=monitor)  # will call gridlabd server
                            if convergence_flg == 1:
                                Pd = Sinj.real * scale[ID]
                                Qd = Sinj.imag * scale[ID]

                        msg = {}
                        msg['mpc'] = {}
                        msg['mpc']['set'] = {}
                        msg['mpc']['get'] = {}
                        if Pd != 0 and Qd != 0:
                            msg['mpc']['set']['Pd'] = [[
                                ID, 1, math.ceil(Pd * 10**6) * 10**-6
                            ]]
                            msg['mpc']['set']['Qd'] = [[
                                ID, 1, math.ceil(Qd * 10**6) * 10**-6
                            ]]
                            msg['mpc']['get']['V'] = [[ID, 0, 0]]
                            msg['mpc']['set']['solar_V']=math.ceil(min([\
                            res[ID]['solar_meter']['measured_voltage_A_mag']/inv_nominalV[ID]*math.sqrt(3),\
                            res[ID]['solar_meter']['measured_voltage_B_mag']/inv_nominalV[ID]*math.sqrt(3),\
                            res[ID]['solar_meter']['measured_voltage_C_mag']/inv_nominalV[ID]*math.sqrt(3)\
                            ])*10**4)*10**-4
                            msg['mpc']['set']['solar_Q']=\
                            math.ceil(-res[ID]['solar_meter']['measured_reactive_power']/\
                            (res[ID]['solar_inv']['rated_power']*3)*10**2)*10**-2 # rated power is per phase

                        # send
                        status = h.helicsPublicationPublishString(
                            self.pub, str(msg))  # publish Sinj as fedName
                        grantedTime = h.helicsFederateRequestTime(
                            self.vf, simTime)  # sync at this point
                        simTime += dt
            if monitor == True:
                json.dump(
                    Res,
                    open(dirName + '/results/res_' + str(ID) + '.json', 'w'))
        except:
            PrintException()
Beispiel #7
0
 def init(self, host='127.0.0.1', portNum=10500):
     try:
         self.pyGldWorker.init(host=host, portNum=portNum)
     except:
         PrintException()
Beispiel #8
0
            h.helicsFederateFree(self.vf)
            h.helicsCloseLibrary()
            self.pyGldWorker.close(ID=ID)
        except:
            PrintException()


#========================RUN AS SCRIPT=======================
if __name__ == "__main__":
    """Sample call: python pyGld.py ID=5 client_portNum=10500 server_portNum=12000"""
    try:
        options = {}
        options['ID'] = 5
        options['client_portNum'] = 10500
        options['server_portNum'] = 12000
        for n in range(1, len(sys.argv)):
            arg, val = sys.argv[n].split('=')
            if arg in options:
                options[arg] = int(val)

        ID, client_portNum, server_portNum = options['ID'], options[
            'client_portNum'], options['server_portNum']

        gld = PyGld()
        gld.init(portNum=client_portNum)
        gld.setup(portNum=server_portNum, ID=ID)
        gld.run(ID=ID, monitor=True)
        gld.close(ID=ID)
    except:
        PrintException()
Beispiel #9
0
    def run(self,dt=300.0,nDis=5,tol=10**-3,\
    pflowFname=dirName+'/results/pflowRes.json',adaptive=False,iterMax=10,\
    msgSize=1024):
        try:
            simTime = 0.0
            comm_end = 0
            pflowRes = []
            setPoint = {}
            sensitivity_info = {}
            sensitivity_info['inv'] = {5: 0.025, 7: 0.025, 9: 0.025}
            sensitivity_info['pcc'] = {5: 0.01, 7: 0.01, 9: 0.01}
            iteration = 0
            dispatchNo = 1

            while comm_end == 0:
                #send (set V at distribution side)
                self.msgFromPflow['mpc'].pop('set')
                self.msgFromPflow['mpc']['set'] = {}

                if iteration == 0:  #set load at iteration 0
                    self.__setDcopfData(self.msgFromPflow, dispatchNo, 'gld')
                    boundaryConditionCheck = np.zeros(shape=(len(self.ID), 2))

                if adaptive and 'flg' in setPoint and setPoint[
                        'flg'] == 1:  # change QV curve if needed
                    setPoint.pop('flg')
                    temp_setpoint = {}
                    temp_setpoint['setpoint'] = setPoint
                    temp_setpoint = str(temp_setpoint).replace(' ', '')
                    setPoint = {}

                    status = h.helicsPublicationPublishString(
                        self.pub, temp_setpoint)
                    grantedTime = h.helicsFederateRequestTime(self.vf, simTime)
                    simTime += dt
                    grantedTime = h.helicsFederateRequestTime(self.vf, simTime)

                    for ID in self.ID:  # receive from GLD
                        errFlg, temp = h.helicsSubscriptionGetString(
                            self.sub['pyGld_' + str(ID)])
                        temp = eval(temp)
                    simTime += dt
                else:
                    status = h.helicsPublicationPublishString(
                        self.pub, str(self.msgFromPflow)
                    )  # will send msg i.e. Vpcc to all
                    # distribution feeders that are subscribers of publisher called pyPflow
                    grantedTime = h.helicsFederateRequestTime(self.vf, simTime)
                    simTime += dt

                    #recv (getS from distribution side)
                    grantedTime = h.helicsFederateRequestTime(self.vf, simTime)

                    msgFromGld = {}
                    msgFromGld['mpc'] = {}
                    msgFromGld['mpc']['set'] = {}
                    msgFromGld['mpc']['get'] = {}
                    setData = msgFromGld['mpc']['set']
                    getData = msgFromGld['mpc']['get']
                    setData['Pd'] = []
                    setData['Qd'] = []
                    getData['V'] = []

                    inv_volt = {}
                    inv_Q = {}
                    for ID in self.ID:
                        errFlg, temp = h.helicsSubscriptionGetString(
                            self.sub['pyGld_' + str(ID)])
                        temp = eval(temp)

                        setData['Pd'].append(temp['mpc']['set']['Pd'][0])
                        setData['Qd'].append(temp['mpc']['set']['Qd'][0])
                        getData['V'].append(temp['mpc']['get']['V'][0])
                        inv_volt[ID] = temp['mpc']['set']['solar_V']
                        inv_Q[ID] = temp['mpc']['set']['solar_Q']
                    simTime += dt

                    # run PFLOW (setS and getV from transmission side)
                    if iteration == 0:  #set load at iteration 0
                        self.__setDcopfData(msgFromGld, dispatchNo, 'pflow')

                    self.socket.send_string(
                        json.dumps(msgFromGld))  # send instructions to pflow
                    self.msgFromPflow = eval(
                        self.socket.recv())  # receive data from pflow
                    iteration += 1

                    # check boundary condition
                    V = np.array(self.msgFromPflow['mpc']['get']['V'])

                    pcc_volt = {}
                    for ID in self.ID:
                        pcc_volt[ID] = V[V[:, 0] == ID, 1][0]

                    count = 0
                    for ID in self.ID:
                        boundaryConditionCheck[
                            count, 1] = boundaryConditionCheck[count, 0]
                        boundaryConditionCheck[count, 0] = V[V[:, 0] == ID,
                                                             1][0]
                        count += 1

                    if np.all(
                            abs(boundaryConditionCheck[:, 0] -
                                boundaryConditionCheck[:, 1]) < tol):
                        # check for QV setting
                        if adaptive:
                            setPoint = self.setQvCurve(pcc_volt, inv_volt,
                                                       inv_Q, sensitivity_info)
                        else:
                            setPoint = {}
                            setPoint['flg'] = 0

                        # implies no adaptive changes are required or max iterations have been exceeded
                        if setPoint['flg'] == 0 or iteration > iterMax:
                            six.print_("Completed Dispath Number: ",
                                       dispatchNo)
                            pflowRes.append([self.msgFromPflow,
                                             msgFromGld])  # store pflow result
                            iteration = 0  # reset iteration for the new dispatch
                            if dispatchNo == nDis:
                                comm_end = 1
                                commEndMsg = {}
                                commEndMsg['comm_end'] = 1
                                status = h.helicsPublicationPublishString(
                                    self.pub,
                                    str(commEndMsg))  # send shutdown signal
                                grantedTime = h.helicsFederateRequestTime(
                                    self.vf, simTime)
                                self.socket.send_string("COMM_END")
                                msgFromPflow = self.socket.recv()
                            else:
                                dispatchNo += 1

            # save results
            json.dump(pflowRes, open(pflowFname, 'w'))
        except:
            PrintException()
Beispiel #10
0
 def close(self):
     try:
         h.helicsFederateFree(self.vf)
         h.helicsCloseLibrary()
     except:
         PrintException()