"""Prescribed synthetic carbon accounting and steady leaf-area water transfer.""" import math import time from plant_kernel import carbon, water DEFAULT = dict(uptake=.8, release=.15, photoperiod=16, conductance=.2, vapor_difference=1, pressure=100) BOUNDS = dict(uptake=(0,2), release=(0,1), photoperiod=(0,24), conductance=(0,1), vapor_difference=(0,5), pressure=(80,110)) def run(name, p): if name != 'plant' or set(p) != set(BOUNDS): raise ValueError('Unknown experiment or parameters') for k, (lo, hi) in BOUNDS.items(): if type(p[k]) not in (int,float) or not math.isfinite(p[k]) or not lo <= p[k] <= hi: raise ValueError('Parameter out of range: ' + k) start = time.perf_counter() hours = sorted({0.,p['photoperiod'],24.}) change = [carbon(p['uptake'],p['release'],min(t,p['photoperiod'])) + carbon(0,p['release'],max(0,t-p['photoperiod'])) for t in hours] differences = [i/10 for i in range(51)] flux = water(p['conductance'],p['vapor_difference'],p['pressure']) light = carbon(p['uptake'],p['release'],p['photoperiod']) dark = carbon(0,p['release'],24-p['photoperiod']) return dict(metrics=[dict(id='carbon_light',value=light,unit='gC'),dict(id='carbon_dark',value=dark,unit='gC'),dict(id='carbon_daily',value=light+dark,unit='gC'),dict(id='water_flux',value=flux,unit='kg m⁻² leaf h⁻¹')], series=[dict(id='carbon_change',x=hours,y=change,x_unit='h',y_unit='gC'),dict(id='water_curve',x=differences,y=[water(p['conductance'],d,p['pressure']) for d in differences],x_unit='kPa',y_unit='kg m⁻² leaf h⁻¹'),dict(id='water_selected',x=[p['vapor_difference']],y=[flux],x_unit='kPa',y_unit='kg m⁻² leaf h⁻¹')], stdout='',provenance=dict(data_type='synthetic',compute_ms=(time.perf_counter()-start)*1000)) if __name__ == '__main__': import argparse,json parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--parameters',default=json.dumps(DEFAULT)) print(json.dumps(run('plant',json.loads(parser.parse_args().parameters)),allow_nan=False))