Skip to content

Commit

Permalink
Update mathieu evp to use rebuilding matrices, fix cartesian ncc corn…
Browse files Browse the repository at this point in the history
…er case where ncc is zero
  • Loading branch information
kburns committed Aug 17, 2022
1 parent 27f3b46 commit 7a114a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
30 changes: 17 additions & 13 deletions dedalus/core/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,23 @@ def build_cartesian_ncc_matrix(self, subproblem, ncc_cutoff, max_ncc_terms):
subproblem_shape = subproblem.coeff_shape(out.domain)
ncc_rank = len(ncc.tensorsig)
select_all_comps = tuple(slice(None) for i in range(ncc_rank))
for ncc_mode in np.ndindex(self._ncc_data.shape[ncc_rank:]):
ncc_coeffs = self._ncc_data[select_all_comps + ncc_mode]
if np.max(np.abs(ncc_coeffs)) > ncc_cutoff:
mode_matrix = self.cartesian_mode_matrix(subproblem_shape, ncc.domain, arg.domain, out.domain, ncc_mode)
matrix = sparse.kron(np.dot(Gamma, ncc_coeffs.ravel()), mode_matrix, format='coo')
shape = matrix.shape
data.append(matrix.data)
rows.append(matrix.row)
cols.append(matrix.col)
data = np.concatenate(data)
rows = np.concatenate(rows)
cols = np.concatenate(cols)
matrix = sparse.coo_matrix((data, (rows, cols)), shape=shape).tocsr()
if np.any(self._ncc_data):
for ncc_mode in np.ndindex(self._ncc_data.shape[ncc_rank:]):
ncc_coeffs = self._ncc_data[select_all_comps + ncc_mode]
if np.max(np.abs(ncc_coeffs)) > ncc_cutoff:
mode_matrix = self.cartesian_mode_matrix(subproblem_shape, ncc.domain, arg.domain, out.domain, ncc_mode)
matrix = sparse.kron(np.dot(Gamma, ncc_coeffs.ravel()), mode_matrix, format='coo')
shape = matrix.shape
data.append(matrix.data)
rows.append(matrix.row)
cols.append(matrix.col)
data = np.concatenate(data)
rows = np.concatenate(rows)
cols = np.concatenate(cols)
matrix = sparse.coo_matrix((data, (rows, cols)), shape=shape).tocsr()
else:
shape = (subproblem.field_size(out), subproblem.field_size(arg))
matrix = sparse.csr_matrix(shape, dtype=self.dtype)
return matrix

@classmethod
Expand Down
26 changes: 14 additions & 12 deletions examples/evp_1d_mathieu/mathieu_evp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@

# Substitutions
x = dist.local_grid(basis)
q = dist.Field()
cos_2x = dist.Field(bases=basis)
cos_2x['g'] = np.cos(2 * x)
dx = lambda A: d3.Differentiate(A, coord)
namespace = locals()

# Eigenvalue solver
def eigenvalues(q):
# Problem
problem = d3.EVP([y], eigenvalue=a, namespace=namespace)
problem.namespace['q'] = q
problem.add_equation("dx(dx(y)) + (a - 2*q*cos_2x)*y = 0")
# Solver
solver = problem.build_solver()
solver.solve_dense(solver.subproblems[0])
return np.sort(solver.eigenvalues.real)
evals = np.array([eigenvalues(q)[:10] for q in q_list])
# Problem
problem = d3.EVP([y], eigenvalue=a, namespace=locals())
problem.add_equation("dx(dx(y)) + (a - 2*q*cos_2x)*y = 0")

# Solver
solver = problem.build_solver()
evals = []
for qi in q_list:
q['g'] = qi
solver.solve_dense(solver.subproblems[0], rebuild_matrices=True)
sorted_evals = np.sort(solver.eigenvalues.real)
evals.append(sorted_evals[:10])
evals = np.array(evals)

# Plot
fig = plt.figure(figsize=(6, 4))
Expand Down

0 comments on commit 7a114a5

Please sign in to comment.