Field Runoff

Meant to simulate the runoff of fertilizer from a field into a river. there is a rainfall condition simulate additional water being added.

Published

No video preview yet

Source

property SoilContents {
    water: Float [0.0, 1.0] = 0.50;
    fertilizer: Float [0.0, 1.0] = 0.10;
}

property RiverTotals {
    water_received: Float = 0.0;
    fertilizer_received: Float = 0.0;
}

model Soil: SoilContents;
model River: RiverTotals;

world {
    dimensions = 3;
    neighborhood = VonNeumann;
}

input runoff_coefficient: Float [0.0, 1.0] = 0.20;

// Add one percentage point of water saturation per tick.
const rainfall_per_tick: Float = 0.00;

fn flow_fraction(
    water: Float,
    fertilizer: Float,
    capacity: Float,
    rate: Float
) -> Float {
    let saturation = clamp(water + fertilizer, 0.0, 1.0);

    return clamp(
        min(
            rate * saturation,
            capacity / max(saturation, 0.000000000001)
        ),
        0.0,
        1.0
    );
}

behavior Rainfall for SoilContents {
    update {
        let remaining_capacity = clamp(
            1.0 - self.water - self.fertilizer,
            0.0,
            1.0
        );

        let rain_added = min(rainfall_per_tick, remaining_capacity);

        set self.water = clamp(
            self.water + rain_added,
            0.0,
            1.0
        );
    }
}

behavior SoilRunoff for SoilContents
    needs inputs(runoff_coefficient: Float [0.0, 1.0]) {
    update {
        let upstream_water = neighbors[Soil, -Y].water else 0.0;
        let upstream_fertilizer = neighbors[Soil, -Y].fertilizer else 0.0;

        // In the supplied world, the final soil row borders the river.
        let downstream_water = neighbors[Soil, +Y].water else 0.0;
        let downstream_fertilizer = neighbors[Soil, +Y].fertilizer else 0.0;

        let own_capacity = clamp(
            1.0 - self.water - self.fertilizer,
            0.0,
            1.0
        );

        let downstream_capacity = clamp(
            1.0 - downstream_water - downstream_fertilizer,
            0.0,
            1.0
        );

        let incoming_fraction = flow_fraction(
            upstream_water,
            upstream_fertilizer,
            own_capacity,
            runoff_coefficient
        );

        let outgoing_fraction = flow_fraction(
            self.water,
            self.fertilizer,
            downstream_capacity,
            runoff_coefficient
        );

        set self.water = clamp(
            self.water * (1.0 - outgoing_fraction)
                + upstream_water * incoming_fraction,
            0.0,
            1.0
        );

        set self.fertilizer = clamp(
            self.fertilizer * (1.0 - outgoing_fraction)
                + upstream_fertilizer * incoming_fraction,
            0.0,
            1.0
        );
    }
}

behavior CollectRunoff for RiverTotals
    needs inputs(runoff_coefficient: Float [0.0, 1.0]) {
    update {
        let upstream_water = neighbors[Soil, -Y].water else 0.0;
        let upstream_fertilizer = neighbors[Soil, -Y].fertilizer else 0.0;

        let incoming_fraction = flow_fraction(
            upstream_water,
            upstream_fertilizer,
            1.0,
            runoff_coefficient
        );

        set self.water_received =
            self.water_received + upstream_water * incoming_fraction;

        set self.fertilizer_received =
            self.fertilizer_received + upstream_fertilizer * incoming_fraction;
    }
}

// Rain commits first; runoff sees the increased saturation.
phase rain {
    run Rainfall on Soil;
}

phase runoff {
    run SoilRunoff on Soil;
    run CollectRunoff on River;
}

visual SoilColor for SoilContents {
    color {
        rgba(
            0.25 + self.fertilizer * 0.65,
            0.16 + self.fertilizer * 0.45,
            0.08 + self.water * 0.65,
            1.0
        )
    }
}

visual RiverColor for RiverTotals {
    color {
        let pollution = clamp(
            self.fertilizer_received / 6.3,
            0.0,
            1.0
        );

        rgba(
            0.05 + pollution * 0.55,
            0.35 + pollution * 0.30,
            0.85 - pollution * 0.65,
            1.0
        )
    }
}

visualize Soil with SoilColor;
visualize River with RiverColor;