<jupyter_start><jupyter_text>Demo SingleRunThis notebook shows a single run consisting of the following four phases:* sail empty* loading* sail full* unloading<jupyter_code>import datetime, time
import simpy

import shapely.geometry

import pandas as pd
import openclsim.core as core
import openclsim.model as model
import openclsim.plot as plot
from plotly.offline import init_notebook_mode, iplot
import plotly.graph_objs as go
import openclsim.plugins as plugin
import numpy as np

init_notebook_mode(connected=True)

# setup environment
simulation_start  = datetime.datetime(2010, 1, 1, 0, 0)
my_env = simpy.Environment(initial_time=simulation_start.timestamp())
registry = {}
keep_resources = {}
Site = type(
    "Site",
    (
        core.Identifiable,
        core.Log,
        core.Locatable,
        core.HasContainer,
        core.HasResource,
    ),
    {},
)

TransportProcessingResource = type(
    "TransportProcessingResource",
    (
        core.Identifiable,
        core.Log,
        core.ContainerDependentMovable,
        core.Processor,
        core.HasResource,
    ),
    {},
)

TestMoveActivity =  type(
    "TestMoveActivity",
    (
        plugin.HasWeatherPluginActivity,
        model.MoveActivity,  # the order is critical!
    ),
    {},
)


TestShiftActivity =  type(
    "TestShiftActivity",
    (
        plugin.HasWeatherPluginActivity,
        model.ShiftAmountActivity,  # the order is critical!
    ),
    {},
)
location_from_site = shapely.geometry.Point(4.18055556, 52.18664444)  # lon, lat
location_to_site = shapely.geometry.Point(4.25222222, 52.11428333)  # lon, lat

from_site = Site(
    env=my_env,
    name="Winlocatie",
    geometry=location_from_site,
    capacity=120,
    level=120,
)
to_site = Site(
    env=my_env,
    name="Dumplocatie",
    geometry=location_to_site,
    capacity=120,
    level=0,
)
hopper = TransportProcessingResource(
    env=my_env,
    name="Hopper 01",
    geometry=location_from_site,
    capacity=4,
    compute_v=lambda x: 10,
)
metocean_df = pd.read_csv("../tests/data/unit_test_weather.csv")
metocean_df = metocean_df.set_index(pd.to_datetime(metocean_df["Time"], dayfirst=True))
metocean_df = metocean_df.sort_index()

metocean_df["Hs [m]"] = 4 + \
                        1.5*np.sin(metocean_df['  Hour']/24 * 8 *np.pi) + \
                        1.5*np.sin(metocean_df['  Hour']/24 * 6 *np.pi)

metocean_df = metocean_df.set_index(pd.to_datetime(metocean_df["Time"], dayfirst=True))
metocean_df = metocean_df.sort_index()
metocean_df["ts"] = metocean_df.index.values.astype(float) / 1_000_000_000
sailing_crit = plugin.WeatherCriterion(
    name="sailing_crit",
    condition="Hs [m]",
    maximum=6,
    window_length=3600,
)

loading_crit = plugin.WeatherCriterion(
    name="loading_crit",
    condition="Hs [m]",
    maximum=4.5,
    window_length=3600,
)

single_run = [
    TestMoveActivity(
        env=my_env,
        name="sailing empty",
        registry=registry,
        mover=hopper,
        destination=from_site,
        metocean_criteria=sailing_crit,
        metocean_df=metocean_df,
    ),
    TestShiftActivity(
        env=my_env,
        name="Loading",
        registry=registry,
        processor=hopper,
        origin=from_site,
        destination=hopper,
        amount=4,
        duration=3600,
        metocean_criteria=loading_crit,
        metocean_df=metocean_df,
    ),
    TestMoveActivity(
        env=my_env,
        name="sailing full",
        registry=registry,
        mover=hopper,
        destination=to_site,
        metocean_criteria=sailing_crit,
        metocean_df=metocean_df,
    ),
    TestShiftActivity(
        env=my_env,
        name="unloading",
        registry=registry,
        processor=hopper,
        origin=hopper,
        destination=to_site,
        amount=4,
        duration=3600,
        metocean_criteria=loading_crit,
        metocean_df=metocean_df,
    ),
]

activity = model.SequentialActivity(
    env=my_env,
    name="Single run process",
    ID="6dbbbdf7-4589-11e9-bf3b-b469212bff60",
    registry=registry,
    sub_processes=single_run,
)

while_activity = model.WhileActivity(
    env=my_env,
    name="while",
    registry=registry,
    sub_processes=[activity],
    condition_event=[{"type": "container", "concept": to_site, "state": "full"}],
)
model.register_processes([while_activity])
my_env.run()
plot.get_log_dataframe(while_activity, [while_activity, activity, *single_run]).head()
plot.get_log_dataframe(single_run[0], [while_activity, activity, *single_run]).head()
res = plot.vessel_planning([hopper], y_scale="numbers", static=True)
data = res['data']
layout = res['layout']

data.append({
    "name": "sailing_crit",
    "x":[hopper.log["Timestamp"][0].isoformat(), hopper.log["Timestamp"][-1].isoformat()],
    "y":[sailing_crit.maximum,sailing_crit.maximum],
})

data.append({
    "name": "Weather",
    "x":metocean_df.index,
    "y":metocean_df[sailing_crit.condition],
})



data.append({
    "name": "loading_crit",
    "x":[hopper.log["Timestamp"][0].isoformat(), hopper.log["Timestamp"][-1].isoformat()],
    "y":[loading_crit.maximum,loading_crit.maximum],
})

go.Figure(data=data, layout=layout)
fig = plot.get_step_chart([hopper, from_site, to_site])
plot.vessel_planning(single_run)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12615500660003773, 'fraction_numerical': 0.023760135772204413, 'mean_word_length': 3.4911092294665536, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '348818', 'license': 'MIT', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1829, 'n_words': 283, 'path': 'notebooks/18_SingleRun with weather.ipynb', 'repo_name': 'thijsreedijk/OpenCLSim', 'size': '5303'}