Skip to content

Commit

Permalink
* Add writing capability and hand over further variables to subsystems,
Browse files Browse the repository at this point in the history
  • Loading branch information
MBaranskiEBC committed May 1, 2019
1 parent c358446 commit bc76d40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
44 changes: 26 additions & 18 deletions pyDMPC/ControlFramework/Subsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def __init__(self, name, position,no_parallel,holon,
bounds_DVs,start_DVs,factor_DVs,
model_path, names_BCs, variation,
num_VarsOut, names_DVs,
output_vars, initial_names, IDs_initial_values,
IDs_inputs,cost_par,cost_factor,model_type,type_subSyst=None):
output_vars, initial_names, IDs_initial_values, IDs_initial_offsets,
IDs_inputs,cost_par,cost_factor,model_type,pred_hor,
type_subSyst=None):
self._name = name
self._type_subSyst = type_subSyst
self._num_DVs = num_DVs
Expand All @@ -46,15 +47,17 @@ def __init__(self, name, position,no_parallel,holon,
self._output_vars = output_vars
self._initial_names = initial_names
self._IDs_initial_values = IDs_initial_values
self._IDs_initial_offsets = IDs_initial_offsets
self._IDs_inputs = IDs_inputs
self._model_type = model_type
self._pred_hor = pred_hor


def GetNeighbour(self, neighbour_name):
"""Gets the name of a subsystem's neighbor"""
self.neighbour_name = neighbour_name

def GetMeasurements(self, ids_list, model):
def GetMeasurements(self, ids_list, model, offsets=None):
"""
Gets measurements from FMU model
inputs:
Expand All @@ -65,12 +68,15 @@ def GetMeasurements(self, ids_list, model):
"""
values = []

for val in ids_list:
for j,val in enumerate(ids_list):
if Init.realtime:
value = 1
#value = model.read_by_name(val, pyads.PLCTYPE_REAL)
value = model.read_by_name(val, pyads.PLCTYPE_REAL)
print(val + str(value))
else:
value = np.asscalar(model.get(val)) #FMU
value = np.asscalar(model.get(val))

if offsets is not None:
value = value + offsets[j]

values.append(value)

Expand Down Expand Up @@ -118,30 +124,32 @@ def CalcDVvalues(self, time_step, time_storage, iter, model=None):
values: list of measurement values
"""
""" Get Measurements """
if self._name == "Hall-long":
if self._name == "Hall-long" or self._name == "Hall-short":
self.GetWeatherForcast()
print('Getting forecast')

if self._IDs_inputs is not None:
self.measurements = self.GetMeasurements(self._IDs_inputs, model)

# Save new 'CompleteInput.mat' File
sio.savemat((Init.path_res +'\\'+Init.name_wkdir + '\\' + self._name +
'\\' + 'CompleteInput.mat'), {'InputTable' :np.array(values)})
self.measurements[0] = self.CalcXfromRH(self.measurements[0]*100,

self.measurements[0] = self.CalcXfromRH(self.measurements[0],
self.measurements[1])
self.measurements = [self.measurements[0], self.measurements[1]]

values = np.concatenate(([0.0], self.measurements[::-1]),axis=0)

print(values)

# Save new 'CompleteInput.mat' File
sio.savemat((Init.path_res +'\\'+Init.name_wkdir + '\\' + self._name +
'\\' + 'CompleteInput.mat'), {'InputTable' :np.array(values)})

else:
self.measurements = []

if self._IDs_initial_values is not None:
self._initial_values = self.GetMeasurements(
self._IDs_initial_values, model)
self._IDs_initial_values, model, self._IDs_initial_offsets)
else:
self._initial_values = []

Expand Down Expand Up @@ -244,12 +252,12 @@ def CalcDVvalues(self, time_step, time_storage, iter, model=None):
if self._names_DVs is not None:

for j,val in enumerate(commands):
command2send = (self.start_DVs[j] +
val/100*self.factor_DVs[j])
command2send = float((self.start_DVs[j] +
val/100*self.factor_DVs[j]))
if Init.realtime:
print('ok')
#write_by_name(self._names_DVs[j], command2send,
# pyads.PLCTYPE_REAL)
print(self._names_DVs[j] + ": " + str(command2send))
model.write_by_name(self._names_DVs[j], command2send,
pyads.PLCTYPE_REAL)
else:
model.set(self._names_DVs[j], command2send)

Expand Down
2 changes: 2 additions & 0 deletions pyDMPC/ControlFramework/System.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def GenerateSubSys(self):
Init.output_vars[i],
Init.initial_names[i],
Init.IDs_initial_values[i],
Init.IDs_initial_offsets[i],
Init.IDs_inputs[i],
Init.cost_par[i],
Init.cost_factor[i],
Init.model_type[i],
Init.pred_hor[i],
Init.type_subSyst[i]
))
subsystems.sort(key = lambda x: x.position)
Expand Down

0 comments on commit bc76d40

Please sign in to comment.