How to modify/add node attributes

Hi everyone,
Currently, I want to modify the variable_cost of node whose label is “grid_elec” using an external script.

    nodes.append(
                        solph.Source(
                            label= "grid_elec",
                            outputs={
                                solph.Bus(label="bel"): solph.Flow(
                                    variable_costs=0.22
                                )
                            },
                        )
                    )

Could some one tell me what is the best way to do.

By the following loop, I could modify the node label

for node in nodes:
            if isinstance(node,solph.network.Source):
                # print(dir(node))
                if node.label=="grid_elec":
                    node.label="grid_electricity"

Thanks a lot!

Regards,

Long

Hey Long,

to access the attribute variable_costs you need to touch the flow object.

node.outputs.data[bel].variable_costs 

note that you call the flow in a dictionary with the bus-object as key (not your given label “bel”).

Regards
Till

Thank tllsngnzlz,

So could you tell me how can I modify the variable_costs (original value is 0.22) of node.outputs.data[bel].variable_costs? when I tried to print out this node.outputs.data[bel].variable_costs, I got empty list [ ].

Long

Hey Long,

I dont really know but you should be able to pass a list with as many items as your length of the datetime index of your energysystem. But I dont get at what point do you want to alter the variable_costs?

Do you want to change the variable_costs before adding the node to the energysystem?
Have you already added the node to your energysystem?
Do you want to change the variable_costs in the flow before solving the model?

I do not really understand your problem without your code.

Greetings,
Till

Hey Till,

Supposing that I have a python (executable) program/script (e.g. base.py) which is used to created nodes, energy_system and also to solve the optimization model. The problem is that I can not modify anything in this python script base.py but this base.py allows me to get nodes or energy_system created by itself to add/modify the attributes in an external python script before solve optimization model.

base.py --> nodes --> energy_system --> solve
               |
               |
       external_script.py

So what I am trying to do is found in external_script.py which nodes created by base.py. Indeed I want not only to modify the node attributes but to add new ones as well.

Regards,

Long

Finally, I think that I could you the following script to update variable_costs. I’m not sure this is a best way but at least it works :slight_smile:

for node in nodes:
            if isinstance(node,solph.network.Source):
                if node.label=="grid_elec":
                    node.label="grid_electricity"
                   node.outputs[bel].variable_costs = [0.3]*len(energy_system.timeindex)