ClimateMARGO / ClimateMARGO.jl

Julia implementation of MARGO, an idealized climate-economic modelling framework for Optimizing trade-offs between emissions Mitigation, Adaptation, carbon dioxide Removal, and solar Geoengineering.
https://margo.plutojl.org/
MIT License
67 stars 13 forks source link

Unit tests #3

Closed hdrake closed 4 years ago

hdrake commented 4 years ago

I should at least have some unit tests for the key functions.

hdrake commented 4 years ago

Added a first unit test as a doctest (docstring test) in PR https://github.com/hdrake/MARGO.jl/pull/5, which should be automatically tested by Travis Continuous Integration (CI) every time the PR is updated.

hdrake commented 4 years ago

As of https://github.com/hdrake/ClimateMARGO.jl/commit/102fdda79ecb0f11fbb6d8ab067202f1642118ae, there is now a high-level set of tests which run the temperature-constrainted optimization for 1.5, 2.0, 2.5, 3.0, 3.5, and 4.0ºC above pre-industrial. In each case, it checks that 1) the optimization solves successfully and 2) the result has a maximum temperature approximately equal to the goal temperature.

function temperature_optimization_works(name::String, temp_goal::Float64)
    config_path = "../configurations"
    model = ClimateModel(ClimateMARGO.IO.load_params(name, config_path=config_path))
    status = optimize_controls!(model, temp_goal=temp_goal)
    return (
        (raw_status(status) == "Solve_Succeeded") & 
        isapprox(
            maximum(T(model, M=true, R=true, G=true, A=true)),
            temp_goal,
            rtol=1.e-5
        )
    )
end

function tests()
    @testset "Subset of tests" begin
        for temp_goal in 1.5:0.5:4.0
            @test temperature_optimization_works("default", temp_goal)
        end
    end
end