# policyanalysisinc/heromod2 architecture

> Architecture diagram of the GitHub repository policyanalysisinc/heromod2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/policyanalysisinc/heromod2
- Repository: https://github.com/policyanalysisinc/heromod2

## Overview

##### JSON Input Reception and Parsing
- Node Label: “Receive JSON Input”
- Description: The workflow starts when the user inputs a Markov model in JSON format. This JSON is parsed into an R object. (Assumed to use a JSON library call such as jsonlite::fromJSON, though this step might be wrapped in a helper function within heRomod2.)

##### Input Validation and Pre-processing
- Node Label: “Validate & Pre-process Input”
- Functions Involved: A utility (possibly in R/error.r) ensures that the JSON adheres to the expected schema. This error-checking step validates structure and content.

##### Initialization of Model Components
- Node Label: “Initialize Model Components”
- Functions Involved:
- R/constants.r: Loads any constant values used across the simulation.
- R/variables.r: Sets up the model’s variables (e.g., starting state probabilities, costs, utilities).
- Description: With valid input, initialization functions prepare the required settings and constant parameters.

##### Main Evaluation Function
- Node Label: “Evaluate Markov Model” (main routine)
- Functions Involved: R/heromod2.r likely contains the main orchestrator function (e.g., evaluate_markov_model or similar) that accepts the pre-processed input and commands the simulation.
- Description: The orchestrator method is responsible for driving the simulation cycles and coordinating other component functions.

##### Execution of the Simulation Loop
- Node Label: “Simulation Loop (Cycle Over Time)”
- Loop Details:
o For each cycle, the following sub-steps occur:
1. State Transition Calculation:
– Function: R/formula.r (function such as compute_transition might be defined here)
– Description: Calculate the probabilities of transitioning between states using the input formulas.
2. Variable Update:
– Function: R/variables.r (could be updating state counters, accumulators for costs/effects)
– Description: Updates the model’s state based on computed transitions.
3. Error Handling within Cycle:
– Function: R/error.r (if any unexpected computation or data is encountered, this function is called)
– Description: Validates the integrity of calculations; if errors arise, the error handling routine is activated.
4. Optional: Additional computations using R/misc.r may be performed to manage any auxiliary calculations.

##### Final Computation and Output Generation
- Node Label: “Summarize & Generate Output”
- Description: Once all cycles are processed, final metrics (e.g., total costs, QALYs) are aggregated.
- Functions Involved: This step may aggregate outputs using functions from R/heromod2.r or other utility functions. Final validations or formatting may be executed here.

##### Optional Logging and Reporting
- Node Label: “Logging / Reporting”
- Description: Although not explicitly shown, the package may include hooks for logging progress or reporting intermediate results.
- This could be drawn as a side-process branching from the main simulation loop.

- Input/Output nodes (e.g., JSON input, final output) could use a rounded rectangle.
- Core processing routines (e.g., Evaluate Markov Model, Simulation Loop) could use rectangles.
- That each step is as granular as possible: specify “Parse JSON”, “Validate Input via error.r”, “Setup Constants via constants.r”, “Compute Transitions via formula.r”, “Update Variables via variables.r”, etc.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Nodes
    jsonInput["Receive JSON Input"]:::input
    parseJSON["Parse JSON Input"]:::process
    validateInput["Validate & Pre-process Input (via error.r)"]:::validation
    loadConstants["Load Constants (via constants.r)"]:::process
    setupVariables["Setup Variables (via variables.r)"]:::process
    evaluate["Evaluate Markov Model (via herbomod2.r)"]:::process
    simulationLoop["Simulation Loop (Cycle Over Time)"]:::loop
    transitionCalc["Transition Calculation (via formula.r)"]:::process
    updateState["Update State Variables (via variables.r)"]:::process
    errorLoop["Error Handling (via error.r)"]:::validation
    auxiliary["Auxiliary Computations (via misc.r)"]:::process
    generateOutput["Summarize & Generate Output (via herbomod2.r)"]:::input
    logging["Logging / Reporting"]:::optional

    %% Main Flow
    jsonInput -->|"input_flows"| parseJSON
    parseJSON -->|"parsed_JSON"| validateInput
    validateInput -->|"valid_input"| loadConstants
    loadConstants -->|"constants_loaded"| setupVariables
    setupVariables -->|"initialization_complete"| evaluate
    evaluate -->|"start_simulation"| simulationLoop

    %% Simulation Loop Substeps
    simulationLoop -->|"cycle_start"| transitionCalc
    transitionCalc -->|"compute_transitions"| updateState
    transitionCalc -->|"error_check"| errorLoop
    updateState -->|"error_check"| errorLoop
    updateState -->|"aux_calc"| auxiliary
    auxiliary -->|"cycle_complete"| simulationLoop
    updateState -->|"next_cycle"| simulationLoop

    %% Exit from Simulation Loop
    simulationLoop -->|"if_simulation_complete"| generateOutput

    %% Optional Logging branch
    simulationLoop -- "log_progress" --> logging
    logging -->|"back_to_loop"| simulationLoop

    %% Click Events
    click validateInput "https://github.com/policyanalysisinc/heromod2/blob/main/R/error.r"
    click loadConstants "https://github.com/policyanalysisinc/heromod2/blob/main/R/constants.r"
    click setupVariables "https://github.com/policyanalysisinc/heromod2/blob/main/R/variables.r"
    click evaluate "https://github.com/policyanalysisinc/heromod2/blob/main/R/heromod2.r"
    click transitionCalc "https://github.com/policyanalysisinc/heromod2/blob/main/R/formula.r"
    click updateState "https://github.com/policyanalysisinc/heromod2/blob/main/R/variables.r"
    click errorLoop "https://github.com/policyanalysisinc/heromod2/blob/main/R/error.r"
    click auxiliary "https://github.com/policyanalysisinc/heromod2/blob/main/R/misc.r"
    click generateOutput "https://github.com/policyanalysisinc/heromod2/blob/main/R/heromod2.r"

    %% Classes / Styles
    classDef input fill:#d9ead3,stroke:#38761d,stroke-width:2px;
    classDef process fill:#cfe2f3,stroke:#1155cc,stroke-width:2px;
    classDef validation fill:#fff2cc,stroke:#bf9000,stroke-width:2px,stroke-dasharray: 5, 5;
    classDef loop fill:#ead1dc,stroke:#b31b1b,stroke-width:2px;
    classDef optional fill:#f4cccc,stroke:#cc0000,stroke-width:2px,stroke-dasharray: 5, 5;
```
