# joaomacalos/sfcr architecture

> Architecture diagram of the GitHub repository joaomacalos/sfcr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/joaomacalos/sfcr
- Repository: https://github.com/joaomacalos/sfcr

## Overview

sfcr is an R package for defining and simulating stock-flow consistent economic models. Its documented main workflow is to define equations and exogenous variables with sfcr_set(), run a baseline simulation, define a shock, and apply it to the baseline with sfcr_scenario() to obtain a tibble of results. The package tree also exposes solver, model-structure, multi-model, portfolio, and visualization capabilities. Source excerpts cover only the native matrix inversion; links among those other subsystems are omitted where their call paths are not shown.

Main entry points: [`R/sfcr_scenario.R`](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_scenario.R) (Scenario simulation), [`R/sfcr_baseline.R`](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_baseline.R) (Baseline simulation), [`R/sfcr_set.R`](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_set.R) (Equation sets), [`R/sfcr_shock.R`](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_shock.R) (Shock definition)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_modeling["Model definition"]
  node_sets["Equation sets<br/>[sfcr_set.R]"]
  node_expand["Model expansion<br/>[sfcr_expand.R]"]
  node_validation["Validation<br/>[sfcr_validate.R]"]
  node_matrix["Model matrix<br/>[sfcr_matrix.R]"]
end

subgraph group_simulation["Simulation"]
  node_baseline["Baseline simulation<br/>[sfcr_baseline.R]"]
  node_solvers["Numerical solvers<br/>[utils-solvers.R]"]
  node_native_solver["Matrix inversion<br/>[code.cpp]"]
  node_scenario["Scenario simulation<br/>[sfcr_scenario.R]"]
  node_shock["Shock definition<br/>[sfcr_shock.R]"]
  node_random["Random variation<br/>[sfcr_set.R]"]
end

subgraph group_analysis["Model analysis"]
  node_multis["Multiple models<br/>[sfcr_multis.R]"]
  node_portfolio["Portfolio analysis<br/>[sfcr_portfolio.R]"]
  node_dag["Dependency graphs<br/>[sfcr_find_order.R]"]
end

subgraph group_output["Results and visualization"]
  node_results["Simulation results<br/>[sfcr_baseline.R]"]
  node_matrix_display["Matrix display"]
  node_plots["Plots<br/>[sfcr_plots.R]"]
  node_sankey["Sankey diagrams<br/>[sfcr_sankey.R]"]
end

node_modeler(("Modeler"))

node_modeler -->|"defines equations"| node_sets
node_modeler -->|"runs baseline"| node_baseline
node_baseline -->|"returns tibble"| node_results
node_modeler -->|"defines shock"| node_shock
node_modeler -->|"runs scenario"| node_scenario
node_scenario -->|"returns tibble"| node_results
node_scenario -->|"uses baseline"| node_baseline
node_scenario -->|"applies shock"| node_shock
node_sets -->|"supports random"| node_random
node_solvers -.->|"may invoke"| node_native_solver

click node_sets "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_set.R"
click node_expand "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_expand.R"
click node_validation "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_validate.R"
click node_matrix "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_matrix.R"
click node_baseline "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_baseline.R"
click node_solvers "https://github.com/joaomacalos/sfcr/blob/main/R/utils-solvers.R"
click node_native_solver "https://github.com/joaomacalos/sfcr/blob/main/src/code.cpp"
click node_scenario "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_scenario.R"
click node_shock "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_shock.R"
click node_random "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_set.R"
click node_multis "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_multis.R"
click node_portfolio "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_portfolio.R"
click node_dag "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_find_order.R"
click node_results "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_baseline.R"
click node_matrix_display "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_matrix_display.R"
click node_plots "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_plots.R"
click node_sankey "https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_sankey.R"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_sets,node_expand,node_validation,node_matrix toneBlue
class node_baseline,node_solvers,node_native_solver,node_scenario,node_shock,node_random toneAmber
class node_multis,node_portfolio,node_dag toneMint
class node_results,node_matrix_display,node_plots,node_sankey toneRose
class node_modeler toneIndigo
```

## Components

### Model definition

- [Equation sets](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_set.R): `R/sfcr_set.R`
- [Model expansion](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_expand.R): `R/sfcr_expand.R`
- [Validation](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_validate.R): `R/sfcr_validate.R`
- [Model matrix](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_matrix.R): `R/sfcr_matrix.R`

### Simulation

- [Baseline simulation](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_baseline.R): `R/sfcr_baseline.R`
- [Numerical solvers](https://github.com/joaomacalos/sfcr/blob/main/R/utils-solvers.R): `R/utils-solvers.R`
- [Matrix inversion](https://github.com/joaomacalos/sfcr/blob/main/src/code.cpp): `src/code.cpp`
- [Scenario simulation](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_scenario.R): `R/sfcr_scenario.R`
- [Shock definition](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_shock.R): `R/sfcr_shock.R`
- [Random variation](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_set.R): `R/sfcr_set.R`

### Model analysis

- [Multiple models](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_multis.R): `R/sfcr_multis.R`
- [Portfolio analysis](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_portfolio.R): `R/sfcr_portfolio.R`
- [Dependency graphs](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_find_order.R): `R/sfcr_find_order.R`

### Results and visualization

- [Simulation results](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_baseline.R): `R/sfcr_baseline.R`
- [Matrix display](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_matrix_display.R): `R/sfcr_matrix_display.R`
- [Plots](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_plots.R): `R/sfcr_plots.R`
- [Sankey diagrams](https://github.com/joaomacalos/sfcr/blob/main/R/sfcr_sankey.R): `R/sfcr_sankey.R`

### Other components

- **Modeler**

## Connections

- Modeler → Equation sets: defines equations (evidence: `README.md`)
- Modeler → Baseline simulation: runs baseline (evidence: `README.md`)
- Baseline simulation → Simulation results: returns tibble (evidence: `README.md`)
- Modeler → Shock definition: defines shock (evidence: `README.md`)
- Modeler → Scenario simulation: runs scenario (evidence: `README.md`)
- Scenario simulation → Simulation results: returns tibble (evidence: `README.md`)
- Scenario simulation → Baseline simulation: uses baseline (evidence: `README.md`)
- Scenario simulation → Shock definition: applies shock (evidence: `README.md`)
- Equation sets → Random variation: supports random (evidence: `README.md`)
- Numerical solvers → Matrix inversion: may invoke
