def test_solver_user_control_func(): """Test the use of the user control function. Here, change the EWMA parameter in the course of solving at specific iterations. """ L, N, dx, x, nL, n = initialize_shestakov_problem() junk, lmParams, junk2 = initialize_parameters() maxIterations = 10 tol = 1e-9 fluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbHandler = tng.lodestro_method.TurbulenceHandler(dx, x, lmParams, fluxModel) compute_all_H = ComputeAllH(turbHandler) t_array = np.array([0, 1e4]) # specify the timesteps to be used. user_control_func = UserControlFunc(turbHandler) solver = tng.solver.Solver(L, x, n, nL, t_array, maxIterations, tol, compute_all_H, turbHandler, user_control_func) expEWMAParamStart = lmParams['EWMAParamTurbFlux'] (obsEWMAParamStart, junk) = turbHandler.get_ewma_params() assert expEWMAParamStart == obsEWMAParamStart while solver.ok: # Implicit time advance: iterate to solve the nonlinear equation! solver.take_timestep() expEWMAParamFinish = 0.13 (obsEWMAParamFinish, junk) = turbHandler.get_ewma_params() assert expEWMAParamFinish == obsEWMAParamFinish
def test_solver_small_ewma_param(): """Test that proper convergence is reached for small EWMA parameters. Previously, a bug prevented full convergence for EWMAParam <~ 0.01 but worked at larger values.""" L, N, dx, x, nL, n = initialize_shestakov_problem() junk, lmParams, junk2 = initialize_parameters() # adjust the EWMA parameter EWMAParam = 0.01 lmParams['EWMAParamTurbFlux'] = EWMAParam lmParams['EWMAParamProfile'] = EWMAParam maxIterations = 4000 tol = 1e-9 fluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbHandler = tng.lodestro_method.TurbulenceHandler(dx, x, lmParams, fluxModel) compute_all_H = ComputeAllH(turbHandler) t_array = np.array([0, 1e4]) # specify the timesteps to be used. solver = tng.solver.Solver(L, x, n, nL, t_array, maxIterations, tol, compute_all_H, turbHandler) while solver.ok: # Implicit time advance: iterate to solve the nonlinear equation! solver.take_timestep() selfConsistencyErrorFinal = solver.errHistoryFinal[-1] assert selfConsistencyErrorFinal <= tol
def problem_setup(): L, N, dx, x, nL, n = initialize_shestakov_problem() MaxIterations, lmparams, tol = initialize_parameters() FluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbhandler = tng.lodestro_method.TurbulenceHandler(dx, x, lmparams, FluxModel) compute_all_H = ComputeAllH(turbhandler) t_array = np.array([0, 1e4]) # specify the timesteps to be used. return (L, N, dx, x, nL, n, MaxIterations, tol, turbhandler, compute_all_H, t_array)
def regular_solution(): L, N, dx, x, nL, n = initialize_shestakov_problem() maxIterations, lmparams, tol = initialize_parameters() fluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbhandler = tng.lodestro_method.TurbulenceHandler(dx, x, lmparams, fluxModel) t_array = np.array([0, 1e4]) # specify the timesteps to be used. compute_all_H = ComputeAllHNoBuffer(turbhandler) solver = tng.solver.Solver(L, x, n, nL, t_array, maxIterations, tol, compute_all_H, turbhandler) arraysToSave = ['H2', 'H3', 'profile', 'fluxTurbGrid', 'xTurbGrid', 'DTurbGrid'] # for list of possible arrays, see solver._pkgdata() dataBasename = 'nobuffer' solver.dataSaverHandler.initialize_datasaver(dataBasename, maxIterations, arraysToSave) while solver.ok: # Implicit time advance: iterate to solve the nonlinear equation! solver.take_timestep() nSteadyState = solver.profile return nSteadyState
logging.info("Initializing...") L, N, dx, x, nL, n = initialize_shestakov_problem() MaxIterations, lmparams, tol = initialize_parameters() arrays_to_save = ['H2', 'H3', 'n'] def pkg_data(H2=None, H3=None, n=None): data = {'H2': H2, 'H3': H3, 'n':n} return data dataSaver = tng.datasaver.DataSaver(MaxIterations, arrays_to_save) FluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbhandler = tng.lodestro_method.TurbulenceHandler(dx, x, lmparams, FluxModel) errhistory = np.zeros(MaxIterations-1) # error history vs. iteration at a given timestep t = np.array([0, 1e4]) # specify the timesteps to be used. n_mminus1 = n # initialize "m minus 1" variables for the first timestep logging.info("Initialization complete.") logging.info("Beginning time integration...") for m in range(1, len(t)): # Implicit time advance: iterate to solve the nonlinear equation! converged = False dt = Get_dt(t, m) l = 1 # reset iteration counter errhistory[:] = 0 logging.info("Beginning iteration loop for timestep {}...".format(m))
def test_lodestro_shestakovproblem(): # test the LoDestro Method on the analytic shestakov problem # setup L = 1 # size of domain N = 500 # number of spatial grid points dx = L / (N-1) # spatial grid size x = np.arange(N)*dx # location corresponding to grid points j=0, ..., N-1 nL = 1e-2 # right boundary condition lmax = 2000 # max number of iterations dt = 1e4 # timestep thetaParams = {'Dmin': 1e-5, 'Dmax': 1e13, 'dpdxThreshold': 10} lmParams = {'EWMAParamTurbFlux': 0.30, 'EWMAParamProfile': 0.30, 'thetaParams': thetaParams} FluxModel = shestakov_nonlinear_diffusion.shestakov_analytic_fluxmodel(dx) turbhandler = lodestro_method.TurbulenceHandler(dx, x, lmParams, FluxModel) # initial condition n = 1 - 0.5*x tol = 1e-11 # tol for convergence t = np.array([0, 1e6]) # initialize "m minus 1" variables for the first timestep n_mminus1 = n for m in range(1, len(t)): # Implicit time advance: iterate to solve the nonlinear equation! dt = t[m] - t[m-1] # use this if using non-constant timesteps converged = False l = 1 while not converged: # compute H's from current iterate n H1 = np.ones_like(x) H7 = shestakov_nonlinear_diffusion.H7contrib_Source(x) # this needs to be packaged... give n, get out H2, H3. (H2, H3, data) = turbhandler.Hcontrib_turbulent_flux(n) # compute matrix system (A, B, C, f) (A, B, C, f) = HToMatrixFD.H_to_matrix(dt, dx, nL, n_mminus1, H1, H2=H2, H3=H3, H7=H7) # check convergence # convergence check: is || ( M[n^l] n^l - f[n^l] ) / max(abs(f)) || < tol # could add more convergence checks here resid = A*np.concatenate((n[1:], np.zeros(1))) + B*n + C*np.concatenate((np.zeros(1), n[:-1])) - f resid = resid / np.max(np.abs(f)) # normalize residuals rms_error = np.sqrt( 1/len(resid) * np.sum(resid**2)) if rms_error < tol: converged = True # compute new iterate n n = HToMatrixFD.solve(A, B, C, f) # Check for NaNs or infs if np.all(np.isfinite(n)) == False: raise RuntimeError('NaN or Inf detected at l=%d. Exiting...' % (l)) # about to loop to next iteration l l += 1 if l >= lmax: raise RuntimeError('Too many iterations on timestep %d. Error is %f.' % (m, rms_error)) # end of while loop for iteration convergence # Converged. Before advancing to next timestep m, save some stuff n_mminus1 = n nss_analytic = shestakov_nonlinear_diffusion.GetSteadyStateSolution(x, nL) solution_residual = (n - nss_analytic) / np.max(np.abs(nss_analytic)) solution_rms_error = np.sqrt( 1/len(n) * np.sum(solution_residual**2)) obs = solution_rms_error exp = 0 testtol = 1e-3 assert abs(obs - exp) < testtol