def _debug_g(self, y_idx): """ Print out the associated variables with the given algebraic equation index. Parameters ---------- y_idx Index of the equation into the `g` array. Diff. eqns. are not counted in. """ y_idx = y_idx.tolist() logger.debug('Max. algebraic mismatch associated with <%s> [y_idx=%d]', self.system.dae.y_name[y_idx], y_idx) assoc_vars = self.system.dae.gy[y_idx, :] vars_idx = np.where(np.ravel(matrix(assoc_vars)))[0] logger.debug('') logger.debug(f'{"y_index":<10} {"Variable":<20} {"Derivative":<20}') for v in vars_idx: v = v.tolist() logger.debug('%10d %20s %20g', v, self.system.dae.y_name[v], assoc_vars[v])
def test_init(self): """ Update f and g to see if initialization is successful. """ system = self.system self.fg_update(system.exist.pflow_tds) system.j_update(models=system.exist.pflow_tds) # warn if variables are initialized at limits if system.config.warn_limits: for model in system.exist.pflow_tds.values(): for item in model.discrete.values(): item.warn_init_limit() if np.max(np.abs(system.dae.fg)) < self.config.tol: logger.debug('Initialization tests passed.') return True # otherwise, show suspect initialization error fail_idx = np.where(abs(system.dae.fg) >= self.config.tol) fail_names = [system.dae.xy_name[int(i)] for i in np.ravel(fail_idx)] title = 'Suspect initialization issue! Simulation may crash!' err_data = { 'Name': fail_names, 'Var. Value': system.dae.xy[fail_idx], 'Eqn. Mismatch': system.dae.fg[fail_idx], } tab = Tab( title=title, header=err_data.keys(), data=list(map(list, zip(*err_data.values()))), ) logger.error(tab.draw()) if system.options.get('verbose') == 1: breakpoint() system.exit_code += 1 return False
def store_switch_times(self, models=None): """ Store event switching time in a sorted Numpy array at ``System.switch_times``. Returns ------- array-like self.switch_times """ models = self._get_models(models) out = [] for instance in models.values(): out.extend(instance.get_times()) out = np.ravel(np.array(out)) out = np.unique(out) out = out[np.where(out >= 0)] out = np.sort(out) self.switch_times = out return self.switch_times
def store_switch_times(self, models): """ Store event switching time in a sorted Numpy array at ``System.switch_times``. Returns ------- array-like self.switch_times """ out = [] for instance in models.values(): out.extend(instance.get_times()) out = np.ravel(np.array(out)) out = np.append(out, out + 1e-4) out = np.unique(out) out = out[np.where(out >= 0)] out = np.sort(out) self.switch_times = out self.n_switches = len(self.switch_times) return self.switch_times
def test_initialization(self): """ Update f and g to see if initialization is successful. """ system = self.system system.e_clear(models=self.pflow_tds_models) system.l_update_var(models=self.pflow_tds_models) system.f_update(models=self.pflow_tds_models) system.g_update(models=self.pflow_tds_models) system.l_check_eq(models=self.pflow_tds_models) system.l_set_eq(models=self.pflow_tds_models) system.fg_to_dae() system.j_update(models=self.pflow_tds_models) if np.max(np.abs(system.dae.fg)) < self.config.tol: logger.debug('Initialization tests passed.') return True else: logger.error('Suspect initialization issue!') fail_idx = np.where(abs(system.dae.fg) >= self.config.tol) fail_names = [system.dae.xy_name[int(i)] for i in np.ravel(fail_idx)] logger.error(f"Check variables {', '.join(fail_names)}") return False
def check_eq(self): """ Check the variables and equations and set the limiter flags. Reset differential equation values based on limiter flags. Notes ----- The current implementation reallocates memory for `self.x_set` in each call. Consider improving for speed. (TODO) """ self.zu[:] = np.logical_and(np.greater_equal(self.u.v, self.upper.v), np.greater_equal(self.state.e, 0)) self.zl[:] = np.logical_and(np.less_equal(self.u.v, self.lower.v), np.less_equal(self.state.e, 0)) self.zi[:] = np.logical_not(np.logical_or(self.zu, self.zl)) # must flush the `x_set` list at the beginning self.x_set = list() if not np.all(self.zi): idx = np.where(self.zi == 0) self.state.e[:] = self.state.e * self.zi self.state.v[:] = self.state.v * self.zi + self.upper.v * self.zu + self.lower.v * self.zl self.x_set.append((self.state.a[idx], self.state.v[idx]))
def _implicit_step(self): """ Integrate for a single given step. This function has an internal Newton-Raphson loop for algebraized semi-explicit DAE. The function returns the convergence status when done but does NOT progress simulation time. Returns ------- bool Convergence status in ``self.converged``. """ system = self.system dae = self.system.dae self.mis = [] self.niter = 0 self.converged = False self.x0 = np.array(dae.x) self.y0 = np.array(dae.y) self.f0 = np.array(dae.f) while True: system.e_clear(models=self.pflow_tds_models) system.l_update_var(models=self.pflow_tds_models) system.f_update(models=self.pflow_tds_models) system.g_update(models=self.pflow_tds_models) system.l_check_eq(models=self.pflow_tds_models) system.l_set_eq(models=self.pflow_tds_models) system.fg_to_dae() # lazy jacobian update if dae.t == 0 or self.niter > 3 or (dae.t - self._last_switch_t < 0.2): system.j_update(models=self.pflow_tds_models) self.solver.factorize = True # solve trapezoidal rule integration In = spdiag([1] * dae.n) self.Ac = sparse([[In - self.h * 0.5 * dae.fx, dae.gx], [-self.h * 0.5 * dae.fy, dae.gy]], 'd') # reset q as well q = dae.x - self.x0 - self.h * 0.5 * (dae.f + self.f0) for item in system.antiwindups: if len(item.x_set) > 0: for key, val in item.x_set: np.put(q, key[np.where(item.zi == 0)], 0) qg = np.hstack((q, dae.g)) inc = self.solver.solve(self.Ac, -matrix(qg)) # check for np.nan first if np.isnan(inc).any(): logger.error(f'NaN found in solution. Convergence not likely') self.niter = self.config.max_iter + 1 self.busted = True break # reset really small values to avoid anti-windup limiter flag jumps inc[np.where(np.abs(inc) < 1e-12)] = 0 # set new values dae.x += np.ravel(np.array(inc[:dae.n])) dae.y += np.ravel(np.array(inc[dae.n: dae.n + dae.m])) system.vars_to_models() # calculate correction mis = np.max(np.abs(inc)) self.mis.append(mis) self.niter += 1 # converged if mis <= self.config.tol: self.converged = True break # non-convergence cases if self.niter > self.config.max_iter: logger.debug(f'Max. iter. {self.config.max_iter} reached for t={dae.t:.6f}, ' f'h={self.h:.6f}, mis={mis:.4g} ' f'({system.dae.xy_name[np.argmax(inc)]})') break if mis > 1000 and (mis > 1e8 * self.mis[0]): logger.error(f'Error increased too quickly. Convergence not likely.') self.busted = True break if not self.converged: dae.x = np.array(self.x0) dae.y = np.array(self.y0) dae.f = np.array(self.f0) system.vars_to_models() return self.converged
def _itm_step(self): """ Integrate with Implicit Trapezoidal Method (ITM) to the current time. This function has an internal Newton-Raphson loop for algebraized semi-explicit DAE. The function returns the convergence status when done but does NOT progress simulation time. Returns ------- bool Convergence status in ``self.converged``. """ system = self.system dae = self.system.dae self.mis = 1 self.niter = 0 self.converged = False self.x0 = np.array(dae.x) self.y0 = np.array(dae.y) self.f0 = np.array(dae.f) while True: self._fg_update(models=system.exist.pflow_tds) # lazy Jacobian update if dae.t == 0 or \ self.config.honest or \ self.custom_event or \ not self.last_converged or \ self.niter > 4 or \ (dae.t - self._last_switch_t < 0.1): system.j_update(models=system.exist.pflow_tds) # set flag in `solver.worker.factorize`, not `solver.factorize`. self.solver.worker.factorize = True # `Tf` should remain constant throughout the simulation, even if the corresponding diff. var. # is pegged by the anti-windup limiters. # solve implicit trapezoidal method (ITM) integration self.Ac = sparse([[self.Teye - self.h * 0.5 * dae.fx, dae.gx], [-self.h * 0.5 * dae.fy, dae.gy]], 'd') # equation `self.qg[:dae.n] = 0` is the implicit form of differential equations using ITM self.qg[:dae.n] = dae.Tf * (dae.x - self.x0) - self.h * 0.5 * (dae.f + self.f0) # reset the corresponding q elements for pegged anti-windup limiter for item in system.antiwindups: for key, _, eqval in item.x_set: np.put(self.qg, key, eqval) self.qg[dae.n:] = dae.g if not self.config.linsolve: inc = self.solver.solve(self.Ac, matrix(self.qg)) else: inc = self.solver.linsolve(self.Ac, matrix(self.qg)) # check for np.nan first if np.isnan(inc).any(): self.err_msg = 'NaN found in solution. Convergence is not likely' self.niter = self.config.max_iter + 1 self.busted = True break # reset small values to reduce chattering inc[np.where(np.abs(inc) < self.tol_zero)] = 0 # set new values dae.x -= inc[:dae.n].ravel() dae.y -= inc[dae.n: dae.n + dae.m].ravel() # store `inc` to self for debugging self.inc = inc system.vars_to_models() # calculate correction mis = np.max(np.abs(inc)) # store initial maximum mismatch if self.niter == 0: self.mis = mis self.niter += 1 # converged if mis <= self.config.tol: self.converged = True break # non-convergence cases if self.niter > self.config.max_iter: tqdm.write(f'* Max. iter. {self.config.max_iter} reached for t={dae.t:.6f}, ' f'h={self.h:.6f}, mis={mis:.4g} ') # debug helpers g_max = np.argmax(abs(dae.g)) inc_max = np.argmax(abs(inc)) self._debug_g(g_max) self._debug_ac(inc_max) break if mis > 1e6 and (mis > 1e6 * self.mis): self.err_msg = 'Error increased too quickly. Convergence not likely.' self.busted = True break if not self.converged: dae.x[:] = np.array(self.x0) dae.y[:] = np.array(self.y0) dae.f[:] = np.array(self.f0) system.vars_to_models() self.last_converged = self.converged return self.converged
def check_eq(self): if not self.enable: return self.u.v[np.where(self.u.v < self.lower.v)] = self.lower.v self.u.v[np.where(self.u.v > self.upper.v)] = self.upper.v
def _itm_step(self): """ Integrate with Implicit Trapezoidal Method (ITM) to the current time. This function has an internal Newton-Raphson loop for algebraized semi-explicit DAE. The function returns the convergence status when done but does NOT progress simulation time. Returns ------- bool Convergence status in ``self.converged``. """ system = self.system dae = self.system.dae self.mis = 1 self.niter = 0 self.converged = False self.x0 = np.array(dae.x) self.y0 = np.array(dae.y) self.f0 = np.array(dae.f) while True: self._fg_update(models=system.exist.pflow_tds) # lazy Jacobian update if dae.t == 0 or self.niter > 3 or (dae.t - self._last_switch_t < 0.2): system.j_update(models=system.exist.pflow_tds) self.solver.factorize = True # TODO: set the `Tf` corresponding to the pegged anti-windup limiters to zero. # Although this should not affect anything since corr. mismatches in `self.qg` are reset to zero # solve implicit trapezoidal method (ITM) integration self.Ac = sparse([[self.Teye - self.h * 0.5 * dae.fx, dae.gx], [-self.h * 0.5 * dae.fy, dae.gy]], 'd') # equation `self.qg[:dae.n] = 0` is the implicit form of differential equations using ITM self.qg[:dae.n] = dae.Tf * (dae.x - self.x0) - self.h * 0.5 * (dae.f + self.f0) # reset the corresponding q elements for pegged anti-windup limiter for item in system.antiwindups: for key, val in item.x_set: np.put(self.qg, key, 0) self.qg[dae.n:] = dae.g if not self.config.linsolve: inc = self.solver.solve(self.Ac, -matrix(self.qg)) else: inc = self.solver.linsolve(self.Ac, -matrix(self.qg)) # check for np.nan first if np.isnan(inc).any(): self.err_msg = 'NaN found in solution. Convergence not likely' self.niter = self.config.max_iter + 1 self.busted = True break # reset small values to reduce chattering inc[np.where(np.abs(inc) < self.tol_zero)] = 0 # set new values dae.x += inc[:dae.n].ravel() dae.y += inc[dae.n: dae.n + dae.m].ravel() system.vars_to_models() # calculate correction mis = np.max(np.abs(inc)) if self.niter == 0: self.mis = mis self.niter += 1 # converged if mis <= self.config.tol: self.converged = True break # non-convergence cases if self.niter > self.config.max_iter: logger.debug(f'Max. iter. {self.config.max_iter} reached for t={dae.t:.6f}, ' f'h={self.h:.6f}, mis={mis:.4g} ') # debug helpers g_max = np.argmax(abs(dae.g)) inc_max = np.argmax(abs(inc)) self._debug_g(g_max) self._debug_ac(inc_max) break if mis > 1000 and (mis > 1e8 * self.mis): self.err_msg = 'Error increased too quickly. Convergence not likely.' self.busted = True break if not self.converged: dae.x = np.array(self.x0) dae.y = np.array(self.y0) dae.f = np.array(self.f0) system.vars_to_models() return self.converged