JH-LandHydroLab / mesas

MIT License
2 stars 4 forks source link

IndexError when visualizing transport column #13

Open LutzKl opened 2 weeks ago

LutzKl commented 2 weeks ago

Hi Ciaran,

I am having some issues with the visualization of the transport column as described in the Hydrolearn course in 2.2 "Visualizing results with the transport column".

I am running the following script:

import os
os.chdir(r'C:\path')
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

timeseries_duration = 1.
timeseries_length = 100
dt = timeseries_duration / timeseries_length

pulse_start = 0.05
pulse_end = 0.15

C_tracer_input = 0.5
Q_steady = 1.
Storage_vol = 3

data_df = pd.DataFrame(index=np.arange(timeseries_length) * dt)
data_df['Q out [vol/time]'] = Q_steady
data_df['J in [vol/time]'] = 0.5
data_df['C [conc]'] = 0
data_df['Storage_vol'] = Storage_vol

data_df.loc[(data_df.index >= pulse_start) & (data_df.index <= pulse_end), 'C [conc]'] = C_tracer_input

data_df.to_csv('data.csv')
data_df.plot()
plt.show()

from mesas.sas.model import Model
model = Model(data_df='data.csv', config='config.json')
model.run()
model.data_df.to_csv('results.csv')

data_df = model.data_df
import matplotlib.pyplot as plt
plt.plot(data_df.index, data_df['C [conc]'])
plt.plot(data_df.index, data_df['C [conc] --> Q out [vol/time]'])
plt.show()

from mesas.utils import vis

print(model.result)

fig=plt.figure(figsize=[18,6])
i = 10
vis.plot_transport_column_with_timeseries(model, flux='Q out [vol/time]', sol='C [conc]', fig=fig, i=i, ST_max=Storage_vol);
plt.show()

This results in the error:

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\SAS_course\Course_Mesas.py", line 48, in <module>
    vis.plot_transport_column_with_timeseries(model, flux='Q out [vol/time]', sol='C [conc]', fig=fig, i=i, ST_max=Storage_vol);
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\Lib\site-packages\mesas\utils\vis.py", line 247, in plot_transport_column_with_timeseries
    plot_transport_column(model, flux, sol, i=i, ax=axTC, artists_dict=artists_dict, **kwargs)
  File "C:\Users\User\anaconda3\Lib\site-packages\mesas\utils\vis.py", line 17, in plot_transport_column
    sTs = np.r_[0, model.result['sT'][:-1, i]]
                   ~~~~~~~~~~~~~~~~~~^^^^^^^^
IndexError: index 10 is out of bounds for axis 1 with size 2

Do you have a fix for this? I tried running it in the juypterhub but got the same error. It will give me a plot of sorts if i = 0 but then it doesn't have any informational content.

Thank you!

charman2 commented 2 weeks ago

Try adding "record_state":true to your options in config.json. See documentation here: https://mesas.readthedocs.io/en/latest/options.html

The record_state option was added recently. It stops the code from retaining large arrays containing the solution at intermediate timesteps. This saves on memory demands. Perhaps I should have made the default value True, but I think for now the best long-term fix is to put a check in vis so it complains if record_state=False and provides some useful feedback. I'll close this issue when that's done.

LutzKl commented 1 week ago

That solved the issue. Thank you!