Exemplo n.º 1
0
 def close(self, ID=5):
     try:
         h.helicsFederateFree(self.vf)
         h.helicsCloseLibrary()
         self.pyGldWorker.close(ID=ID)
     except:
         PrintException()
Exemplo n.º 2
0
    def setSolar(self, val, ID=5):
        try:
            msg='GET /xml/solar_weather/solar_diffuse/'+str(val)+\
            '/solar_weather/solar_direct/'+str(val)+\
            '/solar_weather/solar_global/'+str(val)+' HTTP/1.1\r\n'

            if six.PY2:
                self.gldConn[ID].send(msg)  # send msg to gridlabd server
                self.syncConn[ID][0].send(
                    self.sync_ackMsg
                )  # inform gridlabd sync client that we are done setting V
                ack_from_sync_client = int(
                    self.syncConn[ID][0].recv(BUFFER_SIZE))
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE)  # reply from gridlabd server
            elif six.PY3:
                self.gldConn[ID].send(
                    msg.encode())  # send msg to gridlabd server
                self.syncConn[ID][0].send(self.sync_ackMsg.encode(
                ))  # inform gridlabd sync client that we are done setting V
                ack_from_sync_client = int(
                    self.syncConn[ID][0].recv(BUFFER_SIZE).decode())
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE).decode()  # reply from gridlabd server
        except:
            PrintException()
Exemplo n.º 3
0
    def setup(self,host='127.0.0.1',portNum=12000,\
    fname=dirName+'/data/IEEE13_solar.glm',ID=5,debug=False):
        try:
            if debug:
                self.f_gld_out = open('gld_out_' + str(ID) + '.txt', 'w')
                self.f_gld_err = open('gld_err_' + str(ID) + '.txt', 'w')
            else:
                self.f_gld_out = open('/dev/null', 'w')
                self.f_gld_err = open('/dev/null', 'w')

            gld_directive='gridlabd -D run_realtime=1 -D client_portnum='+\
            str(self.syncServerPortNum)+' -D server_portnum='

            self.proc=Popen(shlex.split(gld_directive+str(portNum)+\
            ' '+fname+' --server -v'),stdout=self.f_gld_out,stderr=self.f_gld_err,\
            close_fds=True)

            self.syncConn[ID] = self.s.accept(
            )  # accept connection from gld (sync connection)
            self.gldConn[ID] = socket.socket(socket.AF_INET,
                                             socket.SOCK_STREAM)
            self.gldConn[ID].connect((host, portNum))
            self.IDs.append(ID)  # add each interface bus ID
        except:
            PrintException()
Exemplo n.º 4
0
    def setLoad(self, scale, fname=dirName + '/data/loadMap.json', ID=5):
        """Scales Feeder load."""
        try:
            loadMap = json.load(open(fname))
            msg = 'GET /xml'

            for loadObj in loadMap:
                for prop in loadMap[loadObj]:
                    msg += '/' + loadObj + '/' + prop + '/' + str(
                        math.ceil(loadMap[loadObj][prop] * scale))

            msg += ' HTTP/1.1\r\n'

            if six.PY2:
                self.gldConn[ID].send(msg)  # send msg to gridlabd server
                self.syncConn[ID][0].send(
                    self.sync_ackMsg
                )  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(self.syncConn[ID][0].recv(BUFFER_SIZE))
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE)  # reply from gridlabd server
            elif six.PY3:
                self.gldConn[ID].send(
                    msg.encode())  # send msg to gridlabd server
                self.syncConn[ID][0].send(self.sync_ackMsg.encode(
                ))  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(
                    self.syncConn[ID][0].recv(BUFFER_SIZE).decode())
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE).decode()  # reply from gridlabd server
        except:
            PrintException()
Exemplo n.º 5
0
    def setV(self, VPu, VNominal=2400, ID=5):
        try:
            V = VPu * VNominal
            Vabc = np.complex(V) * np.array([[1], [self.a * self.a], [self.a]
                                             ])  # pos seq to abc

            msg='GET /xml/Node650/voltage_A/'+str(Vabc[0][0].real)+'+0j/'\
            +'/Node650/voltage_B/'+str(Vabc[1][0]).strip('()')\
            +'/Node650/voltage_C/'+str(Vabc[2][0]).strip('()')+' HTTP/1.1\r\n'

            if six.PY2:
                self.gldConn[ID].send(msg)  # send msg to gridlabd server
                self.syncConn[ID][0].send(
                    self.sync_ackMsg
                )  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(self.syncConn[ID][0].recv(BUFFER_SIZE))
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE)  # reply from gridlabd server
            elif six.PY3:
                self.gldConn[ID].send(
                    msg.encode())  # send msg to gridlabd server
                self.syncConn[ID][0].send(self.sync_ackMsg.encode(
                ))  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(
                    self.syncConn[ID][0].recv(BUFFER_SIZE).decode())
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE).decode()  # reply from gridlabd server

            return convergence_flg
        except:
            PrintException()
Exemplo n.º 6
0
 def init(self, portNum=10000):
     try:
         # connect to pflow
         context = zmq.Context()
         socket = context.socket(zmq.REQ)
         socket.connect("tcp://localhost:" + str(portNum))
         self.socket = socket
     except:
         PrintException()
Exemplo n.º 7
0
 def init(self, host='127.0.0.1', portNum=10500):
     try:
         self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.s.bind((host, portNum))
         self.s.listen(0)
         self.syncConn = {}
         self.gldConn = {}
         self.IDs = []
         self.syncServerPortNum = portNum
     except:
         PrintException()
Exemplo n.º 8
0
    def monitor(self, ID, monMap=dirName + '/data/monitorMap.json'):
        """Send msg to gridlabd server to set/get an object property value.
		if propVal is none, then it is a get operation if not it is a set operation.
		ID - interface_bus_number/id
		objName - object name (should be a list)
		propName - property name (should be a list)
		flg - 'send' or 'recv'
		"""
        try:
            if isinstance(monMap, str):  # then a template file is given
                data = json.load(open(monMap))
            elif isinstance(monMap, dict):
                data = copy.deepcopy(monMap)

            if ID not in data:
                data[ID] = copy.deepcopy(data['1'])

            res = {}
            res[ID] = {}
            objName = []
            propName = []
            for entry in data[ID].keys():
                for item in data[ID][entry]:
                    objName.append(entry)
                    propName.append(item)

            self.objVal(ID,objName,propName,\
            propVal=['none']*len(propName),flg='send')
            reply=self.parseHTTPResponse(self.objVal(ID,\
            objName,propName,propVal=['none']*len(propName),\
            flg='recv')).split(',')

            reply=np.array(reply[0:-1]).reshape(\
            int((len(reply)-1)/3),3)#last entry is an empty value

            for obj, prop, val in reply:
                if obj not in res[ID]:
                    res[ID][obj] = {}
                try:
                    if six.PY2:
                        res[ID][obj][prop]=float(val.translate(None,\
                        string.ascii_letters))
                    elif six.PY3:
                        res[ID][obj][prop]=float(val.translate(\
                        {ord(entry):None for entry in string.ascii_letters}))
                except ValueError:  #if translation to float is not possible
                    val, debugInfo = self.str2complex(val)
                    res[ID][obj][prop + '_mag'] = np.abs(val)
                    res[ID][obj][prop + '_ang'] = np.angle(val)

            return res
        except:
            PrintException()
Exemplo n.º 9
0
    def objVal(self, ID, objName, propName, propVal=['none'], flg='send'):
        """Send msg to gridlabd server to set/get an object property value.
		if propVal is none, then it is a get operation if not it is a set operation.
		ID - interface_bus_number/id
		objName - object name (should be a list)
		propName - property name (should be a list)
		propVal - value to be set (should be a list)
		flg - 'send' or 'recv'
		"""
        try:
            if isinstance(objName, str):
                objName = list(objName)
            if isinstance(propName, str):
                propName = list(propName)
            if isinstance(propVal, str):
                propVal = list(propVal)

            if flg == 'send':
                msg = "GET /xml"
                for obj, prop, val in zip(objName, propName, propVal):
                    msg += '/' + obj + '/' + prop + '/' + str(val)
                msg += ' HTTP/1.1\r\n'

                if six.PY2:
                    self.gldConn[ID].send(msg)  # send msg to gridlabd server
                    self.syncConn[ID][0].send(
                        self.sync_ackMsg
                    )  # inform gridlabd sync client that we are done setting V
                elif six.PY3:
                    self.gldConn[ID].send(
                        msg.encode())  # send msg to gridlabd server
                    self.syncConn[ID][0].send(
                        self.sync_ackMsg.encode()
                    )  # inform gridlabd sync client that we are done setting V
                reply_from_server = ''

            elif flg == 'recv':
                if six.PY2:
                    ack_from_sync_client = int(
                        self.syncConn[ID][0].recv(BUFFER_SIZE))
                    reply_from_server = self.gldConn[ID].recv(
                        BUFFER_SIZE)  # reply from gridlabd server
                elif six.PY3:
                    ack_from_sync_client = int(
                        self.syncConn[ID][0].recv(BUFFER_SIZE).decode())
                    reply_from_server = self.gldConn[ID].recv(
                        BUFFER_SIZE).decode()  # reply from gridlabd server

            return reply_from_server
        except:
            PrintException()
Exemplo n.º 10
0
 def parseHTTPResponse(self, msg):
     try:
         flg = 0
         if msg.find("\r\n\r\n") > 0:  #then CRLF is used as line ending
             startInd = msg.find("\r\n\r\n") + 4  # +4 for CRLF CRLF
             flg = 1
         elif msg.find("\n\n") > 0:  #then LF is used as line ending
             startInd = msg.find("\n\n") + 2  # +2 for LF LF
             flg = 1
         if flg == 1:
             msg = msg[startInd::]
         else:  # will return empty msg
             msg = ''
         return msg
     except:
         PrintException()
Exemplo n.º 11
0
    def close(self, ID=5):
        try:
            # send msg to gridlabd server and client for graceful shutdown
            qry_shutdown = "GET /control/shutdown HTTP/1.1"
            if six.PY2:
                self.gldConn[ID].send(qry_shutdown)
                self.syncConn[ID][0].send(self.sync_endMsg)
            elif six.PY3:
                self.gldConn[ID].send(qry_shutdown.encode())
                self.syncConn[ID][0].send(self.sync_endMsg.encode())

            self.s.shutdown(0)
            self.s.close()
            self.gldConn[ID].shutdown(0)
            self.gldConn[ID].close()
        except:
            PrintException()
Exemplo n.º 12
0
    def setup(self, ID):
        """ID needs to be a list that contains all the T&D interface nodes."""
        try:
            # read solar mapping
            self.solarData=np.genfromtxt(dirName+'/data/solar_diffusion_map.csv',\
            delimiter=',')

            self.ID = ID

            # set up comm with helics
            self.fi = fi = h.helicsFederateInfoCreate()  # create info obj
            status = h.helicsFederateInfoSetFederateName(fi, "pyPflow")
            status = h.helicsFederateInfoSetCoreTypeFromString(fi, "zmq")
            status = h.helicsFederateInfoSetCoreInitString(fi, "--federates=1")
            status = h.helicsFederateInfoSetTimeDelta(
                fi, 300)  # 5 min dispatch interval
            status = h.helicsFederateInfoSetLoggingLevel(fi, 1)
            self.vf = vf = h.helicsCreateValueFederate(fi)
            self.pub = pub = h.helicsFederateRegisterGlobalPublication(
                vf, "adaptive_volt_var", "string", "")

            self.sub = sub = {}
            for entry in ID:  # subscribe to all IDs
                sub['pyGld_' +
                    str(entry)] = h.helicsFederateRegisterSubscription(
                        vf, 'pyGld_' + str(entry), "string", "")

            status = h.helicsFederateEnterExecutionMode(vf)

            # Read schema
            f = open(dirName + '/data/schema_case9.json')
            self.data = eval(f.read())
            f.close()

            # add V in schema for all subscriptions
            for entry in ID:
                self.data['mpc']['get']['V'].append([entry, 0, 0])

            # run initial PFLOW (base case condition as read from .m file)
            self.socket.send_string(json.dumps(
                self.data))  # send instructions to pflow
            self.msgFromPflow = eval(
                self.socket.recv())  # receive data from pflow

        except:
            PrintException()
Exemplo n.º 13
0
    def getS(self, VPu, VNominal=2400, ID=5):
        try:
            V = VPu * VNominal
            Sinj_pos = 0 + 0j
            msg = "GET /xml/Reg1/current_in_A/none/Reg1/current_in_B/none/Reg1/current_in_C/none HTTP/1.1\r\n"

            if six.PY2:
                self.gldConn[ID].send(msg)  # send msg to gridlabd server
                self.syncConn[ID][0].send(
                    self.sync_ackMsg
                )  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(self.syncConn[ID][0].recv(BUFFER_SIZE))
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE)  # reply from gridlabd server
            elif six.PY3:
                self.gldConn[ID].send(
                    msg.encode())  # send msg to gridlabd server
                self.syncConn[ID][0].send(self.sync_ackMsg.encode(
                ))  # inform gridlabd sync client that we are done setting V
                convergence_flg = int(
                    self.syncConn[ID][0].recv(BUFFER_SIZE).decode())
                reply_from_server = self.gldConn[ID].recv(
                    BUFFER_SIZE).decode()  # reply from gridlabd server

            if convergence_flg == 1:
                Iabc_str = self.parseHTTPResponse(reply_from_server)

                if len(Iabc_str) > 0:
                    # now convert to np.complex
                    Iabc_list = Iabc_str.split(',')
                    Iinj_abc=np.array([np.complex(Iabc_list[2].strip(' A').replace('i','j')),\
                    np.complex(Iabc_list[5].strip(' A').replace('i','j')),\
                    np.complex(Iabc_list[8].strip(' A').replace('i','j'))])
                    Iinj_seq = self.abc2seq(Iinj_abc)
                    Sinj_pos = 3 * np.complex(V) * Iinj_seq[1].conjugate()
                else:  # try to request again if an empty string is returned by gridlabd
                    self.getS(VPu, VNominal, ID)

            return Sinj_pos, convergence_flg
        except:
            PrintException()
Exemplo n.º 14
0
    def str2complex(self, strData, precision=2):
        """Converts string returned by gridlabd to a complex number.
		The string can be in polar or rectangular form."""
        try:
            val = 0.0
            debugInfo = []
            # first check if the string is in rectangular or polar form
            if 'd' in strData:  # string likely contains polar form
                strValue = strData.split(' ')[0]
                dInd = strValue.find('d')
                if 'e' in strValue:  # e+- form
                    eInd = strValue.find('e')
                    try:
                        scale = float('1' + strValue[eInd:dInd])  #1e+-x form
                    except ValueError:
                        debugInfo.append([strData, strValue, dInd, eInd])
                else:
                    eInd = -1
                    scale = 1
                strValue = strValue[0:eInd]
                degreeInd = strValue[1::].rfind('+')
                if degreeInd == -1:
                    degreeInd = strValue[1::].rfind('-')

                if degreeInd != -1:
                    degreeInd += 1  #offset due to str[1::]
                    degree = float(strValue[degreeInd::]) * scale
                    theta = degree * (math.pi / 180)  # in radians
                    magnitude = float(strValue[0:degreeInd])
                    val=math.ceil(magnitude*math.cos(theta)*10**precision)*10**-precision+\
                    1j*math.ceil(magnitude*math.sin(theta)*10**precision)*10**-precision
                else:
                    val = 0.0
            elif 'i' in strData:  # string likely contains rectangular form
                val = complex(strData.replace('i', 'j'))
                val=math.ceil(val.real*10**precision)*10**-precision+\
                1j*math.ceil(val.imag*10**precision)*10**-precision

            return val, debugInfo
        except:
            PrintException()
Exemplo n.º 15
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()
Exemplo n.º 16
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()
Exemplo n.º 17
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()
Exemplo n.º 18
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()
Exemplo n.º 19
0
 def close(self):
     try:
         h.helicsFederateFree(self.vf)
         h.helicsCloseLibrary()
     except:
         PrintException()
Exemplo n.º 20
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()
Exemplo n.º 21
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()
Exemplo n.º 22
0
 def init(self, host='127.0.0.1', portNum=10500):
     try:
         self.pyGldWorker.init(host=host, portNum=portNum)
     except:
         PrintException()
Exemplo n.º 23
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()
Exemplo n.º 24
0
 def abc2seq(self, x_abc):
     try:
         x_seq = self.T.dot(x_abc)  # T*x_abc
         return x_seq  # seq
     except:
         PrintException()