<jupyter_start><jupyter_text>HIV Model _<NAME>_<jupyter_code># Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

import sys
modsim_path = !echo $(pwd)/..
sys.path.insert(0, modsim_path[0])

# import functions from the modsim.py module
from modsim import *
state = State(R=200, L=0, E=0, V=100)
system = System(init = state, gamma=1.36, mu=1.36e-3, tau=0.2, beta=0.00027, p=0.1, alpha=3.6e-2, sigma=2, \
                delta=0.33, pi=100, t0=0, t_end = 100, dt = 0.1)
def update_func(state, dt, system):
    """Update the HIV model.
    
    state: State (R, L, E, V)
    t: time
    system: System object
    
    returns: State (hiv)
    """
    R, L, E, V = state
    unpack(system)

    dR_dt = gamma*tau - mu*R - beta*R*V
    dL_dt = p*beta*R*V - mu*L - alpha*L
    dE_dt = (1-p)*beta*R*V + alpha*L -delta*E
    dV_dt = pi*E - sigma*V
    
    R = R + dR_dt*dt
    L = L + dL_dt*dt
    E = E + dE_dt*dt
    V = V + dV_dt*dt
    
    return State(R=R, L=L, E=E, V=V)
def run_simulation(system, update_func):
    """Runs a simulation of the system.
        
    system: System object
    update_func: function that updates state
    
    returns: TimeFrame
    """
    
    frame = TimeFrame(columns=system.init.index)
    frame.row[system.t0] = system.init
    
    timesteps = pd.Series(np.linspace(system.t0, system.t_end, int(system.t_end/system.dt)))
    
    for t in timesteps.index[:-1]:
        frame.row[timesteps[t+1]] = update_func(frame.row[timesteps[t]], system.dt, system)
    
    return frame
res = run_simulation(system, update_func)
res.V.plot()
res.L.plot()
res.E.plot()
res.R.plot()
plt.legend(['L', 'E', 'R'])<jupyter_output><empty_output><jupyter_text>A Better Way to Model Compared to modeling in Python, where both the physical and mathematical (programmatic) aspects of modeling must be considered, there exists a DSL where only the physical equations of the system is needed. This language is **Modelica**. It is an object-oriented, component-based modeling language where physical systems are represented purely by their constituent equations; the actual method of solving is left to the tool, conveniently abstracting out the modeling process to only the physics aspect for the modeler. To illustrate the ease with which one can model systems using such a language, the HIV model above is rewritten into the Modelica language; if one were to run this in a Modelica tool, such as the open source OpenModelica, this code would produce the same results as seen above.<jupyter_code>######## You can't actually run this in Python... :'(

within code.HIV;

model HIVModel
  // Parameters
  parameter Real gamma = 1.36;
  parameter Real tau = 0.2;
  parameter Real mu = 1.36e-3;
  parameter Real beta = 0.00027;
  parameter Real rho = 0.1;
  parameter Real alpha = 3.6e-2;
  parameter Real sigma = 2;
  parameter Real delta = 0.33;
  parameter Real pi = 100;
  
  // Initial values
  parameter Real R_start = 200;
  parameter Real L_start = 0;
  parameter Real E_start = 0;
  parameter Real V_start = 100;
  
  // Stocks
  Real R;
  Real L;
  Real E;
  Real V;
  Real total_lymph;
initial equation
  R = R_start;
  L = L_start;
  E = E_start;
  V = V_start;
equation
  der(R) = gamma*tau - mu*R - beta*R*V;
  der(L) = rho*beta*R*V - mu*L - alpha*L;
  der(E) = (1-rho)*beta*R*V + alpha*L - delta*E;
  der(V) = pi*E - sigma*V;
  total_lymph = 1000*(1-tau) + R + L + E;
end HIVModel;<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11589772095608672, 'fraction_numerical': 0.02501389660922735, 'mean_word_length': 3.351874244256348, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '221503', 'license': 'MIT', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1262, 'n_words': 411, 'path': 'code/HIV/hiv_model.ipynb', 'repo_name': 'SSModelGit/ModSimPy', 'size': '3612'}