def test_computable_mvars(self): # for debugging we draw the sparse_powerset_graph of the actually present computers and mvars spsg = sparse_powerset_graph(bgc_md2_computers()) f = plt.figure() ax = f.add_subplot(1, 1, 1) draw_ComputerSetMultiDiGraph_matplotlib(ax, spsg, bgc_md2_mvar_aliases(), bgc_md2_computer_aliases(), targetNode=frozenset( {SmoothModelRun})) f.savefig("spgs.pdf") fig = plt.figure() draw_update_sequence(bgc_md2_computers(), max_it=8, fig=fig) fig.savefig("c1.pdf") # https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests for mn in [ "Potter1993GlobalBiogeochemicalCycles", "testVectorFree", "Williams2005GCB", # just uncomment the one model you are working on and comment the others ]: with self.subTest(mn=mn): mvs = MVarSet.from_model_name(mn) mvars = mvs.computable_mvar_types() list_str = "\n".join( ["<li> " + str(var.__name__) + " </li>" for var in mvars]) print(list_str) for var in mvars: print("########################################") print(str(var.__name__)) print(mvs._get_single_mvar_value(var))
def test_from_model_name(self): # alternative constructor mvs= MVarSet.from_model_name("testVectorFree") res = frozenset( [ InFluxesBySymbol, OutFluxesBySymbol, InternalFluxesBySymbol, TimeSymbol, StateVariableTuple, ] ) self.assertSetEqual(mvs.provided_mvar_types, res)
def setUp(self): I_vl, I_vw = symbols("I_vl I_vw") vl, vw = symbols("vl vw") k_vl, k_vw = symbols("k_vl k_vw") self.provided_values=frozenset( [ InFluxesBySymbol({vl: I_vl, vw: I_vw}), OutFluxesBySymbol({vl: k_vl * vl, vw: k_vw * vw}), InternalFluxesBySymbol({(vl, vw): k_vl * vl, (vw, vl): k_vw * vw}), TimeSymbol("t"), StateVariableTuple((vl, vw)) ] ) self.mvs=MVarSet(self.provided_values)
def setUp(self): self.mn = "Williams2005GCB" self.mvs = MVarSet.from_model_name(self.mn) self.ref_provided_mvars = frozenset([ CompartmentalMatrix, TimeSymbol, StateVariableTuple, VegetationCarbonInputPartitioningTuple, VegetationCarbonInputScalar, InputTuple, NumericStartValueDict, NumericSimulationTimes, NumericParameterization, QuantityStartValueDict, QuantitySimulationTimes, QuantityParameterization, BibInfo, # QuantityModelRun, # QuantityParameterizedModel ])
def setUp(self): self.mn = "Castanho2013Biogeosciences" self.mvs = MVarSet.from_model_name(self.mn) self.ref_provided_mvars = frozenset([ CompartmentalMatrix, TimeSymbol, StateVariableTuple, VegetationCarbonInputPartitioningTuple, VegetationCarbonInputScalar, VegetationCarbonStateVariableTuple, InputTuple, # # NumericStartValueDict, # # NumericSimulationTimes, # NumericParameterization, # # QuantityStartValueDict, # # QuantitySimulationTimes, # # QuantityParameterization, # BibInfo, # # QuantityModelRun, # # QuantityParameterizedModel ])
def setUp(self): # self.mn = "" self.mn = "ElMasri2013AgricForMeteorol" # self.mn = "Pavlick2013Biogeosciences" # self.mn = "Hilbert1991AnnBot" # self.mn = "Foley1996GBC" # self.mn = "Gu2010EcologicalComplexity" # self.mn = "Arora2005GCB-1" # self.mn = "King1993TreePhysiol" # self.mn = "Murty2000EcolModell" # self.mn = "Wang2010Biogeosciences" # self.mn = "DeAngelis2012TheorEcol" # self.mn = "Comins1993EA" # self.mn = "Running1988EcolModel" # self.mn = "Luo2012TE" self.mvs = MVarSet.from_model_name(self.mn) self.ref_provided_mvars = frozenset( [ CompartmentalMatrix, TimeSymbol, StateVariableTuple, VegetationCarbonInputPartitioningTuple, VegetationCarbonInputScalar, VegetationCarbonStateVariableTuple, InputTuple, # # NumericStartValueDict, # # NumericSimulationTimes, # NumericParameterization, # # QuantityStartValueDict, # # QuantitySimulationTimes, # # QuantityParameterization, # BibInfo, # # QuantityModelRun, # # QuantityParameterizedModel ] )
# "ElMasri2013AgricForMeteorol", #Paper shows results for soil carbon, and fig. 1 has litter and C pools, but the equations on table A2 (although very detailed) don't include them. There are also equations for Phenology, but they were not included in the source.py # "Scheiter2009GlobalChangeBiol", #No soil compartments # "Turgman2018EcologyLetters", #No soil compartments # "Haverd2016Biogeosciences", #No soil compartments # "Foley1996GBC", #No equations for litter and soil, but the figure has those compartments. See Markus’ Ibis.yaml? # "Gu2010EcologicalComplexity", #No equations for litter and soil, but the model description (CEVSA) mentions them # "King1993TreePhysiol", #No soil compartments # "DeAngelis2012TheorEcol", #No soil compartments (model based on G’Day, but removed litter and soil compartments) # "Potter1993GlobalBiogeochemicalCycles", #No equations for litter and soil, but the model description mentions them # "testVectorFree", # "Williams2005GCB", "CARDAMOM", ] ###################################################################### ####### MODELS NOT TRANSLATED FROM .YAML TO SOURCE.PY # Sitch2003GlobChangBiol: Not included because allocation (see pg 8) has no ODEs; biomass increment is allocated to the tissue pools while satisfying the functional balance difference equations... # VanDerWerf1993PlantSoil: this model has no compartment for wood. It is used to simulate effect of nitrogen on growth of a grass (Dactylis glomerata L.). # ICBM: same as Andren1997EA but less parameter sets # Schimel2003SoilBiologyandBiochemistry.yaml, Schimel2003SoilBiologyandBiochemistry_rMM.yaml, Schimel2003SoilBiologyandBiochemistry_rMM_improved.yaml: Original model has no outputs, corrected by Holger # Ibis: includes soil, no metadata -added by Markus. Vero’s version: Foley ###################################################################### for mn in model_names: mvs = MVarSet.from_model_name(mn) mvars = mvs.computable_mvar_types() list_str = "\n".join(["<li> " + str(var.__name__) + " </li>" for var in mvars]) print(list_str) for var in mvars: print("########################################") print(str(var.__name__)) print(mvs._get_single_mvar_value(var))
mvs = MVarSet({ BibInfo(# Bibliographical Information name="DALEC", longName="Data Assimilation Linked Ecosystem model", version="1", entryAuthor="Verónika Ceballos-Núñez", entryAuthorOrcid="0000-0002-0046-1160", entryCreationDate="16/9/2016", doi="10.1111/j.1365-2486.2004.00891.x", further_references=BibInfo(doi="10.1016/j.agrformet.2009.05.002"), # Also from PDF in Reflex experiment sym_dict=sym_dict ), # # the following variables constitute the compartmental system: # A, # the overall compartmental matrix Input, # the overall imput t, # time for the complete system x, # state vector of the the complete system # # the following variables constitute a numerical model run : # np1, nsv1, ntimes, # Again the model run could be created explicitly (in several ways) # # the following variables constitute a quantiy model run : # qp1, qsv1, qtimes, # Again the model run could be created explicitly (in several ways) # # the following variables give a more detailed view with respect to # carbon and vegetation variables # This imformation can be used to extract the part # of a model that is concerned with carbon and vegetation # in the case of this model all of the state variables # are vegetation and carbon related but for an ecosystem model # for different elements there could be different subsystems # e.g. consisting of Nitrogen Soil state variables # # vegetation carbon VegetationCarbonInputScalar(u), # vegetation carbon partitioning. VegetationCarbonInputPartitioningTuple(b), VegetationCarbonStateVariableTuple((C_f, C_lab, C_w, C_r)) })
vl, vw = symbols("vl vw") k_vl, k_vw = symbols("k_vl k_vw") # the keys of the internal flux dictionary are tuples (source_pool,target_pool) # srm:SmoothReservoirModel # srm=SmoothReservoirModel.from_state_variable_indexed_fluxes( # in_fluxes # ,out_fluxes # ,internal_fluxes # ) #specialVars = { mvs = MVarSet({ InFluxesBySymbol({ vl: I_vl, vw: I_vw }), OutFluxesBySymbol({ vl: k_vl * vl, vw: k_vw * vw }), InternalFluxesBySymbol({ (vl, vw): k_vl * vl, (vw, vl): k_vw * vw }), TimeSymbol("t"), StateVariableTuple((vl, vw)) # ,'SmoothReservoirModel':srm })
def test_numeric_input_tuple(self): # setup the paths to the testdata cable_out_path = Path( '/home/data/cable-data/example_runs/parallel_1901_2004_with_spinup/output/new4' ) # cable_data_set = cH.cable_ds(cable_out_path) time_slice = slice(0, None) landpoint_slice = slice(1590, 1637) # cheated # landpoint_slice = slice(None,None) # time_slice=slice(None,None,None) zarr_cache_path = cP.slice_dir_path( cable_out_path, sub_dir_trunk="zarr_mm11", time_slice=time_slice, landpoint_slice=landpoint_slice, ) if "cable_data_set" not in dir(): cable_data_set = cH.cable_ds(cable_out_path) args = { "cable_data_set": cable_data_set, "zarr_cache_path": zarr_cache_path, "landpoint_slice": landpoint_slice, "time_slice": time_slice, #'batch_size': 128, "batch_size": 12, #'rm': True } x_org_iveg = cH.cacheWrapper(cC.x_org_iveg, **args) time = cH.cacheWrapper(cC.time, **args) patches, landpoints = cC.all_pools_vary_cond_nz(**args) pcs = patches.compute() lpcs = landpoints.compute() pcs, lpcs p = Path('plots') p.mkdir(exist_ok=True) ind = 0 # first pair that has a nonconstant solution for lp = lpcs[ind] patch = lpcs[ind] # get the symbolic represantation from the database mvs = self.mvs # define some stuff to extend it with def default(t): return 1 leaf = Symbol('leaf') fine_root = Symbol('fine_root') Npp = Function("Npp") bvec_leaf = Function("bvec_leaf") bvec_fine_root = Function("bvec_fine_root") xk_leaf_cold = Function("xk_leaf_cold") xk_leaf_dry = Function("xk_leaf_dry") kleaf = Function("kleaf") kfroot = Function("kfroot") # bvec_wood = Function("bvec_wood") np1 = NumericParameterization( par_dict={}, func_dict=frozendict({ Npp: default, bvec_fine_root: default, bvec_leaf: default, xk_leaf_cold: default, kleaf: default, kfroot: default, xk_leaf_dry: default, }), ) nsv1 = NumericStartValueDict({leaf: 0.3, fine_root: 3.96}) ntimes1 = NumericSimulationTimes(np.linspace(0, 1, 11)) # extend the symbolice version with the new stuff pvs = mvs.provided_mvar_values #from IPython import embed; embed() pvs1 = pvs.union(frozenset({np1, nsv1, ntimes1})) mvs1 = MVarSet(pvs1) x = mvs1.get_StateVariableTuple() Input = mvs1.get_InputTuple() #B = mvs1.get_CompartmentalMatrix() sym_times = mvs1.get_NumericSimulationTimes() sol_smooth = mvs1.get_NumericSolutionArray() comp_slice = slice(0, 100) n = sol_smooth.shape[1] fig = plt.figure() for pool in range(n): ax = fig.add_subplot(n + 1, 1, 2 + pool) title = "\$" + latex(x[pool]) + "\$" #ax.plot( # sym_times[comp_slice], # sol_smooth[comp_slice, pool], # color='r' #) ax.plot(time[comp_slice], x_org_iveg[comp_slice, pool, patch, lp], color='b') fontsize = 10 ax.set_title(title, fontsize=fontsize) fig.savefig('solution.pdf')
def setUp(self): self.mn = "cable_all" self.mvs = MVarSet.from_model_name(self.mn)
# text_representation: # extension: .py # format_name: light # format_version: '1.5' # jupytext_version: 1.10.2 # kernelspec: # display_name: Python 3 # language: python # name: python3 # --- from IPython.display import HTML from bgc_md2.resolve.mvars import CompartmentalMatrix, StateVariableTuple, VegetationCarbonInputPartitioningTuple,VegetationCarbonInputTuple from bgc_md2.resolve.MVarSet import MVarSet display(HTML("<style>.container { width:100% !important; }</style>")) mvs_mm = MVarSet.from_model_name('TECOmm') import igraph g=igraph.Graph(directed=True) in_fluxes, internal_fluxes, out_fluxes = mvs_mm.get_InFluxesBySymbol(),mvs_mm.get_InternalFluxesBySymbol(),mvs_mm.get_OutFluxesBySymbol() in_flux_targets, out_flux_sources = [[str(k) for k in d.keys()] for d in (in_fluxes, out_fluxes)] internal_connections = [(str(s),str(t)) for s,t in internal_fluxes.keys()] # + G=igraph.Graph(directed=True) for i,s in enumerate(mvs_mm.get_StateVariableTuple()): G.add_vertex(str(s))# the name attribute is set automatically
{ VegetationCarbonInputPartitioningTuple, NumericSolutionArray } ), # explicit_exclude_models=frozenset({'CARDAMOM'}) ) li # From these we chose two models to investigate more thoroughly. # We load the first one with a helper function and the second by using standard python tools. # # mvs_Luo = MVarSet.from_model_name('Luo2012TE') # or using a moduel import from bgc_md2.models.TECO.source import mvs as mvs_TECO # # ComputabilityGraphs # Now that we actually have two records (MVarSets) we can exlore what we can comptute from them. # Just add a dot "." behind the variable in the next cell and press the tab key! # mvs_TECO # The options provided by the python interpreter are actually the result of a graph computation. # To see all computable mvars of the TECO MVarSet execute the next cell! # mvs_TECO.computable_mvar_names