def primary_variable(xml): for pv in asearch.findall_name(xml, "primary variable"): pv.setName("primary variable key") for pv in asearch.findall_name(xml, "primary variable key"): if pv.getValue() == "ponded_depth": pv.setValue("surface-ponded_depth") if pv.getValue() == "snow_depth": pv.setValue("surface-snow_depth")
def checkManning(xml): fe_list = asearch.find_path(xml, ["state", "field evaluators"]) for eval in fe_list: ename = eval.getName() if ename.endswith( 'manning_coefficient') and not ename.startswith('snow'): eval_type = eval.getElement('field evaluator type') if eval_type.getValue() == 'independent variable': func_reg = eval.getElement("function") for reg in func_reg: comp_entries = asearch.findall_name(reg, ['components']) if len(comp_entries) > 1: # previous iterations of this script were broken... for entry in comp_entries[1:]: reg.remove(entry) fixed = False if not fixed: try: comp = reg.getElement('component') except aerrors.MissingXMLError: pass else: reg.pop('component') assert len(asearch.findall_name(reg, 'components')) == 0 reg.append( parameter.ArrayStringParameter( 'components', ['cell', 'boundary_face'])) fixed = True if not fixed: try: comp = reg.getElement('components') except aerrors.MissingXMLError: pass else: reg.pop('components') assert len(asearch.findall_name(reg, 'components')) == 0 reg.append( parameter.ArrayStringParameter( 'components', ['cell', 'boundary_face'])) fixed = True if not fixed: print(reg.__str__().decode('utf-8')) raise aerrors.MissingXMLError( 'Missing "component" or "components"')
def fixOverlandConductivity(xml): pm = asearch.parent_map(xml) fe = asearch.find_path(xml, ["state", "field evaluators"]) try: elev = asearch.find_name(xml, "elevation evaluator") except aerrors.MissingXMLError: pass else: pk = pm[elev] pk.remove(elev) domain_name = pk.getElement("domain name").getValue() elev.setName(domain_name + "-elevation") if elev.isElement("elevation function"): elev.setParameter("field evaluator type", "string", "standalone elevation") for comp in asearch.findall_name(elev, "components"): comp.setValue(["cell", "face"]) else: elev.setParameter("field evaluator type", "string", "meshed elevation") fe.append(elev) try: oc = asearch.find_name(xml, "overland conductivity evaluator") except aerrors.MissingXMLError: pass else: pk = pm[oc] pk.remove(oc) domain_name = pk.getElement("domain name").getValue() oc.setName(domain_name + "-overland_conductivity") oc.setParameter("field evaluator type", "string", "overland conductivity") fe.append(oc)
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 fixSolvers(xml): for diff in asearch.findall_name(xml, "Diffusion"): _fixDiffusion(diff, False, True) need_pc = True for pk in asearch.findall_name(xml, "PK type"): if pk.get("value") == "new permafrost model no SC" or \ pk.get("value") == "subsurface permafrost" or \ pk.get("value") == "coupled water": need_pc = False for diff in asearch.findall_name(xml, "Diffusion PC"): _fixDiffusion(diff, need_pc, True) for solver in asearch.findall_name(xml, "Coupled PC"): _fixPC(solver.sublist("preconditioner"), "boomer amg", True) for solver in asearch.findall_name(xml, "Coupled Solver"): _fixLinOp(solver)
def update(xml, clobber=True): for lin_op_list in asearch.findall_name(xml, "linear solver"): if lin_op_list.getElement("iterative method").getValue() == "nka": lin_op_list.setParameter("iterative method", "string", "gmres") lin_op_list.pop("nka parameters") lin_op_list.append(get_gmres()) elif clobber and lin_op_list.getElement( "iterative method").getValue() == "gmres": lin_op_list.pop("gmres parameters") lin_op_list.append(get_gmres())
def seepage_face_bcs(xml): for bclist in asearch.findall_name(xml, "boundary conditions"): if bclist.isElement("seepage face"): agroup = bclist.sublist("seepage face")[0] if agroup.isElement("boundary pressure"): bclist.sublist("seepage face").set("name", "seepage face pressure") elif agroup.isElement("boundary head"): bclist.sublist("seepage face").set("name", "seepage face head") else: raise RuntimeError("unknown seepage condition")
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 diffusion(xml): for diff in asearch.findall_name(xml, "Diffusion"): diff.set("name", "diffusion") for diff in asearch.findall_name(xml, "Diffusion PC"): diff.set("name", "diffusion preconditioner")
def compressibility(xml): for c in asearch.findall_name(xml, "pore compressibility"): c.set("name", "pore compressibility [Pa^-1]")
def seb(xml): pk_list = asearch.findall_name(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.child_by_name( pk, "PK type").getValue() == "surface balance implicit": if seb_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") seb_pk = pk elif asearch.child_by_name(pk, "PK type").getValue() == "permafrost flow": if flow_sub_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") flow_sub_pk = pk elif asearch.child_by_name( pk, "PK type").getValue() == "overland flow with ice": if flow_surf_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") flow_surf_pk = pk elif asearch.child_by_name( pk, "PK type").getValue() == "three-phase energy": if energy_sub_pk is not None: raise RuntimeError("Cannot deal with SEB changes!") energy_sub_pk = pk elif asearch.child_by_name(pk, "PK type").getValue() == "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.child_by_name(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 = asearch.find_path(xml, ["state", "field evaluators"]) 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.child_by_name(eval_list, "surface-source_molar_density") if molar_dens.isElement("temperature key"): asearch.child_by_name(molar_dens, "temperature key").set("value", "surface-temperature") else: molar_dens.append( parameter.StringParameter("temperature key", "surface-temperature"))
def fixMassSource(xml): # find all pks pks = asearch.find_name(xml, "PKs") for pk in pks: pk_type = asearch.find_name(pk, "PK type") if pk_type.getValue() in [ "richards", "overland flow, pressure basis", "overland flow", "overland flow with ice", "snow distribution", "richards steady state", "permafrost flow" ]: # has source? try: has_source = asearch.find_name(pk, "source term") except aerrors.MissingXMLError: continue if has_source.getValue(): # check for the default key try: source_name = asearch.find_name(pk, "source key") except aerrors.MissingXMLError: try: source_name_suffix = asearch.find_name( pk, "source key suffix") except aerrors.MissingXMLError: # if we are using the default, check to see if # the old default is in the list of evaluators default_val = _get_prefixed_name(pk, "mass_source") try: source_eval = asearch.find_path( xml, ["state", "field evaluators", default_val]) except aerrors.MissingXMLError: # likely this is a Arctic run, and the # source name is now changed to the new # default, do nothing pass else: # we are using the default, change it to the new default prefix_name = _get_prefixed_name( pk, "water_source") source_eval.setName(prefix_name) else: if source_name_suffix.getValue() == "mass_source": # if we found source key suffix, and it is mass_source... default_val = _get_prefixed_name(pk, "mass_source") try: source_eval = asearch.find_path( xml, ["state", "field evaluators", default_val]) except aerrors.MissingXMLError: # likely this is an Arctic run, and # the source name is now changed to # the new default. Change the suffix source_name_suffix.setValue("water_source") else: # change both the suffix and the evaluator source_name_suffix.setValue("water_source") prefix_name = _get_prefixed_name( pk, "water_source") source_eval.setName( _get_prefixed_name(pk, "water_source")) else: if source_name.getValue().endswith("mass_source"): # we found a source key, and its suffix is mass source... try: source_eval = asearch.find_path( xml, [ "state", "field evaluators", source_name.getValue() ]) except aerrors.MissingXMLError: # likely Arctic if '-' in source_name.getValue(): source_domain = source_name.getValue().split( '-')[0] source_name.setValue(source_domain + '-water_source') else: source_name.setValue('water_source') else: source_domain = source_name.getValue().split( '-')[0] source_name.setValue(source_domain + '-water_source') source_eval.setName(source_domain + '-water_source') # changes "mass source in meters" to "water source". Note this # can be in transport, not only in flow. for ws_in_meters in asearch.findall_name(xml, "mass source in meters"): ws_in_meters.setName("water source in meters") # changes "mass source units" to "water source units" in total energy sources fe_list = asearch.find_path(xml, ["state", "field evaluators"]) for eval in fe_list: if asearch.find_name( eval, "field evaluator type").getValue() == "advected energy source": try: units = asearch.find_name(eval, "mass source units") except aerrors.MissingXMLError: pass else: units.setName("water source units")
def fixTIList(xml): for ti_list in asearch.findall_name(xml, [ "time integrator", ]): _fixTIList(ti_list)