> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mangrovesystems.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Module 2 Exercise: Build a Multi-Step Calculation Pipeline

> Add two new event types and expand your model with a 5-step calculation pipeline that processes feedstock, lab analysis, and delivery data into net carbon output.

<Panel>
  <Card title="Accounting Academy" className="accounting-academy-card">
    <div className="accounting-academy-content">
      <Steps className="my-0">
        <Step title="MODULE 1: Fundamentals" icon="bookmark" iconType="solid" stepNumber={1} titleSize="p">
          * [1.1: Core Concepts & the Data Lifecycle](/accounting-academy/module-1/1-1-core-concepts-data-lifecycle)
          * [1.2: Translating Methodologies to Mangrove](/accounting-academy/module-1/1-2-translating-methodologies-to-mangrove)
          * [1.3: Events and Datapoints](/accounting-academy/module-1/1-3-events-and-datapoints)
          * [Exercise: Design and Build](/accounting-academy/module-1/1-exercise-design-and-build)
        </Step>

        <Step title="MODULE 2: Calculations" icon="bookmark" iconType="solid" stepNumber={2} titleSize="p">
          * [2.1: Calculation Architecture](/accounting-academy/module-2/2-1-calculation-architecture)
          * [2.2: Expanding Your Data Model](/accounting-academy/module-2/2-2-expanding-your-data-model)
          * [Exercise: Multi-Step Calculations](/accounting-academy/module-2/2-exercise-multi-step-calculations)
        </Step>

        <Step title="MODULE 3: Batch Partitioning & LCA" icon="bookmark" iconType="solid" stepNumber={3} titleSize="p">
          * [3.1: What Is a Batch?](/accounting-academy/module-3/3-1-what-is-a-batch)
          * [3.2: Batch Partitioning Strategies](/accounting-academy/module-3/3-2-batch-partitioning)
          * [3.3: Life Cycle Assessment & Emissions](/accounting-academy/module-3/3-3-lca-and-emissions)
          * [3.4: Allocating Emissions to Batches](/accounting-academy/module-3/3-4-allocating-emissions-to-batches)
          * [Exercise: Batch Partitioning & LCA](/accounting-academy/module-3/3-exercise-batch-partitioning-and-lca)
        </Step>

        <Step title="MODULE 4: Mass Balance with Ledgers" icon="bookmark" iconType="solid" stepNumber={4} titleSize="p">
          * [4.1: Introduction to Mass Balance](/accounting-academy/module-4/4-1-intro-to-mass-balance)
          * [4.2: Designing the Ledger Structure](/accounting-academy/module-4/4-2-designing-the-ledger-structure)
          * [4.3: Multiple Batch Types for Different Stages](/accounting-academy/module-4/4-3-multiple-batch-types)
          * [Exercise: Design & Build Mass Balance](/accounting-academy/module-4/4-exercise-design-and-build-mass-balance)
        </Step>

        <Step title="MODULE 5: Integration Testing" icon="bookmark" iconType="solid" stepNumber={5} titleSize="p">
          * [5.1: The Complete Model Assembly](/accounting-academy/module-5/5-1-the-complete-model-assembly)
          * [5.2: Testing Patterns](/accounting-academy/module-5/5-2-testing-patterns)
          * [5.3: Common Integration Pitfalls](/accounting-academy/module-5/5-3-common-integration-pitfalls)
          * [Exercise: Integration Testing](/accounting-academy/module-5/5-exercise-integration-testing)
        </Step>

        <Step title="MODULE 6: Advanced Patterns & Production" icon="bookmark" iconType="solid" stepNumber={6} titleSize="p">
          * [6.1: Model Design Best Practices](/accounting-academy/module-6/6-1-model-design-best-practices)
          * [6.2: Advanced Techniques](/accounting-academy/module-6/6-2-advanced-techniques)
          * [6.3: Production Readiness](/accounting-academy/module-6/6-3-production-readiness)
          * [Exercise: Production Deployment](/accounting-academy/module-6/6-exercise-production-deployment)
        </Step>
      </Steps>
    </div>
  </Card>
</Panel>

<Check>
  **What you'll build in this exercise:**

  * Add 2 new event types (Feedstock Receipt, Lab Analysis) with appropriate datapoints
  * Expand your model to a 5-step calculation pipeline with intermediate outputs
  * Test the pipeline with varied data and verify intermediate and final results
</Check>

This exercise expands the single-formula model you built in Module 1 into a **multi-step calculation pipeline** that processes data from three event types. By the end, your model will calculate net carbon sequestered through 5 distinct intermediate steps.

```mermaid theme={null}
flowchart LR
    subgraph Inputs["3 Event Types"]
        FR["Feedstock<br/>Receipt"]
        LA["Lab<br/>Analysis"]
        BD["Biochar<br/>Delivery"]
    end

    subgraph Pipeline["5-Step Pipeline"]
        S1["1. Dry Mass"]
        S2["2. Carbon<br/>Content"]
        S3["3. Biochar<br/>Yield"]
        S4["4. Permanence<br/>Factor"]
        S5["5. Net Carbon<br/>(tCO2e)"]
        S1 --> S2 --> S3 --> S5
        S4 --> S5
    end

    FR --> S1
    LA --> S2
    LA --> S4
    BD --> S3

    style FR fill:#42a5f5,stroke:#1976d2,stroke-width:2px,color:#000
    style LA fill:#42a5f5,stroke:#1976d2,stroke-width:2px,color:#000
    style BD fill:#42a5f5,stroke:#1976d2,stroke-width:2px,color:#000
    style S1 fill:#66bb6a,stroke:#388e3c,stroke-width:2px,color:#000
    style S2 fill:#66bb6a,stroke:#388e3c,stroke-width:2px,color:#000
    style S3 fill:#66bb6a,stroke:#388e3c,stroke-width:2px,color:#000
    style S4 fill:#ffa726,stroke:#f57c00,stroke-width:2px,color:#000
    style S5 fill:#ef5350,stroke:#c62828,stroke-width:2px,color:#000
```

**Prerequisites:** Complete [Lesson 2.1: Calculation Architecture](/accounting-academy/module-2/2-1-calculation-architecture) and [Lesson 2.2: Expanding Your Data Model](/accounting-academy/module-2/2-2-expanding-your-data-model). Ensure your Mangrove Biochar training project has the Biochar Delivery event type and simple model from Module 1.

***

## Part 1: Design (30 min)

Before building in Mangrove, design the expanded model on paper or in a spreadsheet.

### 1. Identify additional event types

Your project needs data from two new operational activities:

<CardGroup cols={2}>
  <Card icon="truck-loading" title="Feedstock Receipt">
    What goes into the pyrolysis process.

    * Feedstock type (text)
    * Wet mass in tonnes (number)
    * Moisture content as % (number)
    * Receipt date (date)
  </Card>

  <Card icon="flask" title="Lab Analysis">
    Quality testing of produced biochar.

    * Sample ID (text)
    * Carbon content as % (number)
    * H:C molar ratio (number)
    * Analysis date (date)
  </Card>
</CardGroup>

### 2. Map the 5-step calculation flow

For each step, identify the inputs and the formula:

| Step | Calculation              | Inputs                                      | Formula                                              |
| ---- | ------------------------ | ------------------------------------------- | ---------------------------------------------------- |
| 1    | Dry feedstock mass       | wet\_mass, moisture\_content                | `wet_mass × (1 - moisture/100)`                      |
| 2    | Feedstock carbon content | dry\_mass, carbon\_pct                      | `dry_mass × (carbon_pct / 100)`                      |
| 3    | Biochar yield            | feedstock\_carbon, biochar\_mass            | `biochar_mass / feedstock_carbon` (efficiency ratio) |
| 4    | Permanence factor        | hc\_ratio                                   | If H:C \< 0.4 → 0.9; \< 0.7 → 0.8; else → 0.7        |
| 5    | Net carbon sequestered   | feedstock\_carbon, permanence, factor\_3.67 | `feedstock_carbon × permanence × 3.67`               |

### 3. Identify validation rules

Think about what values should be flagged or rejected:

* Moisture content must be between 0% and 100%
* Carbon content must be between 0% and 100%
* H:C ratio for biochar is typically 0.1–1.0
* Wet mass must be positive

***

## Part 2: Build in Mangrove (1.5 hrs)

### Step 1: Add 2 new event types

1. Open your Mangrove Biochar project and go to **Data Inputs > Input Settings**.
2. Add a **Feedstock Receipt** event type (slug: `feedstock-receipt`) with 4 datapoints:

| Datapoint            | Slug                   | Value Type |
| -------------------- | ---------------------- | ---------- |
| Feedstock type       | `feedstock-type`       | text       |
| Wet mass (tonnes)    | `wet-mass-tonnes`      | number     |
| Moisture content (%) | `moisture-content-pct` | number     |
| Receipt date         | `receipt-date`         | date       |

3. Add a **Lab Analysis** event type (slug: `lab-analysis`) with 4 datapoints:

| Datapoint          | Slug                 | Value Type |
| ------------------ | -------------------- | ---------- |
| Sample ID          | `sample-id`          | text       |
| Carbon content (%) | `carbon-content-pct` | number     |
| H:C molar ratio    | `hc-ratio`           | number     |
| Analysis date      | `analysis-date`      | date       |

See <a href="/production-accounting/data-inputs" target="_blank" rel="noopener noreferrer">configuring event types</a>

for details on setting up datapoints.

### Step 2: Expand your model with a 5-step pipeline

1. Open the **Models** section and the <a href="/production-accounting/models" target="_blank" rel="noopener noreferrer">Model Editor</a>

   .
2. Build the multi-step calculation tree:

<Steps>
  <Step title="Step 1: Dry feedstock mass">
    **Operator:** `product` **Children:**

    * Input node: `wet-mass-tonnes` (from Feedstock Receipt)
    * Calculated node: `1 - moisture/100` (use a Keisan expression or intermediate nodes)

    **Expected result:** If wet mass = 12 tonnes and moisture = 15%, dry mass = 12 × 0.85 = **10.2 tonnes**
  </Step>

  <Step title="Step 2: Feedstock carbon content">
    **Operator:** `product` **Children:**

    * Intermediate: dry feedstock mass (from Step 1)
    * Input node: `carbon-content-pct` (from Lab Analysis, divided by 100)

    **Expected result:** If dry mass = 10.2 tonnes and carbon = 80%, carbon mass = 10.2 × 0.80 = **8.16 tonnes C**
  </Step>

  <Step title="Step 3: Biochar yield efficiency">
    **Operator:** `quotient` **Children:**

    * Input node: `biochar-mass-tonnes` (from Biochar Delivery)
    * Intermediate: dry feedstock mass (from Step 1)

    **Expected result:** If biochar mass = 8 tonnes and dry feedstock = 10.2 tonnes, yield = 8 / 10.2 = **0.784** (78.4% yield)
  </Step>

  <Step title="Step 4: Permanence factor">
    **Operator:** `keisan` **Expression:** Conditional based on H:C ratio from Lab Analysis

    ```
    if hc_ratio < 0.4
      0.9
    elsif hc_ratio < 0.7
      0.8
    else
      0.7
    end
    ```

    **Expected result:** If H:C = 0.35, permanence factor = **0.9**
  </Step>

  <Step title="Step 5: Net carbon sequestered (tCO2e)">
    **Operator:** `product` **Children:**

    * Intermediate: feedstock carbon content (from Step 2)
    * Intermediate: permanence factor (from Step 4)
    * Constant: 3.67

    **Output datapoint type:** `net-carbon-tco2e`

    **Expected result:** 8.16 × 0.9 × 3.67 = **26.95 tCO2e**
  </Step>
</Steps>

<Note>
  The biochar yield (Step 3) is calculated for informational purposes — it doesn't feed directly into the net carbon calculation in this simplified model. You could use it in more advanced models to adjust carbon content based on conversion efficiency.
</Note>

### Step 3: Test with varied data

1. **Create test events** — <a href="/data-collection/add-data" target="_blank" rel="noopener noreferrer">add events</a>

   for each of the 3 event types with the following test data:

| Event Type        | Key Values                             |
| ----------------- | -------------------------------------- |
| Feedstock Receipt | wet mass = 12 t, moisture = 15%        |
| Lab Analysis      | carbon content = 80%, H:C ratio = 0.35 |
| Biochar Delivery  | mass = 8 t, carbon content = 80%       |

2. **Run the model** in the Console and verify each intermediate step:
   * Dry mass: 12 × 0.85 = **10.2 t**
   * Carbon mass: 10.2 × 0.80 = **8.16 t C**
   * Yield: 8 / 10.2 = **0.784**
   * Permanence: **0.9** (H:C \< 0.4)
   * Net carbon: 8.16 × 0.9 × 3.67 ≈ **26.95 tCO2e**
3. **Test with edge cases** — create additional events with:
   * High moisture (50%) — dry mass should drop significantly
   * High H:C ratio (0.75) — permanence should be 0.7 (lowest tier)
   * Different carbon content — verify proportional impact on output

<Warning>
  If intermediate values don't match expectations, check: (1) datapoint slugs match between event types and model nodes, (2) percentage values are divided by 100 where needed, (3) the Keisan expression handles all H:C ranges correctly.
</Warning>

***

## Success criteria

You have completed the Module 2 exercise when:

* **Two new event types** exist — Feedstock Receipt and Lab Analysis — each with appropriate datapoints in **Data Inputs > Input Settings**
* **Model expanded** to a 5-step pipeline with visible intermediate calculations
* **Intermediate outputs** are verifiable — dry mass, carbon content, yield, permanence factor all produce expected values
* **Final output** (net carbon in tCO2e) matches manual calculation for your test data
* **Edge cases tested** — varied moisture, H:C ratios, and carbon content produce correct adjusted results

***

## What's next

In Module 3 you'll learn about **batch partitioning** — how to split your operational data into discrete, reportable batches, and add **LCA emissions** calculations to compute net carbon removal. Continue to [Module 3: Batch Partitioning & LCA](/accounting-academy/module-3/3-1-what-is-a-batch).
