Skip to content
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

Should be possible to couple scalar and vector equations #920

Open
guyer opened this issue Apr 26, 2023 · 0 comments
Open

Should be possible to couple scalar and vector equations #920

guyer opened this issue Apr 26, 2023 · 0 comments
Labels

Comments

@guyer
Copy link
Member

guyer commented Apr 26, 2023

As illustrated

If we attempt to represent

$$ \begin{aligned} \frac{\partial C}{\partial t} &= \nabla\cdot(D_C \nabla C) + \nabla\cdot(C \vec{v}) \\ \frac{\partial \vec{v}}{\partial t} &= \nabla\cdot(D_\vec{v} \nabla \vec{v}) \end{aligned} $$

as

import fipy as fp

mesh = fp.Grid2D(nx=10, ny=10)
sca = fp.CellVariable(mesh=mesh, name="sca", hasOld=True)
vec = fp.CellVariable(mesh=mesh, name="vec", rank=1, hasOld=True)

Dsca = 1
Dvec = 2

eq_sca = fp.TransientTerm(var=sca) == fp.DiffusionTerm(coeff=Dsca, var=sca) + fp.ConvectionTerm(coeff=vec, var=sca)
eq_vec = fp.TransientTerm(var=vec) == fp.DiffusionTerm(coeff=[[[Dvec, 0],
                                                               [0, Dvec]]], var=vec)

eq = eq_sca & eq_vec

eq.solve(dt=1)

FiPy raises

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

Solving sequentially is fine:

eq_sca.solve(dt=1)
eq_vec.solve(dt=1)

For stability reasons, it may be necessary to use a sequential approach, anyway (e.g. SIMPLE), but FiPy shouldn't prevent trying to solve coupled.

@guyer guyer added the terms label Apr 26, 2023
guyer added a commit that referenced this issue Dec 4, 2023
Fixes #963
(Actually, PETSC 3.19.0 broke the world.)

This PR:
- Assembles before `multTranspose` to prevent newly added exception
- Renames `bandwidth` to `nonZerosPerRow` and removes `sizeHint`

  The two were confusingly redundant:
  - PySparse takes `sizeHint`, the number of non-zeros in the matrix.
  - PyTrilinos takes `NumEntriesPerRow`.
  - petsc4py didn't used to be clear what it took, but is now
    documented as number of non-zeros per row (of the local portion
    of the matrix, but we'll ignore that part).
  - scipy doesn't preallocate.
  - Linear algebra
    [defines bandwidth](https://en.wikipedia.org/wiki/Band_matrix#Bandwidth)
    as "number $k$ such that $a_{i,j}=0$ if $|i-j| > k$", which is
    roughly half the number of non-zeros per row (and only applies
    to a band-diagonal matrix).
    Better to be explicit about what we really mean.

  Now all take same parameter and PySparse adjusts as needed.

  `sizeHint` was introduced in @a15d696 (in 2006!) to
  "allow smaller preallocations", but it was never used that way.
  Now, `nonZerosPerRow` can take an array_like to specify row-by-row
  preallocations, which are directly supported by PyTrilinos and petsc4py,
  and can be simulated for PySparse.

  Added `exactNonZeros`, which may have performance benefits for
  PyTrilinos and petsc4py. Currently unused.
- Uses `Term`'s knowledge of own stencil to preallocate more effectively.
  Still doesn't do a good job with vector equations, but that's a deeper change
  (the resolution of which might help #920).

* Assemble before multTranspose

* Rename `bandwidth` to `nonZerosPerRow` and remove `sizeHint`

The two were confusingly redundant:
- PySparse takes `sizeHint`, the number of non-zeros in the matrix.
- PyTrilinos takes `NumEntriesPerRow`.
- petsc4py didn't used to be clear what it took, but is now
  documented as number of non-zeros per row (of the local portion
  of the matrix, but we'll ignore that part).
- scipy doesn't preallocate.
- Linear algebra
  [defines bandwidth](https://en.wikipedia.org/wiki/Band_matrix#Bandwidth)
  as "number $k$ such that $a_{i,j}=0$ if $|i-j| > k$", which is
  roughly half the number of non-zeros per row (and only applies
  to a band-diagonal matrix).
  Better to be explicit about what we really mean.

Now all take same parameter and PySparse adjusts as needed.

`sizeHint` was introduced in @a15d696 (in 2006!) to
"allow smaller preallocations", but it was never used that way.
Now, `nonZerosPerRow` can take an array_like to specify row-by-row
preallocations, which are directly supported by PyTrilinos and petsc4py,
and can be simulated for PySparse.

Added `exactNonZeros`, which may have performance benefits for
PyTrilinos and petsc4py. Currently unused.

* Fix(?) conda/mamba installs

* Fix(?) race condition
guyer added a commit that referenced this issue Dec 4, 2023
Fixes #963
(Actually, PETSC 3.19.0 broke the world.)

This PR:
- Assembles before `multTranspose` to prevent newly added exception
- Renames `bandwidth` to `nonZerosPerRow` and removes `sizeHint`

  The two were confusingly redundant:
  - PySparse takes `sizeHint`, the number of non-zeros in the matrix.
  - PyTrilinos takes `NumEntriesPerRow`.
  - petsc4py didn't used to be clear what it took, but is now
    documented as number of non-zeros per row (of the local portion
    of the matrix, but we'll ignore that part).
  - scipy doesn't preallocate.
  - Linear algebra
    [defines bandwidth](https://en.wikipedia.org/wiki/Band_matrix#Bandwidth)
    as "number $k$ such that $a_{i,j}=0$ if $|i-j| > k$", which is
    roughly half the number of non-zeros per row (and only applies
    to a band-diagonal matrix).
    Better to be explicit about what we really mean.

  Now all take same parameter and PySparse adjusts as needed.

  `sizeHint` was introduced in @a15d696 (in 2006!) to
  "allow smaller preallocations", but it was never used that way.
  Now, `nonZerosPerRow` can take an array_like to specify row-by-row
  preallocations, which are directly supported by PyTrilinos and petsc4py,
  and can be simulated for PySparse.

  Added `exactNonZeros`, which may have performance benefits for
  PyTrilinos and petsc4py. Currently unused.
- Uses `Term`'s knowledge of own stencil to preallocate more effectively.
  Still doesn't do a good job with vector equations, but that's a deeper change
  (the resolution of which might help #920).
- Fixes(?) conda/mamba installs
- Fixes(?) race condition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant