def water_energy(xml): try: water = asearch.childByNamePath(xml, "state/field evaluators/water_content") except aerrors.MissingXMLError: pass else: ftype = asearch.childByNamePath(water, "field evaluator type") if ftype.value == "permafrost water content": if water.isElement("include water vapor") and water.getElement("include water vapor").value: ftype.setValue("three phase water content") else: ftype.setValue("liquid+ice water content") elif ftype.value == "richards water content": if water.isElement("include water vapor") and water.getElement("include water vapor").value: ftype.setValue("liquid+gas water content") else: ftype.setValue("richards water content") try: energy = asearch.childByNamePath(xml, "state/field evaluators/energy") except aerrors.MissingXMLError: pass else: etype = asearch.childByNamePath(energy, "field evaluator type") if ftype.value == "three phase water content": etype.setValue("three phase energy") elif ftype.value == "liquid+ice water content": etype.setValue("liquid+ice energy") elif ftype.value == "liquid+gas water content": etype.setValue("liquid+gas energy") elif ftype.value == "richards water content": etype.setValue("richards energy")
def update(xml, clobber=True): for lin_op_list in asearch.generateElementByNamePath(xml, "linear solver"): if asearch.childByNamePath(lin_op_list, "iterative method").get("value") == "nka": lin_op_list.setParameter("iterative method", "string", "gmres") lin_op_list.pop("nka parameters") lin_op_list.append(get_gmres()) elif clobber and asearch.childByNamePath(lin_op_list, "iterative method").get("value") == "gmres": lin_op_list.pop("gmres parameters") lin_op_list.append(get_gmres())
def update(xml, clobber=True): for lin_op_list in asearch.generateElementByNamePath(xml, "linear solver"): if asearch.childByNamePath(lin_op_list, "iterative method").get("value") == "nka": lin_op_list.setParameter("iterative method", "string", "gmres") lin_op_list.pop("nka parameters") lin_op_list.append(get_gmres()) elif clobber and asearch.childByNamePath( lin_op_list, "iterative method").get("value") == "gmres": lin_op_list.pop("gmres parameters") lin_op_list.append(get_gmres())
def fixEvaluator(xml, name, newname): try: pd = asearch.childByNamePath(xml, "state/field evaluators/%s" % name) except aerrors.MissingXMLError: pass else: pd.setName(newname)
def fixEvaluator(xml, name, newname): try: pd = asearch.childByNamePath(xml, "state/field evaluators/%s"%name) except aerrors.MissingXMLError: pass else: pd.setName(newname)
def fixVerboseObject(xml): for vo in asearch.generateElementByNamePath(xml, "VerboseObject"): vo.set("name", "verbose object") try: vlev = asearch.childByNamePath(vo, "Verbosity Level") except aerrors.MissingXMLError: pass else: vlev.set("name", "verbosity level")
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 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 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 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 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