Skip to content

Commit

Permalink
return array for time in getMSXComputedQualityNode/Link - reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariosmsk committed Jun 24, 2024
1 parent 7146487 commit 901e7d5
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions epyt/epanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11252,7 +11252,8 @@ def loadMSXFile(self, msxname, customMSXlib=None, ignore_properties=False):
self.MSXFile = msxname[:-4]
self.MSXTempFile = msxname[:-4] + '_temp.msx'
copyfile(msxname, self.MSXTempFile)
self.msx = epanetmsxapi(self.MSXTempFile, customMSXlib=customMSXlib, display_msg=self.display_msg, msxrealfile = self.MSXFile)
self.msx = epanetmsxapi(self.MSXTempFile, customMSXlib=customMSXlib, display_msg=self.display_msg,
msxrealfile=self.MSXFile)

# Message to user if he uses ph with msx
if self.api._ph is not None:
Expand Down Expand Up @@ -11304,6 +11305,7 @@ def unloadMSX(self):
msx_temp_files = list(filter(lambda f: os.path.isfile(os.path.join(os.getcwd(), f))
and f.startswith("msx") and "." not in f, os.listdir(os.getcwd())))
safe_delete(msx_temp_files)
print('EPANET-MSX Toolkit is unloaded.')

def getMSXSpeciesCount(self):
""" Retrieves the number of species.
Expand Down Expand Up @@ -12417,6 +12419,10 @@ def changeMSXOptions(self, param, change):

self.msx.MSXclose()
copyfile(options_section, self.MSXTempFile)
try:
os.remove(options_section)
except:
pass
self.loadMSXEPANETFile(self.MSXTempFile)

def setMSXAreaUnitsCM2(self):
Expand Down Expand Up @@ -12829,8 +12835,6 @@ def getMSXComputedQualitySpecie(self, species=None, nodes=1, links=1):
else:
species_index_name = self.getMSXSpeciesIndex(species)



node_count = self.getNodeCount()
link_count = self.getLinkCount()
specie_count = len(species_index_name)
Expand Down Expand Up @@ -13011,23 +13015,21 @@ def getMSXComputedQualityNode(self, *args):
raise ValueError(
'Wrong species index. Please check the functions getMSXSpeciesNameID, getMSXSpeciesCount.')
k += 1

time_data.append(0)
# Run simulation steps and collect quality data
simulation_duration = self.getTimeSimulationDuration()
while tleft > 0 and t != simulation_duration:
t, tleft = self.stepMSXQualityAnalysisTimeLeft()
time_data.append(t)
for nl in ss:
for idx, j in enumerate(uu, start=1):

concentration = self.getMSXSpeciesConcentration(0, nl, j)
quality_data[nl][k , idx-1] = concentration
concentration = self.getMSXSpeciesConcentration(0, nl, j)
quality_data[nl][k, idx - 1] = concentration
k += 1


out = EpytValues()
out.Quality = quality_data
out.Time = time_data
out.Time = np.array(time_data)
return out

def getMSXComputedQualityLink(self, *args):
Expand Down Expand Up @@ -13060,7 +13062,7 @@ def getMSXComputedQualityLink(self, *args):

time_steps = int(self.getTimeSimulationDuration() / self.getMSXTimeStep()) + 1

quality_data = {node: np.zeros((time_steps, len(uu))) for node in ss}
quality_data = {link: np.zeros((time_steps, len(uu))) for link in ss}
time_data = []

k = 0
Expand Down Expand Up @@ -13089,7 +13091,7 @@ def getMSXComputedQualityLink(self, *args):

out = EpytValues()
out.Quality = quality_data
out.Time = time_data
out.Time = np.array(time_data)
return out

def setMSXLinkInitqualValue(self, value):
Expand Down Expand Up @@ -13190,6 +13192,7 @@ def initializeMSXWrite(self):
setattr(value, attr, {})

return value

def writeMSXFile(self, msx):
"""
Write a new MSX file
Expand Down Expand Up @@ -13261,7 +13264,6 @@ def writeMSXFile(self, msx):
for item in items:
f.write("\n{}".format(item))


f.write('\n\n[REPORT]\n')
f.write('NODES ALL\n')
f.write('LINKS ALL\n')
Expand All @@ -13284,7 +13286,7 @@ def setMSXPatternMatrix(self, pattern_matrix):
pattern_matrix = [[float(value) for value in row] for row in pattern_matrix]
nfactors = len(pattern_matrix[0])
for i, pattern in enumerate(pattern_matrix):
self.msx.MSXsetpattern(i+1, pattern, nfactors)
self.msx.MSXsetpattern(i + 1, pattern, nfactors)

def getAllAttributes(self, obj):
"""Get all attributes of a given Python object
Expand All @@ -13297,6 +13299,7 @@ def getAllAttributes(self, obj):
print(attr) #Will print Time, LinkQuality , NodeQuality and MassFlowRate
"""
attributes = []

def recurse_attrs(obj):
for k, v in obj.__dict__.items():
attributes.append((k, v))
Expand All @@ -13319,6 +13322,7 @@ def getMethods(self):
callable(getattr(self, method)) and not method.startswith('__') and not method.startswith('_')]
return methods_dir


class epanetapi:
"""
EPANET Toolkit functions - API
Expand Down Expand Up @@ -16172,7 +16176,8 @@ def ENwriteline(self, line):
class epanetmsxapi:
"""example msx = epanetmsxapi()"""

def __init__(self, msxfile='', loadlib=True, ignore_msxfile=False, customMSXlib=None, display_msg=True, msxrealfile = ''):
def __init__(self, msxfile='', loadlib=True, ignore_msxfile=False, customMSXlib=None, display_msg=True,
msxrealfile=''):
self.display_msg = display_msg
self.customMSXlib = customMSXlib
if customMSXlib is not None:
Expand Down Expand Up @@ -16623,7 +16628,7 @@ def MSXsetpattern(self, index, factors, nfactors):
the pattern
nfactors: the number of entries in the multiplier array/ vector factors"""
if isinstance(index, int):
index = c_int(index)
index = c_int(index)
nfactors = c_int(nfactors)
DoubleArray = c_double * len(factors)
mult_array = DoubleArray(*factors)
Expand Down

0 comments on commit 901e7d5

Please sign in to comment.