Pypsa and Python - Error when simulating power market of multiple countries

29 Views Asked by At

Console error when running code from python:
enter image description here

I am trying to simulate a power market based on three countries using Python and PyPSA. In this example, the countries are DE, FR, NL.

I did not understand how to modify properly the sample code from the PyPSA documentation to achieve such 3 country configuration.

Please find my code below that lead to these error message on the capture attached. PyPSA model returns me values for all outputs: prices, flows and loads. But I also get the error message attached so I do not know what I did not specify properly to solve a multiple n-country model.

Also some interconnections have non-symmetric flows (Export = 2000MW but Import = 1400) I did not figure out in the PyPSA documentation how to specify this situation in the model code.

# transmission capacities in MW (not necessarily realistic)
transmission = {
    "DE": {"FR": 2500, "NL": 1400},
    "FR": {"NL": 1400},
}   


network = pypsa.Network()

for country in countries:
    network.add("Bus", country)

    for plant in power_plant_p_nom[country]:
        network.add(
            "Generator",
            "{} {}".format(country, plant),
            bus=country,
            p_nom=power_plant_p_nom[country][plant],
            marginal_cost=marginal_costs[country][plant],
    )

    network.add("Load", "{} load".format(country), bus=country, p_set=loads[country])

    # add transmission as controllable Link
    if country not in transmission:
        continue

    for other_country in countries:
        if other_country not in transmission[country]:
            continue

        # NB: Link is by default unidirectional, so have to set p_min_pu = -1
        # to allow bidirectional (i.e. also negative) flow
        network.add(
            "Link",
            "{} - {} link".format(country, other_country),
            bus0=country,
            bus1=other_country,
            p_nom=transmission[country][other_country],
            p_min_pu=(-0.8 if (country == "DE" and other_country =="FR") else -1), 
        )
network.optimize()


# Print Results  

# print the load active power (P) consumption
print(network.loads_t.p)

# print the generator active power (P) dispatch
print(network.generators_t.p)

#  print the clearing price (corresponding to gas)
print("SMP :", network.buses_t.marginal_price)

# print Power Flows
print("Power flows:", network.links_t.p0)

# link shadow prices
print(network.links_t.mu_lower)
1

There are 1 best solutions below

0
Qaufeng On

I think you get the error because of the for loop. For instance, in the first iteration you deploy a bus as the first country in the countries list. Then you add some production which is fine. The problem arises when you deploy the links between the countries, as the link takes two buses as input, but the "other_country" bus has not been defined in the network yet. Hence, the error message.

A way to prevent this is to have a for loop where you deploy all the buses (countries) first and then have another for loop where you add the links between the buses.