Arrays and indexing

3 posts / 0 new
Last post
billing
Offline
Joined: 2012-03-20
Arrays and indexing

Hello JModelica peopel! I'm currently trying to compile a model I originally created in OpenModelica but run into a problem with arrays. Consider the following model: 
 

model ElectricitySource

  parameter Real[:] electricityPricePlan = {42,41,40,41,42,60,65,64,63,62,61,58,56,55,56,58,66,62,58,50,46,44,43,42};

  parameter Real energyBaseConsumption = 0.8 "(MWh)";

  Real electricityPrice;

  Integer h(start = 0);

  Pin p "Electricity Consumption";

  output Real cost;

  output Real totalCost;

  output Real totalElectricityUsage;

equation

  p.v = 0;

  electricityPrice = electricityPricePlan[h];

  cost = (energyBaseConsumption - p.i) * electricityPrice;

  der(totalCost) = cost;

  der(totalElectricityUsage) = energyBaseConsumption - p.i;

  when sample(0, 1) then

      h = pre(h) + 1;

  end when;

end ElectricitySource;

 
 
When I compile the model I get the following error. 
 

Error: in file 'ElectricitySource.mo':

Semantic error at line 12, column 43:

  Array index in equation must be constant, parameter or loop index: h

 

I read in the docs that JModelica currently have some limitatations regarding arrays and I assume that this is one such limitation. But if anyone has a sugestions on how to get arround the problem I would be very greatful.

 

Just to be explicit, what I want to do is to specify the electricity price over 24h and make the model use the electricity price for the corresponding hour, i.e. assign it to the variable electricityPrice. I could also imagine to put the price plan outside the source and have the model reading some external file, if that would solve the problem. 

 
 

tove
Offline
Joined: 2009-04-08
Hi, The error you are getting

Hi,

The error you are getting is because in the array access 

electricityPrice = electricityPricePlan[h]

h is a variable and not a constant or parameter. This is currently a limitation in JModelica.org. However, you can solve that by making a function instead, e.g.

function electricityPricePlan
 input Integer h;
 output Real electricityPrice;

protected
 Real[:] electricityPricePlan = {42,41,40,41,42,60,65,64,63,62,61,58,56,55,56,58,66,62,58,50,46,44,43,42};

algorithm
 electricityPrice := electricityPricePlan[h];

end electricityPricePlan;

and change the array access to

electricityPrice = electricityPricePlan(h);

 

Best regards,
Tove

billing
Offline
Joined: 2012-03-20
Thank you for the help, the

Thank you for the help, the function solution works fine!

Login or register to post comments