-
Notifications
You must be signed in to change notification settings - Fork 198
autoregressive covariance operator: fix scaling for dim>1 #5358
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
Open
JHopeCollins
wants to merge
4
commits into
main
Choose a base branch
from
JHopeCollins/covariance-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−13
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e2b8e0a
autoregressive covariance operator: fix scaling for dim>1
JHopeCollins 018c686
Merge branch 'main' into JHopeCollins/covariance-fixes
JHopeCollins 8a721e0
IPDG coefficient for AR covariance
JHopeCollins 3bc7db8
AR covariance needs m>2 in 2D and 3D
JHopeCollins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| from functools import cached_property | ||
| from typing import Iterable | ||
| from textwrap import dedent | ||
| import math | ||
| from scipy.special import factorial | ||
| import petsctools | ||
| from loopy import generate_code_v2 | ||
|
|
@@ -420,7 +421,7 @@ def sample(self, *, rng=None, | |
|
|
||
| # Auto-regressive function parameters | ||
|
|
||
| def lengthscale_m(Lar: float, m: int): | ||
| def lengthscale_m(Lar: float, m: int, dim: int): | ||
| """Daley-equivalent lengthscale of m-th order autoregressive function. | ||
|
|
||
| Parameters | ||
|
|
@@ -429,16 +430,18 @@ def lengthscale_m(Lar: float, m: int): | |
| Target Daley correlation lengthscale. | ||
| m : | ||
| Order of autoregressive function. | ||
| dim : | ||
| Topological dimension of the mesh. | ||
|
|
||
| Returns | ||
| ------- | ||
| L : float | ||
| Lengthscale parameter for autoregressive function. | ||
| """ | ||
| return Lar/sqrt(2*m - 3) | ||
| return Lar/sqrt(2*m - dim - 2) | ||
|
|
||
|
|
||
| def lambda_m(Lar: float, m: int): | ||
| def lambda_m(Lar: float, m: int, dim: int): | ||
| """Normalisation factor for autoregressive function. | ||
|
|
||
| Parameters | ||
|
|
@@ -447,19 +450,30 @@ def lambda_m(Lar: float, m: int): | |
| Target Daley correlation lengthscale. | ||
| m : | ||
| Order of autoregressive function. | ||
| dim : | ||
| Topological dimension of the mesh. | ||
|
|
||
| Returns | ||
| ------- | ||
| lambda : float | ||
| Normalisation coefficient for autoregressive correlation operator. | ||
| """ | ||
| L = lengthscale_m(Lar, m) | ||
| num = (2**(2*m - 1))*factorial(m - 1)**2 | ||
| den = factorial(2*m - 2) | ||
| return L*num/den | ||
| L = lengthscale_m(Lar, m, dim) | ||
| if dim == 1: | ||
| num = (2**(2*m - 1))*factorial(m - 1)**2 | ||
| den = factorial(2*m - 2) | ||
| return (num/den)*L | ||
| elif dim == 2: | ||
| return 4*math.pi*(m - 1)*(L**2) | ||
| elif dim == 3: | ||
| num = (2**(2*m - 1))*math.pi*(factorial(m - 2)**2)*(m - 1) | ||
| den = factorial(2*m - 4) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here. |
||
| return (num/den)*(L**3) | ||
| else: | ||
| raise NotImplementedError(f"Not implemented for {dim=} yet") | ||
|
|
||
|
|
||
| def kappa_m(Lar: float, m: int): | ||
| def kappa_m(Lar: float, m: int, dim: int): | ||
| """Diffusion coefficient for autoregressive function. | ||
|
|
||
| Parameters | ||
|
|
@@ -468,13 +482,15 @@ def kappa_m(Lar: float, m: int): | |
| Target Daley correlation lengthscale. | ||
| m : | ||
| Order of autoregressive function. | ||
| dim : | ||
| Topological dimension of the mesh. | ||
|
|
||
| Returns | ||
| ------- | ||
| kappa : float | ||
| Diffusion coefficient for autoregressive covariance operator. | ||
| """ | ||
| return lengthscale_m(Lar, m)**2 | ||
| return lengthscale_m(Lar, m, dim)**2 | ||
|
|
||
|
|
||
| class CovarianceOperatorBase: | ||
|
|
@@ -827,8 +843,9 @@ def __init__(self, V: WithGeometry, L: float | Constant, | |
| # setup diffusion solver | ||
| u, v = TrialFunction(V), TestFunction(V) | ||
| if isinstance(form, self.DiffusionForm): | ||
| self.kappa = Constant(kappa_m(L, m)) | ||
| self.lambda_m = Constant(lambda_m(L, m)) | ||
| dim = V.mesh().ufl_cell().topological_dimension | ||
| self.kappa = Constant(kappa_m(L, m, dim)) | ||
| self.lambda_m = Constant(lambda_m(L, m, dim)) | ||
| self._weight = Constant(sigma*sqrt(self.lambda_m)) | ||
| K = diffusion_form(u, v, self.kappa, formulation=form) | ||
| else: | ||
|
|
@@ -984,7 +1001,8 @@ def diffusion_form(u, v, kappa: Constant | Function, | |
| n = FacetNormal(mesh) | ||
| h = cell_size or CellSize(mesh) | ||
| h_avg = 0.5*(h('+') + h('-')) | ||
| alpha_h = Constant(4.0)/h_avg | ||
| k = v.function_space().ufl_element().degree() | ||
| alpha_h = Constant(10 * (k+1)**2)/h_avg | ||
| return ( | ||
| inner(u, v)*dx + kappa*( | ||
| inner(grad(u), grad(v))*dx | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
should warn if den is zero.