diff --git a/examples/7_MHD/mhd.py b/examples/7_MHD/mhd.py index 02b90fb..bb8482c 100644 --- a/examples/7_MHD/mhd.py +++ b/examples/7_MHD/mhd.py @@ -1,73 +1,73 @@ import numpy as np import matplotlib.pyplot as plt from mhd_engine import MHDEngine as engine from rrompy.reduction_methods import (RationalInterpolant as RI, RationalInterpolantGreedy as RIG) from rrompy.parameter.parameter_sampling import (FFTSampler as FFTS, QuadratureCircleSampler as QCS, QuadratureBoxSampler as QBS) ks = [-.35 + .5j, 0. + .5j] k0 = np.mean(ks) solver = engine(5) kEffDelta = .1 * (ks[1] - ks[0]) kEff = np.real([ks[0] - kEffDelta, ks[1] + kEffDelta]) iEff = kEff - .5 * np.sum(np.real(ks)) + np.imag(ks[0]) nPoles = 50 polesEx = solver.getPolesExact(nPoles, k0) for corrector in [False, True]: for method in ["FFT", "BOX", "GREEDY"]: print("Testing {} method with{} corrector".format(method, "out" * (not corrector))) if method == "FFT": params = {'S':64, 'POD':True, 'polybasis':"MONOMIAL", - 'sampler':FFTS(ks), 'correctorTol':1e-5} + 'sampler':FFTS(ks), 'residueTol':1e-5} algo = RI if method == "BOX": params = {'S':64, 'POD':True, 'polybasis':"MONOMIAL", - 'sampler':QBS(ks), 'correctorTol':1e-5} + 'sampler':QBS(ks), 'residueTol':1e-5} algo = RI if method == "GREEDY": params = {'S':30, 'POD':True, 'greedyTol':1e-2, 'polybasis':"MONOMIAL", 'sampler':QCS(ks), 'errorEstimatorKind':"LOOK_AHEAD", 'nTestPoints':10000, - 'trainSetGenerator':FFTS(ks), 'correctorTol':1e-5} + 'trainSetGenerator':FFTS(ks), 'residueTol':1e-5} algo = RIG params['correctorForce'] = corrector approx = algo(solver, mu0 = k0, approx_state = True, approxParameters = params, verbosity = 10) approx.setupApprox() poles, residues = approx.getResidues() inRange = np.logical_and( np.logical_and(np.real(poles) >= kEff[0], np.real(poles) <= kEff[1]), np.logical_and(np.imag(poles) >= iEff[0], np.imag(poles) <= iEff[1])) polesEff = poles[inRange] resNormEff = np.linalg.norm(residues, axis = 1)[inRange] rLm = np.min(np.log(resNormEff)) rLmM = np.max(np.log(resNormEff)) - rLm fig = plt.figure(figsize = (10, 10)) ax = fig.add_subplot(1, 1, 1) if method == "GREEDY": ax.plot(approx.muTest.re.data.flatten(), approx.muTest.im.data.flatten(), 'k,', alpha = 0.25) for pl, rN in zip(polesEff, resNormEff): if corrector: alpha = .35 + .4 * (np.log(rN) - rLm) / rLmM else: alpha = .1 + .65 * (np.log(rN) - rLm) / rLmM ax.annotate("{0:.0e}".format(rN), (np.real(pl), np.imag(pl)), alpha = alpha) ax.plot(np.real(pl), np.imag(pl), 'r+', alpha = alpha + .25) ax.plot(approx.mus.re.data.flatten(), approx.mus.im.data.flatten(), 'k.') ax.plot(np.real(polesEx), np.imag(polesEx), 'bx') ax.set_xlim(kEff) ax.set_ylim(iEff) ax.grid() plt.tight_layout() plt.show() print("\n") diff --git a/rrompy/utilities/base/verbosity_depth.py b/rrompy/utilities/base/verbosity_depth.py index a9e000a..db51c7d 100644 --- a/rrompy/utilities/base/verbosity_depth.py +++ b/rrompy/utilities/base/verbosity_depth.py @@ -1,96 +1,97 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # from copy import deepcopy as copy from datetime import datetime from rrompy.utilities.exception_manager import RROMPyException __all__ = ["verbosityDepth", "verbosityManager"] def getTimestamp() -> str: time = datetime.now().strftime("%H:%M:%S.%f") return "\x1b[42m{}\x1b[0m".format(time) def updateVerbosityCheckpoint(vctype:int) -> str: global RROMPy_verbosity_checkpoint, RROMPy_verbosity_buffer if "RROMPy_verbosity_checkpoint" not in globals(): RROMPy_verbosity_checkpoint = 0 RROMPy_verbosity_checkpoint += vctype if "RROMPy_verbosity_buffer" not in globals(): RROMPy_verbosity_buffer = "" if RROMPy_verbosity_checkpoint <= 0: buffer = copy(RROMPy_verbosity_buffer) del RROMPy_verbosity_buffer return buffer return None def getVerbosityDepth() -> int: global RROMPy_verbosity_depth if "RROMPy_verbosity_depth" not in globals(): return 0 return RROMPy_verbosity_depth def setVerbosityDepth(depth): global RROMPy_verbosity_depth if depth <= 0: - del RROMPy_verbosity_depth + if "RROMPy_verbosity_depth" in globals(): + del RROMPy_verbosity_depth else: RROMPy_verbosity_depth = depth def verbosityDepth(vdtype:str, message:str, end : str = "\n", timestamp : bool = True): global RROMPy_verbosity_depth, RROMPy_verbosity_checkpoint, \ RROMPy_verbosity_buffer assert isinstance(vdtype, str) vdtype = vdtype.upper() if vdtype not in ["INIT", "MAIN", "DEL"]: raise RROMPyException("Verbosity depth type not recognized.") if "RROMPy_verbosity_checkpoint" not in globals(): RROMPy_verbosity_checkpoint = 0 if vdtype == "INIT": if "RROMPy_verbosity_depth" not in globals(): setVerbosityDepth(1) else: setVerbosityDepth(RROMPy_verbosity_depth + 1) assert "RROMPy_verbosity_depth" in globals() out = "{} ".format(getTimestamp()) if timestamp else "" out += "│" * (RROMPy_verbosity_depth - 1) if vdtype == "INIT": out += "┌" elif vdtype == "MAIN": out += "├" else: #if vdtype == "DEL": setVerbosityDepth(RROMPy_verbosity_depth - 1) out += "└" from rrompy.utilities.parallel import poolRank, poolSize, masterCore if message != "" and masterCore(): if RROMPy_verbosity_checkpoint and poolSize() > 1: poolrk = "{{\x1b[34m{}\x1b[0m}}".format(poolRank()) else: poolrk = "" msg = "{}{}{}{}".format(out, poolrk, message, end) if RROMPy_verbosity_checkpoint: assert "RROMPy_verbosity_buffer" in globals() RROMPy_verbosity_buffer += msg else: print(msg, end = "", flush = True) return def verbosityManager(object, vdtype:str, message:str, vlvl : int = 0, end : str = "\n"): if object.verbosity >= vlvl: return verbosityDepth(vdtype, message, end, object.timestamp) diff --git a/setup.cfg b/setup.cfg index a95dc41..9078760 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,10 +1,10 @@ [aliases] test=pytest [tool:pytest] -addopts = --tb=short +addopts = --tb=short --runxfail python_files = tests/*/*.py filterwarnings = error ignore::DeprecationWarning ignore::FutureWarning