-
Notifications
You must be signed in to change notification settings - Fork 5
Perf: parallelize the construction of Jacobian and RHS-vector in Poisson solving problem #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
43954c7
6215bce
7d4bd2e
b0e149f
fa97971
fdcdcc5
eb9bd64
53c5f6e
722540a
c33fd56
e1b0195
f3ca2ae
b559308
eba14c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,251 @@ | ||||||||
| import logging | ||||||||
| from typing import Optional, Tuple, List, Callable | ||||||||
|
|
||||||||
| import numpy as np | ||||||||
| from scipy.sparse import lil_matrix | ||||||||
|
|
||||||||
| log = logging.getLogger(__name__) | ||||||||
|
|
||||||||
| def _impose_j_bound(inout, nx, ny, nz, typ, val, mask): | ||||||||
| '''impose the special mask for boundary points''' | ||||||||
| # Neumann boundary types | ||||||||
| neumann_types_ = ['xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax'] | ||||||||
| displ_ = [+1, -1, +nx, -nx, +nx*ny, -nx*ny] | ||||||||
| for t, d in zip(neumann_types_, displ_): | ||||||||
| i = np.where(np.array(typ) == t)[0] | ||||||||
| if len(i) == 0: | ||||||||
| log.warning(f'no grid point with type {t} found') | ||||||||
| # scipy sparse matrix also supports the parallelized assignment | ||||||||
| inout[i, i] = val | ||||||||
| inout[i, i + d] = mask | ||||||||
|
|
||||||||
| # Dirichlet boundary | ||||||||
| i = np.where(typ == 'Dirichlet')[0] | ||||||||
| inout[i, i] = val | ||||||||
|
|
||||||||
| def _impose_b_bound(inout, nx, ny, nz, typ, phi, dirichlet_pot): | ||||||||
| '''impose the special mask for boundary points in b vector''' | ||||||||
| # Neumann boundary types | ||||||||
| neumann_types_ = ['xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax'] | ||||||||
| displ_ = [+1, -1, +nx, -nx, +nx*ny, -nx*ny] | ||||||||
| for t, d in zip(neumann_types_, displ_): | ||||||||
| i = np.where(typ == t)[0] | ||||||||
| if len(i) == 0: | ||||||||
| log.warning(f'no grid point with type {t} found') | ||||||||
| inout[i] = phi[i] - phi[i + d] | ||||||||
|
|
||||||||
| # Dirichlet boundary | ||||||||
| i = np.where(typ == 'Dirichlet')[0] | ||||||||
| inout[i] = phi[i] - dirichlet_pot[i] | ||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||
|
|
||||||||
| def coulomb(chr: float|np.ndarray) -> float|np.ndarray: | ||||||||
| '''convert the charge to unit of Coulomb''' | ||||||||
| assert isinstance(chr, (float, np.ndarray)), "chr must be a float or numpy array" | ||||||||
| from dpnegf.utils.constants import elementary_charge as e | ||||||||
| return chr * e | ||||||||
|
|
||||||||
| def _bflux_impl(jflux, i, nx, ny, nz, phi, full_size=True) -> np.ndarray: | ||||||||
| '''calculate the b vector from the jflux and phi''' | ||||||||
| disp_ = [-1, +1, -nx, +nx, -nx*ny, +nx*ny] | ||||||||
| if full_size: | ||||||||
| assert jflux.shape == (6, len(phi)) | ||||||||
| bflux = np.zeros((6, len(phi)), dtype=float) | ||||||||
| for j, d in enumerate(disp_): | ||||||||
| bflux[j, i] = jflux[j, i] * (phi[i + d] - phi[i]) | ||||||||
| else: | ||||||||
| assert jflux.shape == (6, len(i)) | ||||||||
| bflux = np.array([jf * (phi[i + d] - phi[i]) for jf, d in zip(jflux, disp_)]) | ||||||||
| assert bflux.shape == (6, len(i)) | ||||||||
| return bflux | ||||||||
|
|
||||||||
| def _jflux_impl(nx, ny, nz, r, typ, sigma, eps, eps0, avgeps, with_index=False, | ||||||||
| full_size=True) -> Tuple[np.ndarray, Optional[np.ndarray]]: | ||||||||
| '''kernal implementation of the Jacobian calculation.''' | ||||||||
|
||||||||
| '''kernal implementation of the Jacobian calculation.''' | |
| '''kernel implementation of the Jacobian calculation.''' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor style: avoid semicolon for multiple statements.
Line 180 uses a semicolon to place multiple statements on one line, which violates PEP 8 style guidelines.
🔎 Proposed fix
- nx, ny, nz = grid_dim; nr = nx * ny * nz
+ nx, ny, nz = grid_dim
+ nr = nx * ny * nz📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| nx, ny, nz = grid_dim; nr = nx * ny * nz | |
| nx, ny, nz = grid_dim | |
| nr = nx * ny * nz |
🧰 Tools
🪛 Ruff (0.14.8)
180-180: Multiple statements on one line (semicolon)
(E702)
🤖 Prompt for AI Agents
In dpnegf/negf/newton_raphson_speed_up.py around line 180, the statement uses a
semicolon to combine two assignments; split it into two separate lines to comply
with PEP8: first unpack grid_dim into nx, ny, nz on one line, then compute nr =
nx * ny * nz on the following line (no semicolon).
Uh oh!
There was an error while loading. Please reload this page.