示例#1
0
def priestley_taylor(xml):
    eval_list = asearch.find_path(xml, ['state', 'field evaluators'])
    for ev in eval_list:
        ev_type = asearch.child_by_name(ev, 'field evaluator type')
        if ev_type.getValue() == 'potential evapotranspiration':
            ev_type.setValue('potential evapotranspiration, Priestley-Taylor')

            # net radiation is the new key, old used shortwave by default
            default = True
            try:
                sw_key = asearch.child_by_name(ev, 'shortwave radiation key')
            except aerrors.MissingXMLError:
                pass
            else:
                sw_key.setName('net radiation key')
                default = False

            try:
                sw_suffix = asearch.child_by_name(
                    ev, 'shortwave radiation key suffix')
            except aerrors.MissingXMLError:
                pass
            else:
                sw_key.setName('net radiation key suffix')
                default = False

            if default:
                ev.append(
                    parameter.StringParameter('net radiation key suffix',
                                              'shortwave_radiation'))

        if ev.getName() == 'surface-air_temperature_inter':
            # this name is dead, now just surface-temperature (which is what it was intended to be physically)
            ev.setName('surface-temperature')
示例#2
0
def snow_distribution(xml):
    for snow_dist_pk in asearch.findall_path(xml,
                                             ["PKs", "snow distribution"]):
        snow_dist_pk.append(
            parameter.DoubleParameter("distribution time", 86400.0))
        if snow_dist_pk.isElement("primary variable key") and \
             asearch.child_by_name(snow_dist_pk,"primary variable key").getValue() == "surface-precipitation_snow" and \
             snow_dist_pk.isElement("conserved quantity key") and \
             asearch.child_by_name(snow_dist_pk,"conserved quantity key").getValue() == "precipitation-snow":
            asearch.child_by_name(snow_dist_pk, "conserved quantity key").set(
                "value", "surface-precipitation-snow")

    for ssk in asearch.findall_path(
            xml, ["state", "field evaluators", "surface-snow_skin_potential"]):
        if not ssk.isElement("dt factor"):
            ssk.append(parameter.DoubleParameter("dt factor", 86400.0))
        else:
            asearch.child_by_name(ssk, "dt factor").set("value", "86400.0")

    for ssc in asearch.findall_path(
            xml, ["state", "field evaluators", "surface-snow_conductivity"]):
        if not ssc.isElement("include dt factor"):
            ssc.append(parameter.BoolParameter("include dt factor", True))
        else:
            asearch.child_by_name(ssc,
                                  "include dt factor").set("value", "true")

        if not ssc.isElement("dt factor"):
            ssc.append(parameter.DoubleParameter("dt factor", 86400.0))
        else:
            asearch.child_by_name(ssc, "dt factor").set("value", "86400.0")
示例#3
0
def snow_depth(xml):
    for seb_pk in asearch.gen_by_path(xml, ["PKs", "SEB"]):
        if seb_pk.isElement("primary variable key") and \
             asearch.child_by_name(seb_pk,"primary variable key").getValue() == "surface-snow_depth" and \
             seb_pk.isElement("conserved quantity key") and \
             asearch.child_by_name(seb_pk,"conserved quantity key").getValue() == "snow_depth":
            asearch.child_by_name(seb_pk, "conserved quantity key").set(
                "value", "surface-snow_depth")
示例#4
0
def seb_threecomponent(xml, eval_name):
    """This changes the old, 'Arctic' SEB model into more robust variant that crashes less often."""
    # 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, three 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, three components")

    # 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("three-component subgrid albedos")
示例#5
0
def alias_mesh_target(xml):
    mesh_list = asearch.child_by_name(xml, "mesh")
    for mesh in mesh_list:
        mtype = asearch.child_by_name(mesh, "mesh type")
        if mtype.getValue() == "aliased":
            try:
                alias = asearch.find_path(mesh,
                                          ["aliased parameters", "alias"])
            except aerrors.MissingXMLError:
                pass
            else:
                alias.setName("target")
示例#6
0
def linear_operator(xml):
    """Changes any instances of "linear operator" to "linear solver",
    which is now standard across all PKs."""

    pks = asearch.child_by_name(xml, "PKs")
    for pk in pks:
        try:
            lin_op = asearch.child_by_name(pk, "linear operator")
        except aerrors.MissingXMLError:
            pass
        else:
            lin_op.setName("linear solver")
示例#7
0
def flatten_pks(xml):
    try:
        cycle_driver = asearch.child_by_name(xml, "cycle driver")
    except aerrors.MissingXMLError:
        pass
    else:
        return

    pks = asearch.child_by_name(xml, "PKs")
    cd_pks = coordinator_to_cycle_driver(xml)
    flat_pks = ParameterList("PKs")
    flatten(pks, flat_pks, cd_pks)
    pks.extend(flat_pks)
示例#8
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))
示例#9
0
    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))
示例#10
0
def coordinator_to_cycle_driver(xml):
    cycle_driver = asearch.child_by_name(xml, "coordinator")
    cycle_driver.set("name", "cycle driver")
    cycle_driver_pks = ParameterList("PK tree")
    cycle_driver.append(cycle_driver_pks)

    return cycle_driver_pks
示例#11
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))
示例#12
0
def update(xml):
    linear_operator(xml)
    max_valid_change(xml)
    bad_spinup_longwave(xml)
    sources(xml)

    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') == '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)

    import verbose_object
    verbose_object.fixVerboseObject(xml)
示例#13
0
def create_landcover(xml,
                     eval_name='water_source',
                     water_transition_depth=0.02):
    """Adds a default land-cover section that will be the same as the Arctic defaults."""
    ic_list = asearch.find_path(xml, ['state', 'initial conditions'])
    try:
        lc = asearch.child_by_name(ic_list, "land cover types")
    except aerrors.MissingXMLError:
        pass
    else:
        return  # already exists

    # create a single land-cover type on the entire surface domain
    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))

    lc = ic_list.sublist('land cover types').sublist('surface domain')
    add_to_lc('water_source', 'dessicated zone thickness [m]', 0.1, lc)
    add_to_lc('water_source', 'roughness length of bare ground [m]', 0.04, lc)
    add_to_lc('water_source', 'roughness length of snow-covered ground [m]',
              0.004, lc, 'roughness length of snow [m]')
    add_to_lc('water_source', 'snow-ground transitional depth [m]', 0.02, lc,
              'snow transition depth [m]')
    add_to_lc('water_source', 'water-ground transitional depth [m]',
              water_transition_depth, lc, 'water transition depth [m]')
    add_to_lc('surface-subgrid_albedos', 'albedo ground surface [-]', 0.135,
              lc, 'albedo of bare ground [-]')
    add_to_lc('surface-subgrid_albedos', 'emissivity tundra [-]', 0.92, lc,
              'emissivity of bare ground [-]')
示例#14
0
    def getCoordinateFromList(self, one_list):
        well_name = one_list.getElement("Region").value
        region = search.child_by_name(
            search.find_path(self.xml, [
                "amanzi_input",
                "regions",
            ], False), well_name)

        try:
            coordinate = region.get("coordinate")
        except KeyError:
            raise RuntimeError("Region is not of type point")
        return coordinate
示例#15
0
def lower_case(xml):
    """Converts an xml object, in-place"""

    try:
        regions = asearch.child_by_name(xml, "Regions")
    except aerrors.MissingXMLError:
        pass

    else:
        for label in changes_regions:
            asearch.change_name(regions,
                                label,
                                label.lower(),
                                allow_multiple=True)

        asearch.change_name(xml, ["Region: Plane", "location"],
                            "point",
                            allow_multiple=True)
        asearch.change_name(xml, ["Region: Plane", "direction"],
                            "normal",
                            allow_multiple=True)
        regions.setName("regions")

    try:
        mesh = asearch.child_by_name(xml, "Mesh")
    except aerrors.MissingXMLError:
        pass
    else:
        for label in changes_mesh:
            asearch.change_name(mesh, label, label.lower(), True)
        mesh.setName("mesh")

    try:
        domain = asearch.child_by_name(xml, "Domain")
    except aerrors.MissingXMLError:
        pass
    else:
        xml.remove(domain)
示例#16
0
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.child_by_name(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)
示例#17
0
def vis(xml):
    if xml.isElement("visualization") and not asearch.child_by_name(
            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)
示例#18
0
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"))
示例#19
0
def snow_distribution(xml):
    for snow_dist_pk in asearch.gen_path(xml, ["PKs", "snow distribution"]):
        if snow_dist_pk.isElement("primary variable key") and \
             asearch.child_by_name(snow_dist_pk,"primary variable key").getValue() == "surface-precipitation_snow":
            asearch.child_by_name(
                snow_dist_pk,
                "primary variable key").setValue("snow-precipitation")
        if snow_dist_pk.isElement("conserved quantity key") and \
             asearch.child_by_name(snow_dist_pk,"conserved quantity key").getValue() == "surface-precipitation_snow":
            asearch.child_by_name(
                snow_dist_pk,
                "conserved quantity key").setValue("snow-precipitation")
        if snow_dist_pk.isElement("domain name") and \
           asearch.child_by_name(snow_dist_pk,"domain name").getValue() == "surface":
            asearch.child_by_name(snow_dist_pk, "domain name").setValue("snow")

    for ssk in asearch.find_path(
            xml, ["state", "field evaluators", "snow-conductivity"]):
        if ssk.isElement("height key"):
            asearch.child_by_name(ssk,
                                  "height key").setValue("snow-precipitation")
示例#20
0
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")
示例#21
0
 def getElement(self, name):
     """Get Parameter/sublist from the ParameterList"""
     return search.child_by_name(self, name)
示例#22
0
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.child_by_name(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"):
            print(el)
            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.child_by_name(
                        el, valid_type).setName(valid_type + " parameters")
                    found = True
                    break
            assert (found)

    return
示例#23
0
 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")
示例#24
0
def snow_distribution(xml):
    try:
        snow_dist_pk = asearch.find_path(xml, ["PKs","snow distribution"])
    except aerrors.MissingXMLError:
        pass
    else:
        if snow_dist_pk.isElement("primary variable key") and \
             asearch.child_by_name(snow_dist_pk,"primary variable key").getValue() == "surface-precipitation_snow":
            asearch.child_by_name(snow_dist_pk,"primary variable key").setValue("snow-precipitation")
        if snow_dist_pk.isElement("conserved quantity key") and \
             asearch.child_by_name(snow_dist_pk,"conserved quantity key").getValue() == "surface-precipitation_snow":
            asearch.child_by_name(snow_dist_pk,"conserved quantity key").setValue("snow-precipitation")
        if snow_dist_pk.isElement("domain name") and \
           asearch.child_by_name(snow_dist_pk,"domain name").getValue() == "surface":
            asearch.child_by_name(snow_dist_pk,"domain name").setValue("snow")
    
    try:
        ssk = asearch.find_path(xml, ["state","field evaluators","snow-conductivity"])
    except aerrors.MissingXMLError:
        pass
    else:
        if ssk.isElement("height key"):
            asearch.child_by_name(ssk, "height key").setValue("snow-precipitation")