| 225 | |
| 226 | |
| 227 | class CGSolver(LinearSolver): |
| 228 | """Preconditioned conjugate gradient method |
| 229 | |
| 230 | Parameters |
| 231 | ---------- |
| 232 | |
| 233 | """ + linear_solver_param_doc + """ |
| 234 | |
| 235 | conjugate : bool = False |
| 236 | If set to True, then the complex inner product is used, else a pseudo inner product that makes CG work with complex symmetric matrices. |
| 237 | """ |
| 238 | name = "CG" |
| 239 | |
| 240 | def __init__(self, *args, |
| 241 | conjugate : bool = False, |
| 242 | abstol : float = None, |
| 243 | maxsteps : int = None, |
| 244 | printing : bool = False, |
| 245 | **kwargs): |
| 246 | if printing: |
| 247 | print("WARNING: printing is deprecated, use printrates instead!") |
| 248 | kwargs["printrates"] = printing |
| 249 | if abstol is not None: |
| 250 | print("WARNING: abstol is deprecated, use atol instead!") |
| 251 | kwargs["abstol"] = abstol |
| 252 | if maxsteps is not None: |
| 253 | print("WARNING: maxsteps is deprecated, use maxiter instead!") |
| 254 | kwargs["maxiter"] = maxsteps |
| 255 | super().__init__(*args, **kwargs) |
| 256 | self.conjugate = conjugate |
| 257 | |
| 258 | # for backward compatibility |
| 259 | @property |
| 260 | def errors(self): |
| 261 | return self.residuals |
| 262 | |
| 263 | def _SolveImpl(self, rhs : BaseVector, sol : BaseVector): |
| 264 | d, w, s = [sol.CreateVector() for i in range(3)] |
| 265 | conjugate = self.conjugate |
| 266 | d.data = rhs - self.mat * sol |
| 267 | w.data = self.pre * d |
| 268 | s.data = w |
| 269 | wdn = w.InnerProduct(d, conjugate=conjugate) |
| 270 | if self.CheckResidual(sqrt(abs(wdn))): |
| 271 | return |
| 272 | |
| 273 | while True: |
| 274 | w.data = self.mat * s |
| 275 | wd = wdn |
| 276 | as_s = s.InnerProduct(w, conjugate=conjugate) |
| 277 | if as_s == 0 or wd == 0: break |
| 278 | alpha = wd / as_s |
| 279 | sol.data += alpha * s |
| 280 | d.data += (-alpha) * w |
| 281 | |
| 282 | w.data = self.pre * d |
| 283 | |
| 284 | wdn = w.InnerProduct(d, conjugate=conjugate) |
no outgoing calls
no test coverage detected