def sources(xml): """Can turn off derivative of source terms""" pks = asearch.childByName(xml, "PKs") for pk in pks: try: source_term = asearch.childByName(pk, "mass source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = asearch.childByName(pk, "energy source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pass else: if source_term.getValue(): try: source_is_diff = asearch.childByName( pk, "source term is differentiable") except aerrors.MissingXMLError: pk.append( parameter.BoolParameter( "source term is differentiable", True))
def sources(xml): """Can turn off derivative of source terms""" pks = asearch.childByName(xml, "PKs") for pk in pks: try: source_term = asearch.childByName(pk, "mass source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = asearch.childByName(pk, "energy source key") except aerrors.MissingXMLError: pass else: source_term.setName('source key') try: source_term = asearch.childByName(pk, "source term") except aerrors.MissingXMLError: pass else: if source_term.getValue(): try: source_is_diff = asearch.childByName(pk, "source term is differentiable") except aerrors.MissingXMLError: pk.append(parameter.BoolParameter("source term is differentiable", True))
def snow_depth(xml): for seb_pk in asearch.generateElementByNamePath(xml, "PKs/SEB"): if seb_pk.isElement("primary variable key") and \ asearch.childByName(seb_pk,"primary variable key").get("value") == "surface-snow_depth" and \ seb_pk.isElement("conserved quantity key") and \ asearch.childByName(seb_pk,"conserved quantity key").get("value") == "snow_depth": asearch.childByName(seb_pk,"conserved quantity key").set("value", "surface-snow_depth")
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 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 linear_operator(xml): """Changes any instances of "linear operator" to "linear solver", which is now standard across all PKs.""" pks = asearch.childByName(xml, "PKs") for pk in pks: try: lin_op = asearch.childByName(pk, "linear operator") except aerrors.MissingXMLError: pass else: lin_op.setName("linear solver")
def linear_operator(xml): """Changes any instances of "linear operator" to "linear solver", which is now standard across all PKs.""" pks = asearch.childByName(xml, "PKs") for pk in pks: try: lin_op = asearch.childByName(pk, "linear operator") except aerrors.MissingXMLError: pass else: lin_op.setName("linear solver")
def flatten_pks(xml): try: cycle_driver = asearch.childByName(xml, "cycle driver") except aerrors.MissingXMLError: pass else: return pks = asearch.childByName(xml, "PKs") cd_pks = coordinator_to_cycle_driver(xml) flat_pks = ParameterList("PKs") flatten(pks, flat_pks, cd_pks) pks.extend(flat_pks)
def flatten_pks(xml): try: cycle_driver = asearch.childByName(xml, "cycle driver") except aerrors.MissingXMLError(): pass else: return pks = asearch.childByName(xml, "PKs") cd_pks = coordinator_to_cycle_driver(xml) flat_pks = ParameterList("PKs") flatten(pks, flat_pks, cd_pks) pks.extend(flat_pks)
def add_snow_mesh(xml): meshes = asearch.childByName(xml, "mesh") snow = meshes.sublist("snow") snow.append(parameter.StringParameter("mesh type", "aliased")) aliased = snow.sublist("aliased parameters") aliased.append(parameter.StringParameter("alias", "surface")) vis = asearch.childByName(xml, "visualization") surf_vis = asearch.childByName(vis, "surface") snow_vis = vis.sublist("snow") snow_vis.append(parameter.StringParameter("file name base", "visdump_snow")) for p in surf_vis: if p.get('name') != "file name base": snow_vis.append(copy.copy(p))
def add_snow_mesh(xml): meshes = asearch.childByName(xml, "mesh") snow = meshes.sublist("snow") snow.append(parameter.StringParameter("mesh type", "aliased")) aliased = snow.sublist("aliased parameters") aliased.append(parameter.StringParameter("alias", "surface")) vis = asearch.childByName(xml, "visualization") surf_vis = asearch.childByName(vis, "surface") snow_vis = vis.sublist("snow") snow_vis.append(parameter.StringParameter("file name base", "visdump_snow")) for p in surf_vis: if p.get('name') != "file name base": snow_vis.append(copy.copy(p))
def max_valid_change(xml): """Adds options for max valid change, which aren't required, but are strongly suggested.""" pks = asearch.childByName(xml, "PKs") for pk in pks: pk_type = asearch.childByName(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 coordinator_to_cycle_driver(xml): cycle_driver = asearch.childByName(xml, "coordinator") cycle_driver.set("name", "cycle driver") cycle_driver_pks = ParameterList("PK tree") cycle_driver.append(cycle_driver_pks) return cycle_driver_pks
def coordinator_to_cycle_driver(xml): cycle_driver = asearch.childByName(xml, "coordinator") cycle_driver.set("name", "cycle driver") cycle_driver_pks = ParameterList("PK tree") cycle_driver.append(cycle_driver_pks) return cycle_driver_pks
def adds_source_units(xml): fevals = asearch.childByNamePath(xml, "state/field evaluators") try: te = asearch.childByName(fevals, "total_energy_source") except aerrors.MissingXMLError: pass else: if not te.isElement("mass source units"): te.setParameter("mass source units", "string", "mol m^-2 s^-1") try: tes = asearch.childByName(fevals, "surface-total_energy_source") except aerrors.MissingXMLError: pass else: if not tes.isElement("mass source units"): tes.setParameter("mass source units", "string", "m s^-1")
def adds_source_units(xml): fevals = asearch.childByNamePath(xml, "state/field evaluators") try: te = asearch.childByName(fevals, "total_energy_source") except aerrors.MissingXMLError: pass else: if not te.isElement("mass source units"): te.setParameter("mass source units", "string", "mol m^-2 s^-1") try: tes = asearch.childByName(fevals, "surface-total_energy_source") except aerrors.MissingXMLError: pass else: if not tes.isElement("mass source units"): tes.setParameter("mass source units", "string", "m s^-1")
def update(xml): linear_operator(xml) max_valid_change(xml) bad_spinup_longwave(xml) sources(xml) pks = asearch.childByName(xml, "PKs") for pk in pks: pk_type = asearch.childByName(pk, "PK type") if pk_type.get('value') == 'surface balance implicit': print('updating seb monolitic') import seb_monolithic_to_evals seb_monolithic_to_evals.update_seb(xml) fixEvaluator(xml, "surface-snow_skin_potential", "snow-skin_potential") fixEvaluator(xml, "surface-snow_conductivity", "snow-conductivity") snow_distribution(xml) end_time_units(xml)
def update(xml): linear_operator(xml) max_valid_change(xml) bad_spinup_longwave(xml) sources(xml) pks = asearch.childByName(xml, "PKs") for pk in pks: pk_type = asearch.childByName(pk, "PK type") if pk_type.get('value') == 'surface balance implicit': print('updating seb monolitic') import seb_monolithic_to_evals seb_monolithic_to_evals.update_seb(xml) fixEvaluator(xml, "surface-snow_skin_potential", "snow-skin_potential") fixEvaluator(xml, "surface-snow_conductivity", "snow-conductivity") snow_distribution(xml) end_time_units(xml)
def evals(xml, dc): eval_list = asearch.childByNamePath(xml, "state/field evaluators") try: lw = asearch.childByName(eval_list, "surface-incoming_longwave_radiation") except aerrors.MissingXMLError: eval_list.append(eval_longwave()) eval_list.append(eval_snow_frac_areas()) eval_list.append(eval_snow_swe()) eval_list.append(eval_snow_source_sink(dc)) eval_list.append(eval_albedo()) try: snow_precip = asearch.childByName(eval_list, "surface-precipitation_snow") except aerrors.MissingXMLError: pass else: snow_precip.setName("snow-precipitation")
def evals(xml, dc): eval_list = asearch.childByNamePath(xml, "state/field evaluators") try: lw = asearch.childByName(eval_list, "surface-incoming_longwave_radiation") except aerrors.MissingXMLError: eval_list.append(eval_longwave()) eval_list.append(eval_snow_frac_areas()) eval_list.append(eval_snow_swe()) eval_list.append(eval_snow_source_sink(dc)) eval_list.append(eval_albedo()) try: snow_precip = asearch.childByName(eval_list, "surface-precipitation_snow") except aerrors.MissingXMLError: pass else: snow_precip.setName("snow-precipitation")
def flatten(pks, flat_pks, cd_pks): while len(pks) > 0: pk = pks.getchildren().pop(0) flat_pks.append(pk) new_cd = ParameterList(pk.get("name")) new_cd.append(parameter.StringParameter("PK type", asearch.childByName(pk, "PK type").get("value"))) cd_pks.append(new_cd) try: subpks = pk.pop("PKs") except aerrors.MissingXMLError: pass else: flatten(subpks, flat_pks, new_cd)
def max_valid_change(xml): """Adds options for max valid change, which aren't required, but are strongly suggested.""" pks = asearch.childByName(xml, "PKs") for pk in pks: pk_type = asearch.childByName(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 vis(xml): if xml.isElement("visualization") and not asearch.childByName(xml, "visualization").isElement("domain"): vis_domain = xml.pop("visualization") vis_domain.setName("domain") vis_list = xml.sublist("visualization") vis_list.append(vis_domain) if xml.isElement("visualization surface"): vis_surf = xml.pop("visualization surface") vis_surf.setName("surface") vis_list.append(vis_surf) if xml.isElement("visualization columns"): vis_col = xml.pop("visualization columns") vis_col.setName("column_*") vis_list.append(vis_col)
def flatten(pks, flat_pks, cd_pks): while len(pks) > 0: pk = pks.getchildren().pop(0) flat_pks.append(pk) new_cd = ParameterList(pk.get("name")) new_cd.append( parameter.StringParameter( "PK type", asearch.childByName(pk, "PK type").get("value"))) cd_pks.append(new_cd) try: subpks = pk.pop("PKs") except aerrors.MissingXMLError: pass else: flatten(subpks, flat_pks, new_cd)
def bad_spinup_longwave(xml): """One spinup file commonly used includes a longwave radiation value that is totally wrong. Not many runs actually used it. Some runs even had a spec for it in their file, but didn't include the necessary flag to use it. So this just removes it to avoid confusion.""" evals = asearch.childByNamePath(xml, "state/field evaluators") try: lw = asearch.childByName(evals, "surface-incoming_longwave_radiation") except aerrors.MissingXMLError: pass else: try: filename = asearch.childByNamePath(lw, "function/domain/function/function-tabular/file") except aerrors.MissingXMLError: pass else: if "spinup-10yr.h5" in filename.get('value'): evals.pop("surface-incoming_longwave_radiation")
def bad_spinup_longwave(xml): """One spinup file commonly used includes a longwave radiation value that is totally wrong. Not many runs actually used it. Some runs even had a spec for it in their file, but didn't include the necessary flag to use it. So this just removes it to avoid confusion.""" evals = asearch.childByNamePath(xml, "state/field evaluators") try: lw = asearch.childByName(evals, "surface-incoming_longwave_radiation") except aerrors.MissingXMLError: pass else: try: filename = asearch.childByNamePath( lw, "function/domain/function/function-tabular/file") except aerrors.MissingXMLError: pass else: if "spinup-10yr.h5" in filename.get('value'): evals.pop("surface-incoming_longwave_radiation")
def mesh_list(xml): mesh = asearch.childByName(xml, "mesh") if (mesh.isElement("domain")): return domain = mesh.sublist("domain") to_pop = [] for el in mesh: if el.get("name") == "surface mesh": el.set("name", "surface") elif el.get("name") == "column meshes": el.set("name", "column") elif el.get("name") == "column surface meshes": el.set("name", "column surface") elif el.get("name") in [ "surface", "column", "column surface", "domain" ]: pass else: domain.append(el) for el in domain: mesh.pop(el.get("name")) return
def flatten_pks(xml): pks = asearch.childByName(xml, "PKs") cd_pks = coordinator_to_cycle_driver(xml) flat_pks = ParameterList("PKs") flatten(pks, flat_pks, cd_pks) pks.extend(flat_pks)
def snow_distribution(xml): for snow_dist_pk in asearch.generateElementByNamePath(xml, "PKs/snow distribution"): if snow_dist_pk.isElement("primary variable key") and \ asearch.childByName(snow_dist_pk,"primary variable key").get("value") == "surface-precipitation_snow": asearch.childByName(snow_dist_pk,"primary variable key").set("value", "snow-precipitation") if snow_dist_pk.isElement("conserved quantity key") and \ asearch.childByName(snow_dist_pk,"conserved quantity key").get("value") == "surface-precipitation_snow": asearch.childByName(snow_dist_pk,"conserved quantity key").set("value", "snow-precipitation") if snow_dist_pk.isElement("domain name") and \ asearch.childByName(snow_dist_pk,"domain name").get("value") == "surface": asearch.childByName(snow_dist_pk,"domain name").set("value", "snow") for ssk in asearch.generateElementByNamePath(xml, "state/field evaluators/snow-conductivity"): if ssk.isElement("height key"): asearch.childByName(ssk, "height key").set("value", "snow-precipitation")
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 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
def mesh_list(xml): try: xml.pop("Native Unstructured Input") except aerrors.MissingXMLError: pass try: xml.pop("grid_option") except aerrors.MissingXMLError: pass # move domain mesh parameters to a sublist mesh = asearch.childByName(xml, "mesh") domain = mesh.sublist("domain") to_pop = [] for el in mesh: if el.get("name") == "surface mesh": el.setName("surface") elif el.get("name") == "column meshes": el.setName("column") elif el.get("name") == "column surface meshes": el.setName("column surface") elif el.get("name") in ["surface", "column", "column surface", "domain", "subgrid"]: pass elif el.get("name") in ["framework"]: to_pop.append(el) else: domain.append(el) if domain.isElement("framework"): domain.pop("framework") for el in domain: if mesh.isElement(el.get("name")): mesh.pop(el.get("name")) for el in to_pop: mesh.pop(el.get("name")) # move surface mesh parameters to a sublist if mesh.isElement("surface"): surf_list = mesh.sublist("surface") surf_p_list = surf_list.sublist("surface") if surf_list.isElement("surface sideset name"): surf_p_list.append(surf_list.pop("surface sideset name")) if surf_list.isElement("surface sideset names"): surf_p_list.append(surf_list.pop("surface sideset names")) # make sure all left are mesh sublists, add a mesh type parameter valid_types = ["read mesh file", "generate mesh", "logical mesh", "aliased", "surface", "column", "column surface", "subgrid"] for el in mesh: if not el.isElement("mesh type"): if (el.get("type") != "ParameterList"): print "I don't know what to do with: " print str(el) print el.get("type") assert(el.get("type") == "ParameterList") found = False for valid_type in valid_types: if el.isElement(valid_type): print "setting type: ", valid_type el.setParameter("mesh type", "string", valid_type) asearch.childByName(el, valid_type).setName(valid_type+" parameters") found = True continue assert(found) return
def getElement(self, name): """Get Parameter/sublist from the ParameterList""" return search.childByName(self, name)
def mesh_list(xml): try: xml.pop("Native Unstructured Input") except aerrors.MissingXMLError: pass try: xml.pop("grid_option") except aerrors.MissingXMLError: pass # move domain mesh parameters to a sublist mesh = asearch.childByName(xml, "mesh") domain = mesh.sublist("domain") to_pop = [] for el in mesh: if el.get("name") == "surface mesh": el.setName("surface") elif el.get("name") == "column meshes": el.setName("column") elif el.get("name") == "column surface meshes": el.setName("column surface") elif el.get("name") in [ "surface", "column", "column surface", "domain", "subgrid" ]: pass elif el.get("name") in ["framework"]: to_pop.append(el) else: domain.append(el) if domain.isElement("framework"): domain.pop("framework") for el in domain: if mesh.isElement(el.get("name")): mesh.pop(el.get("name")) for el in to_pop: mesh.pop(el.get("name")) # move surface mesh parameters to a sublist if mesh.isElement("surface"): surf_list = mesh.sublist("surface") surf_p_list = surf_list.sublist("surface") if surf_list.isElement("surface sideset name"): surf_p_list.append(surf_list.pop("surface sideset name")) if surf_list.isElement("surface sideset names"): surf_p_list.append(surf_list.pop("surface sideset names")) # make sure all left are mesh sublists, add a mesh type parameter valid_types = [ "read mesh file", "generate mesh", "logical mesh", "aliased", "surface", "column", "column surface", "subgrid" ] for el in mesh: if not el.isElement("mesh type"): if (el.get("type") != "ParameterList"): print "I don't know what to do with: " print str(el) print el.get("type") assert (el.get("type") == "ParameterList") found = False for valid_type in valid_types: if el.isElement(valid_type): print "setting type: ", valid_type el.setParameter("mesh type", "string", valid_type) asearch.childByName(el, valid_type).setName(valid_type + " parameters") found = True continue assert (found) return
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
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(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 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 snow_distribution(xml): for snow_dist_pk in asearch.generateElementByNamePath( xml, "PKs/snow distribution"): if snow_dist_pk.isElement("primary variable key") and \ asearch.childByName(snow_dist_pk,"primary variable key").get("value") == "surface-precipitation_snow": asearch.childByName(snow_dist_pk, "primary variable key").set( "value", "snow-precipitation") if snow_dist_pk.isElement("conserved quantity key") and \ asearch.childByName(snow_dist_pk,"conserved quantity key").get("value") == "surface-precipitation_snow": asearch.childByName(snow_dist_pk, "conserved quantity key").set( "value", "snow-precipitation") if snow_dist_pk.isElement("domain name") and \ asearch.childByName(snow_dist_pk,"domain name").get("value") == "surface": asearch.childByName(snow_dist_pk, "domain name").set("value", "snow") for ssk in asearch.generateElementByNamePath( xml, "state/field evaluators/snow-conductivity"): if ssk.isElement("height key"): asearch.childByName(ssk, "height key").set("value", "snow-precipitation")