Skip to content

Instantly share code, notes, and snippets.

@hersfeldtn
Last active December 3, 2021 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hersfeldtn/fafcd7b83b98716968645dfe759effd5 to your computer and use it in GitHub Desktop.
Save hersfeldtn/fafcd7b83b98716968645dfe759effd5 to your computer and use it in GitHub Desktop.
import exoplasim as exo
model = exo.Model(workdir='model_run', modelname='model', outputtype='.nc',
resolution='T21', layers=10, ncpus=4, precision=8, crashtolerant=True)
model.configure(timestep=45, runsteps=11520, otherargs={'NSTPW@plasim_namelist':'160'},
physicsfilter='gp|exp|sp', restarfile=,
flux=1367, startemp=5780,
year=360, eccentricity=0.016715, fixedorbit=True,
rotationperiod=1, obliquity=23.441, lonvernaleq=102.7,
synchronous=False, substellarlon=180, desync=0, tlcontrast=0,
gravity=9.80665, radius=1,
landmap=, topomap=,
pN2=0.7809, pO2=0.2095, pAr=0.0093, pCO2=300e-6, ozone=True,
pH2=0, pHe=0, pNe=0, pKr=0, pH2O=0,
wetsoil=True, vegetation=2, vegaccel=1, initgrowth=0.5,
glaciers={'toggle': True, 'mindepth': 2, 'initialh': -1}
)
level = 300e-6 #initial CO2 level
adjust = 40e-6 #initial CO2 adjustment level
run = 0
goal = 288.15 #desired final temperature in Kelvin
tolerance = 8
ts = 248.15
ts1 = ts
model.run(years=5, crashifbroken=True, clean=True)
lines = ['start','\n']
while True:
ts2 = ts1
ts1 = ts
ts = model.getbalance(key='ts')
run += 1
savename = 'model_run_'+str(run)
model.finalize(savename,allyears=False,clean=False,keeprestarts=True)
model.save(savename)
lines.extend(['\n','\nrun ',str(run),'\nlevel ',str(level),'\nadjust ',str(adjust),'\nts ',str(ts)])
with open('model_report.txt','a') as f:
f.writelines(lines)
f.close()
if ts < goal - tolerance:
balance = 0
if ts - ts1 < tolerance/4:
if ts1 < goal:
adjust /= 2
elif ts < ts1 and ts1 < ts2:
adjust *= 2
level += adjust
elif ts - ts1 > goal - ts:
adjust /= 2
level -= adjust
elif ts > goal + tolerance:
balance = 0
if ts1 - ts < tolerance/4:
if ts1 < goal:
adjust /= 2
elif ts > ts1 and ts1 > ts2:
adjust *= 2
level -= adjust
elif ts1 - ts > ts - goal:
adjust /= 2
level += adjust
else:
if ts > ts1 + tolerance/4:
balance = 0
if ts2 > ts1:
adjust /= 2
level -= adjust
elif ts < ts1 - tolerance/4:
balance = 0
if ts2 < ts1:
adjust /= 2
level += adjust
else:
if tolerance > 0.6: #sets the final tolerance
balance = 0
tolerance /= 2
adjust /= 2
else:
balance += 1
if balance > 2:
break
level = max(10e-6, level) #sets the minimum CO2 level
model.modify(pCO2=level) #step where the model is actually adjusted
if balance == 2:
model.runtobalance(baseline=10, maxyears=1000, minyears=10, crashifbroken=True clean=True)
elif balance == 1:
model.run(years=10, crashifbroken=True, clean=True)
else:
model.run(years=5, crashifbroken=True, clean=True)
model.finalize('model_final',allyears=False,clean=False,keeprestarts=True)
model.save('model_final')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment