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 sources(xml): """Can turn off derivative of source terms""" pks = asearch.child_by_name(xml, "PKs") for pk in pks: try: source_term = pk.getElement("mass source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = pk.getElement("energy source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = pk.getElement("source term") except aerrors.MissingXMLError: pass else: if source_term.getValue(): try: source_is_diff = pk.getElement( "source term is differentiable") except aerrors.MissingXMLError: pk.append( parameter.BoolParameter( "source term is differentiable", True))
def eval_snow_source_sink(dc=None): ss = parameter_list.ParameterList("snow-source_sink") ss.append( parameter.StringParameter("field evaluator type", "surface balance")) ss.append(parameter.BoolParameter("save diagnostic data", True)) if dc is not None: ss.append(dc) vo = ss.sublist("verbose object") vo.append(parameter.StringParameter("verbosity level", "high")) return ss
def set_source_term(pk): if not pk.isElement("source term"): pk.append(parameter.BoolParameter("source term", True)) else: asearch.childByName(pk, "source term").set("value", "true")
def seb(xml): pk_list = [pk for pk in asearch.generateElementByNamePath(xml, "PKs")] assert (len(pk_list) == 1) pk_list = pk_list[0] seb_pk = None flow_sub_pk = None flow_surf_pk = None energy_sub_pk = None energy_surf_pk = None # make sure we can find all of them! for pk in pk_list: if asearch.childByName( pk, "PK type").get("value") == "surface balance implicit": if seb_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") seb_pk = pk elif asearch.childByName(pk, "PK type").get("value") == "permafrost flow": if flow_sub_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") flow_sub_pk = pk elif asearch.childByName( pk, "PK type").get("value") == "overland flow with ice": if flow_surf_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") flow_surf_pk = pk elif asearch.childByName( pk, "PK type").get("value") == "three-phase energy": if energy_sub_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") energy_sub_pk = pk elif asearch.childByName(pk, "PK type").get("value") == "surface energy": if energy_surf_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") energy_surf_pk = pk if seb_pk is None or flow_sub_pk is None or flow_surf_pk is None or energy_sub_pk is None or energy_surf_pk is None: return # check the source terms for all def set_source_term(pk): if not pk.isElement("source term"): pk.append(parameter.BoolParameter("source term", True)) else: asearch.childByName(pk, "source term").set("value", "true") set_source_term(flow_sub_pk) set_source_term(flow_surf_pk) set_source_term(energy_sub_pk) set_source_term(energy_surf_pk) if not flow_sub_pk.isElement("mass source key"): flow_sub_pk.append( parameter.StringParameter("mass source key", "mass_source")) if not flow_surf_pk.isElement("source key"): flow_surf_pk.append( parameter.StringParameter("source key", "surface-mass_source")) if not flow_surf_pk.isElement("mass source in meters"): flow_surf_pk.append( parameter.BoolParameter("mass source in meters", True)) if not energy_sub_pk.isElement("energy source"): energy_sub_pk.append( parameter.StringParameter("energy source", "total_energy_source")) if not energy_surf_pk.isElement("energy source"): energy_surf_pk.append( parameter.StringParameter("energy source", "surface-total_energy_source")) eval_list = [ ev for ev in asearch.generateElementByNamePath( xml, "state/field evaluators") ] assert (len(eval_list) == 1) eval_list = eval_list[0] try: eval_list.pop("surface-total_energy_source") except aerrors.MissingXMLError: pass try: eval_list.pop("surface-mass_source_enthalpy") except aerrors.MissingXMLError: pass try: eval_list.pop("surface-source_internal_energy") except aerrors.MissingXMLError: pass molar_dens = asearch.childByName(eval_list, "surface-source_molar_density") if molar_dens.isElement("temperature key"): asearch.childByName(molar_dens, "temperature key").set("value", "surface-temperature") else: molar_dens.append( parameter.StringParameter("temperature key", "surface-temperature"))
def seb_twocomponent(xml, eval_name): """This changes the old, 'Arctic' SEB model into a test-reproducing, same answer input file.""" # area fractions try: frac_area = asearch.find_path( xml, ["state", "field evaluators", "snow-fractional_areas"]) except aerrors.MissingXMLError: pass else: frac_area.setName("surface-fractional_areas") for name in [ 'surface-fractional_areas', 'surface_column:*-fractional_areas' ]: try: frac_area = asearch.find_path(xml, ["state", "field evaluators", name]) except aerrors.MissingXMLError: pass else: domain = frac_area.getName().split('-')[0] frac_area.setName(domain + "-area_fractions") frac_area_type = asearch.child_by_name(frac_area, "field evaluator type") if frac_area_type.getValue() == "surface balance area fractions": frac_area_type.setValue("area fractions, two components") # water source try: water_source = asearch.find_path( xml, ['state', 'field evaluators', eval_name]) except aerrors.MissingXMLError: pass else: water_source_type = asearch.child_by_name(water_source, "field evaluator type") if water_source_type.getValue() == "surface balance": water_source_type.setValue( "surface energy balance, two components") try: asearch.child_by_name(water_source, "use model from ATS 1.1") except aerrors.MissingXMLError: water_source.append( parameter.BoolParameter("use model from ATS 1.1", True)) # subgrid albedos for name in [ 'surface-subgrid_albedos', 'surface_column:*-subgrid_albedos' ]: try: albedo = asearch.find_path(xml, ['state', 'field evaluators', name]) except aerrors.MissingXMLError: pass else: dname = name.split('-')[0] albedo.setName(dname + '-albedos') albedo_type = asearch.child_by_name(albedo, "field evaluator type") if albedo_type.getValue() == "albedo": albedo_type.setValue("two-component subgrid albedos")
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