|
| 1 | +from dask import array, compute, delayed |
| 2 | +import numpy as np |
| 3 | +from ....potential_fields.magnetics import Simulation3DDifferential as Sim |
| 4 | +from ....utils import sdiag, mkvc |
| 5 | + |
| 6 | + |
| 7 | +def distance_weights(locations, cell_centers, cell_volumes, exponent=3, threshold=1e-2): |
| 8 | + weights = np.zeros(len(cell_centers)) |
| 9 | + for loc in locations: |
| 10 | + distance = np.linalg.norm(cell_centers - loc, axis=1) |
| 11 | + weights += cell_volumes**2.0 * (distance + threshold) ** ( |
| 12 | + -2 * exponent |
| 13 | + ) |
| 14 | + |
| 15 | + return weights |
| 16 | + |
| 17 | + |
| 18 | +def dask_getJtJdiag(self, m, W=None, f=None): |
| 19 | + """ |
| 20 | + Return the diagonal of JtJ |
| 21 | + """ |
| 22 | + |
| 23 | + self.model = m |
| 24 | + |
| 25 | + self.model = m |
| 26 | + if W is None: |
| 27 | + W = np.ones(self.Jmatrix.shape[0]) |
| 28 | + else: |
| 29 | + W = W.diagonal() |
| 30 | + |
| 31 | + client, worker = self._get_client_worker() |
| 32 | + |
| 33 | + n_threads = self.n_threads(client=client, worker=worker) |
| 34 | + |
| 35 | + chunks = np.array_split(self.survey.receiver_locations, n_threads) |
| 36 | + cell_centers = self.mesh.cell_centers.copy() |
| 37 | + cell_volumes = self.mesh.cell_volumes.copy() |
| 38 | + |
| 39 | + if client: |
| 40 | + cell_centers = client.scatter(cell_centers, workers=worker) |
| 41 | + cell_volumes = client.scatter(cell_volumes, workers=worker) |
| 42 | + else: |
| 43 | + delayed_distance_weights = delayed(distance_weights) |
| 44 | + |
| 45 | + futures = [] |
| 46 | + for block in chunks: |
| 47 | + if client: |
| 48 | + futures.append( |
| 49 | + client.submit( |
| 50 | + distance_weights, |
| 51 | + block, |
| 52 | + cell_centers, |
| 53 | + cell_volumes, |
| 54 | + workers=worker, |
| 55 | + ) |
| 56 | + ) |
| 57 | + else: |
| 58 | + futures.append( |
| 59 | + array.from_delayed( |
| 60 | + delayed_distance_weights( |
| 61 | + block, |
| 62 | + cell_centers, |
| 63 | + cell_volumes, |
| 64 | + ), |
| 65 | + dtype=np.float32, |
| 66 | + shape=( |
| 67 | + len(block), |
| 68 | + len(cell_centers), |
| 69 | + ), |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + if client: |
| 74 | + diag = client.gather(futures) |
| 75 | + else: |
| 76 | + diag = compute(futures)[0] |
| 77 | + |
| 78 | + diag = np.tile(np.vstack(diag).sum(axis=0), 3) |
| 79 | + return mkvc((sdiag(np.sqrt(diag)) @ self.remDeriv).power(2).sum(axis=0)) |
| 80 | + |
| 81 | + |
| 82 | +Sim.getJtJdiag = dask_getJtJdiag |
0 commit comments