2

I'm trying to do a simulation with simpy for a battery charging. The parameters needed are the battery capacity (W), the state of charge of the battery (in percentage) and the charging power of the charger (W/h)

My code looks like this:

import simpy
import random

battery_percentage = []

class ChargingCycle(object):
    def __init__(self, env, battery_capacity, SoC, charging_power):
        self.env = env
        self.battery_capacity = battery_capacity
        self.SoC = SoC
        self.charging_power = simpy.Resource(env,charging_power)

    def charge(self, battery):
        print(self.charging_power)
        power_per_minute = self.charging_power/60
        charge_in_kw = (self.SoC/100)/self.battery_capacity
        charge_in_kw += power_per_minute
        self.SoC = charge_in_kw/self.battery_capacity*100

    
def start_charging(env, battery, charging):
    with charging.charging_power.request() as request:
        yield request
        yield env.process(charging.charge(battery))
    

def run_charging(env, battery_capacity, SoC, charging_power):
    charging = ChargingCycle(env, battery_capacity, SoC, charging_power)
    battery = 0
    while True:
        yield env.timeout(1)
        battery += 1
        env.process(start_charging(env, battery, charging))

def main():
    env = simpy.Environment()
    env.process(run_charging(env,45000, 10, 7630))
    env.run(until=360)

if __name__ == "__main__":
    main()

Basically I want to get the charging power for every minute, and every minute I add the power given from the charger to the battery to a variable charge_in_kw and I update the state of charge SoC.

When I run the simulation I get this error:

File "c:\Users\sc57978\Desktop\OCPP\sim_charge.py", line 17, in charge
power_per_minute = self.charging_power/60

TypeError: unsupported operand type(s) for /: 'Resource' and 'int'

Someone knows how can I fix this?

Geo30
  • 99
  • 1
  • 6
  • You are dividing a resource by int which is obviously not gonna happen, try to get the integral value from resource by self.charging_power.capacity / 60 – Zain Ul Abidin Jun 23 '22 at 08:19
  • I tried doing this also but I get a new error: line 26, in start_charging yield env.process(charging.charge(battery)) raise ValueError(f'{generator} is not a generator.') ValueError: None is not a generator. – Humberto Adhemar Jun 23 '22 at 08:21
  • i am also trying this, this error actually exists in the charge function of CharginCycle class because its not returning any thing we need to use yield here to make it a generator – Zain Ul Abidin Jun 23 '22 at 08:28

1 Answers1

3

After running and doing a lot of debugging i am able to run your code, i made following changes to make it run

  • Get the value from the charging_capacity resource to solve your actual error
  • added yield self.env.timeout(1) to resolve further error in Charge function

Final Running Code

import simpy
import random

battery_percentage = []

class ChargingCycle(object):
    def __init__(self, env, battery_capacity, SoC, charging_power):
        self.env = env
        self.battery_capacity = battery_capacity
        self.SoC = SoC
        self.charging_power = simpy.Resource(env, charging_power)

    def charge(self, battery):
        print(self.charging_power)
        power_per_minute = self.charging_power.capacity/60
        charge_in_kw = (self.SoC/100)/self.battery_capacity
        charge_in_kw += power_per_minute
        self.SoC = charge_in_kw/self.battery_capacity*100
        yield self.env.timeout(1)


def start_charging(env, battery, charging):
    with charging.charging_power.request() as request:
        yield request
        yield env.process(charging.charge(battery))
    

def run_charging(env, battery_capacity, SoC, charging_power):
    charging = ChargingCycle(env, battery_capacity, SoC, charging_power)
    battery = 0
    while True:
        yield env.timeout(1)
        battery += 1
        env.process(start_charging(env, battery, charging))

def main():
    env = simpy.Environment()
    env.process(run_charging(env,45000, 10, 7630))
    env.run(until=360)

if __name__ == "__main__":
    main()
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29