Talk:World3 nonrenewable resource sector

From Wikipedia, the free encyclopedia

Python program used to simulate the nonrenewable resource sector:

#!/usr/bin/env python

import math
from dynamo import *


#levels
nr = "nr" #Nonrenewable resources
ic = "ic" #Industrial Captial
nrtd = "nrtd" #Nonrenewable resource technology initiated
#nruf2_l1 = "nruf2_l1" #Nonrenewable resource usage rate delay level
#nruf2_l2 = "nruf2_l2" #Nonrenewable resource usage rate delay level
#nruf2_l3 = "nruf2_l3" #Nonrenewable resource usage rate delay level


#rates
nrur = "nrur" #Nonrenewabel resource usage rate
icir = "icir" #Industrial capital investment rate
icdr = "icdr" #Industrial capital depreciation rate
#nruf2_r1 = "nruf2_r1" #Nonrenewable resource usage rate delay rate
#nruf2_r2 = "nruf2_r2" #Nonrenewable resource usage rate delay rate
#nruf2_r3 = "nruf2_r3" #Nonrenewable resource usage rate delay rate
nrate = "nrate" #Nonrenewable resource technology improvement rate



#axillaries
nruf = "nruf" #Nonrenewable resource usage factor
pcrum = "pcrum" #Per capita resource use multiplier
nrfr = "nrfr" #Nonrenewable resource fraction remaining
fcaor ="fcaor" #Fraction of capital allocated to obtaining resources
fcaor1 = "fcaor1" #Normal fcaor
fcaor2 = "fcaor2" #Alternative fcaor
pop = "pop" #Population
pop1 = "pop1" #Exponentially growing pop
io = "io" #Industrial output
iopc = "iopc" #Industrial output percapita
nruf2 = "nruf2" #Nonrenewable resource usage factor after recycle year
nrcm = "nrcm" #Resource technological change multiplier
icor = "icor" #Industrial capital output ratio


#constants
nri = 1e12 #Nonrenewable resources initial
nruf1 = 1 #Nonrenewable resource usage factor 1
popi = 1.65e9 #Initial population in 1900
gc = 0.012 #Population growth constant
zpgt = 2500 #Zero population growth time
pop2 = popi*math.exp(gc*(zpgt-1900)) #population at zpgt
ici = 2.1e11 #Industrial capital initial
fioaa = 0.12 #Fraction of industrial output allocated to agriculture
fioas = 0.12 #Fraction of industrial output allocated to services
fioac = 0.43 #Fraction of industrial output allocated to consumption
alic = 14.0 #Average life of industrial capital
pyear = 1995 #Year to switch nruf
tdd=10 #Technological development and implementation delay
dnrur=2e9 #Desired nonrenewable resource usage rate

#parameters
dt = 1.0
initial_time = 1900.0

#initial data
i = {}

#Initial levels
i[nr] = nri
i[ic] = ici
i[nrtd]=1.0
smooth3_init(i,nruf2,i[nrtd])

def calc_auxiliaries_and_rates(n,time):
    #first the auxiliaries
    n[nruf2] = smooth3_cur_value(n,nruf2)
    n[nruf] = clip(n[nruf2],nruf1,time,pyear)
    n[nrfr] = n[nr]/nri
    
    n[fcaor1] = table_lookup(n[nrfr],0.0,1.0,0.1,[1.0, 0.9, 0.7, 0.5, 0.2, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05])
    n[fcaor2] = table_lookup(n[nrfr],0.0,1.0,0.1,[1.0, 0.9, 0.7, 0.5, 0.2, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05])
    n[fcaor] = clip(n[fcaor2],n[fcaor1],time,pyear)

    n[pop1] = popi*math.exp(gc*(time-1900.0))
    n[pop] = clip(pop2,n[pop1],time,zpgt)

    n[icor] = table_lookup(n[nrtd],0.0,1.0,0.2,[6.0,3.3,3.1,3.06,3.02,3.0])
    n[io] = n[ic]*(1.0 - n[fcaor])/n[icor]
    n[iopc] = n[io]/n[pop]
    
    n[pcrum] = table_lookup(n[iopc],0.0,1600.0,200.0,[0.0, 0.85, 2.6, 4.4, 5.4, 6.2, 6.8, 7.0, 7.0])


    #then the rates
    n[nrur] = n[pop]*n[pcrum]*n[nruf]
    n[icir] = n[io]*(1.0-fioaa-fioas-fioac)
    n[icdr] = n[ic]/alic
    n[nrcm] = table_lookup(1.0-n[nrur]/dnrur,-1.0,0.0,1.0,[-0.05,0.0])
    n[nrate] = clip(n[nrtd]*n[nrcm],0.0,time,pyear)

    smooth3_next_rates(n, nruf2, n[nrtd], tdd)    

    
calc_auxiliaries_and_rates(i,initial_time)

def get_next_time_step(j,time):
    n = {}
    #nextize levels
    n[nr] = j[nr]+dt*(-j[nrur])
    n[ic] = j[ic]+dt*(j[icir]-j[icdr])
    n[nrtd] = j[nrtd]+dt*(j[nrate])

    smooth3_next_levels(n,j,nruf2,dt)    

    calc_auxiliaries_and_rates(n,time)
    return n

run_times(initial_time,dt,200,i,get_next_time_step)

Dynamo include:

import math

def print_sorted_keys(dict,width):
    keys = dict.keys()
    keys.sort()
    f = "%"+str(width)+"s"
    for key in keys[:-1]:
        print (f+",") % key,
    print f % keys[-1]

def print_sorted_values(dict,width):
    keys = dict.keys()
    keys.sort()
    f = "%"+str(width)+"s"
    for key in keys[:-1]:
        print (f+",") % dict[key],
    print f % dict[keys[-1]]

def run_times(initial_time,dt,times,initial,get_next_time_step):
    prev = initial

    width = 20
    f = "%"+str(width)+"s,"
    print f % "time",
    print_sorted_keys(initial,width)
    print f % initial_time,
    print_sorted_values(initial,width)
    
    for i in range(1,times+1):
        time = i*dt + initial_time
        next = get_next_time_step(prev,time)
        print f % time,
        print_sorted_values(next,width)

        prev = next
    print f % "time",
    print_sorted_keys(initial,width)


def step(value,time,currentTime):
    if currentTime >= time:
        return value
    else:
        return 0.0

def clip(after_cutoff,before,value,cutoff_value):
    if value < cutoff_value:
        return before
    else:
        return after_cutoff

def table_lookup(value,low,high,increment,list):
    if value <= low:
        return list[0]
    elif high <= value:
        return list[-1]
    else:
        trans = (value - low)/increment
        low_index = int(math.floor(trans))
        delta = trans - low_index
        return list[low_index]*(1.0-delta)+list[low_index+1]*delta
        
def smooth3_init(init_dictionary,name,value):
    init_dictionary[name+"_l1"] = value
    init_dictionary[name+"_l2"] = value
    init_dictionary[name+"_l3"] = value

def smooth3_next_levels(cur_dict, prev_dict, name, dt):
    cur_dict[name+"_l1"] = prev_dict[name+"_l1"]+dt*prev_dict[name+"_r1"]
    cur_dict[name+"_l2"] = prev_dict[name+"_l2"]+dt*prev_dict[name+"_r2"]
    cur_dict[name+"_l3"] = prev_dict[name+"_l3"]+dt*prev_dict[name+"_r3"]

def smooth3_next_rates(cur_dict, name, value, delay):
    cur_dict[name+"_r1"] = (value - cur_dict[name+"_l1"])/(delay/3.0)
    cur_dict[name+"_r2"] = (cur_dict[name+"_l1"] - cur_dict[name+"_l2"])/(delay/3.0)
    cur_dict[name+"_r3"] = (cur_dict[name+"_l2"] - cur_dict[name+"_l3"])/(delay/3.0)

def smooth3_cur_value(cur_dict, name):
    return cur_dict[name+"_l3"]