def snow_distribution(xml): for snow_dist_pk in asearch.generateElementByNamePath( xml, "PKs/snow distribution"): snow_dist_pk.append( parameter.DoubleParameter("distribution time", 86400.0)) if snow_dist_pk.isElement("primary variable key") and \ asearch.childByName(snow_dist_pk,"primary variable key").get("value") == "surface-precipitation_snow" and \ snow_dist_pk.isElement("conserved quantity key") and \ asearch.childByName(snow_dist_pk,"conserved quantity key").get("value") == "precipitation-snow": asearch.childByName(snow_dist_pk, "conserved quantity key").set( "value", "surface-precipitation-snow") for ssk in asearch.generateElementByNamePath( xml, "state/field evaluators/surface-snow_skin_potential"): if not ssk.isElement("dt factor"): ssk.append(parameter.DoubleParameter("dt factor", 86400.0)) else: asearch.childByName(ssk, "dt factor").set("value", "86400.0") for ssc in asearch.generateElementByNamePath( xml, "state/field evaluators/surface-snow_conductivity"): if not ssc.isElement("include dt factor"): ssc.append(parameter.BoolParameter("include dt factor", True)) else: asearch.childByName(ssc, "include dt factor").set("value", "true") if not ssc.isElement("dt factor"): ssc.append(parameter.DoubleParameter("dt factor", 86400.0)) else: asearch.childByName(ssc, "dt factor").set("value", "86400.0")
def max_valid_change(xml): """Adds options for max valid change, which aren't required, but are strongly suggested.""" pks = asearch.child_by_name(xml, "PKs") for pk in pks: pk_type = asearch.child_by_name(pk, "PK type") if pk_type.get('value') == 'permafrost flow': try: pk.getElement("max valid change in saturation in a time step [-]") except aerrors.MissingXMLError: pk.append(parameter.DoubleParameter("max valid change in saturation in a time step [-]", 0.1)) try: pk.getElement("max valid change in ice saturation in a time step [-]") except aerrors.MissingXMLError: pk.append(parameter.DoubleParameter("max valid change in ice saturation in a time step [-]", 0.1))
def add_to_lc(eval_name, param_name, param_default, lc, param_name_new=None): if param_name_new is None: param_name_new = param_name # add the dessicated zone thickness try: evaluator = asearch.find_path( xml, ['state', 'field evaluators', eval_name]) except aerrors.MissingXMLError: pval = None else: try: param = asearch.child_by_name(evaluator, param_name) except aerrors.MissingXMLError: pval = None else: pval = param.getValue() evaluator.remove(param) if pval is None: pval = param_default lc.append(parameter.DoubleParameter(param_name_new, pval))
def fixSnow(xml): # remove "include dt factor" # rename "dt factor" --> "dt factor [s]" try: snow_cond = asearch.find_path( xml, ["state", "field evaluators", "snow-conductivity"]) except aerrors.MissingXMLError: pass else: try: snow_cond.pop("include dt factor") except aerrors.MissingXMLError: pass try: dtf = asearch.find_name(snow_cond, "dt factor") except aerrors.MissingXMLError: pass else: dtf.setName("dt factor [s]") # rename "include density factor" --> "include density" try: inc_dens = asearch.find_name(snow_cond, "include density factor") except aerrors.MissingXMLError: pass else: inc_dens.setName("include density") # add "swe density factor [-]" (default is 10) if len(asearch.findall_name(snow_cond, "swe density factor [-]")) == 0: snow_cond.append( parameter.DoubleParameter("swe density factor [-]", 10.0))
def eval_snow_swe(): swe = parameter_list.ParameterList("snow-swe") swe.append( parameter.StringParameter("field evaluator type", "multiplicative evaluator")) swe.append( parameter.ArrayStringParameter( "evaluator dependencies", ["snow-depth", "snow-density", "snow-cell_volume"])) swe.append(parameter.DoubleParameter("coefficient", 1.e-3)) return swe
def surface_rel_perm_one(xml): """Add units, changed to pressure.""" for surf_rel_perm in asearch.findall_name(xml, "surface rel perm model"): pres_above = None if surf_rel_perm.isElement("unfrozen rel perm cutoff depth"): height_el = surf_rel_perm.pop("unfrozen rel perm cutoff height") pres_above = height_el.getValue() * 1000 * 10 if surf_rel_perm.isElement("unfrozen rel pres cutoff pressure"): pres_el = surf_rel_perm.pop("unfrozen rel perm cutoff pressure") pres_above = pres_el.getValue() if surf_rel_perm.isElement("unfrozen rel pres cutoff pressure [Pa]"): continue else: if pres_above is not None: surf_rel_perm.append(parameter.DoubleParameter("unfrozen rel pres cutoff pressure [Pa]", pres_above))
def pks(xml): pk_tree = asearch.childByNamePath(xml, "cycle driver/PK tree") for pk_type in asearch.generateElementByNamePath(pk_tree, "PK type"): if pk_type.getValue() == 'surface balance implicit': pk_type.setValue('surface balance implicit subgrid') pks = asearch.childByName(xml, "PKs") for pk in pks: pk_type = asearch.childByName(pk, "PK type") if pk_type.get('value') == 'permafrost flow': try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pass else: source_term.setValue(False) elif pk_type.get('value') == 'overland flow with ice': try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pk.append(parameter.BoolParameter("source term", True)) else: source_term.setValue(True) try: source_is_diff = asearch.childByName( pk, "source term is differentiable") except aerrors.MissingXMLError: pk.append( parameter.BoolParameter("source term is differentiable", False)) else: source_is_diff.setValue(False) elif pk_type.get('value') == 'three-phase energy': try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pass else: source_term.setValue(False) elif pk_type.get('value') == 'surface energy': try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pk.append(parameter.BoolParameter("source term", True)) else: source_term.setValue(True) try: source_is_diff = asearch.childByName( pk, "source term is differentiable") except aerrors.MissingXMLError: pk.append( parameter.BoolParameter("source term is differentiable", True)) else: source_is_diff.setValue(True) try: source_fd = asearch.childByName( pk, "source term finite difference") except aerrors.MissingXMLError: pk.append( parameter.BoolParameter("source term finite difference", True)) else: source_fd.setValue(True) elif pk_type.get('value') == 'surface balance implicit': pk_seb = parameter_list.ParameterList(pk.get('name')) pk_seb.append( parameter.StringParameter('PK type', 'surface balance implicit subgrid')) pk_seb.append(parameter.StringParameter('layer name', 'snow')) pk_seb.append(parameter.StringParameter('domain name', 'snow')) pk_seb.append( parameter.StringParameter('primary variable key', 'snow-depth')) pk_seb.append( parameter.StringParameter('conserved quantity key', 'snow-swe')) pk_seb.append( parameter.StringParameter('source key', 'snow-source_sink')) pk_seb.append( parameter.BoolParameter('source term is differentiable', False)) pk_seb.append(pk.pop('initial condition')) try: pk_seb.append(pk.pop('verbose object')) except aerrors.MissingXMLError: pass try: dc = pk.pop('debug cells') except aerrors.MissingXMLError: dc = None else: pk_seb.append(copy.copy(dc)) pc = pk_seb.sublist('preconditioner') pc.append( parameter.StringParameter('preconditioner type', 'identity')) ls = pk_seb.sublist('linear solver') ls.append(parameter.StringParameter('iterative method', 'nka')) nka = ls.sublist('nka parameters') nka.append(parameter.DoubleParameter('error tolerance', 1.e-6)) nka.append( parameter.IntParameter('maximum number of iterations', 10)) pks.pop(pk.get('name')) pks.append(pk_seb) return dc