I have copied some code below.
I was hoping to take the outer product which is formed in a FullMatrix,
called Kronecker_matrix below, multiply it by a coefficient, and add a
matrix to it called nonlinear_part_full. This entire operation I have tried
to put into a Sparse matrix, called Nonlinear_part, because I need to add
this term to linear_part which is Sparse. I have done this simply because
it was the only way I could make the addition between linear_part (Sparse)
and nonlinear_part_full (FullMatrix) work all while trying to make the code
as efficient as possible.
template <int dim>
void TimeStep<dim>::prepare()
{
std::cout << "TimeStep<dim>::prepare()" << std::endl;
const auto &offline_data = *p_offline_data;
linear_part.reinit(offline_data.sparsity_pattern);
auto &M_c = offline_data.mass_matrix;
auto &S_c = offline_data.stiffness_matrix;
linear_part.copy_from(M_c);
linear_part.add((1. - theta) * kappa, S_c);
linear_part_inverse.initialize(linear_part);
nonlinear_part.reinit(offline_data.sparsity_pattern);
auto &X = offline_data.inner_product_matrix;
nonlinear_part.copy_from(X);
}
template <int dim>
void TimeStep<dim>::step(Vector<double> &old_solution,double new_t)
{
const auto &offline_data = *p_offline_data;
const auto &coefficients = *offline_data.p_coefficients;
const auto &affine_constraints = offline_data.affine_constraints;
auto &manufactured = *p_manufactured;
const auto lam = coefficients.lam;
std::cout<<"lambda in Time step = "<<lam<<std::endl;
const auto M_c = linear_operator(offline_data.mass_matrix);
const auto S_c = linear_operator(offline_data.stiffness_matrix);
const auto M_u = linear_operator(offline_data.mass_matrix_unconstrained);
const auto S_u =
linear_operator(offline_data.stiffness_matrix_unconstrained);
const auto X = linear_operator(offline_data.inner_product_matrix);
GrowingVectorMemory<Vector<double>> vector_memory;
typename VectorMemory<Vector<double>>::Pointer
p_new_solution(vector_memory);
auto &new_solution = *p_new_solution;
new_solution = old_solution;
apply_boundary_values(offline_data, coefficients, new_t, new_solution,
old_solution);
for (unsigned int m = 0; m < nonlinear_solver_limit; ++m)
{ Vector<double> residual = M_u * (new_solution - old_solution)
+ kappa * (1. - theta) * S_u * new_solution + theta * kappa *
S_u * old_solution - kappa * (1.- theta) * lam * new_solution.norm_sqr()
* new_solution - kappa * theta * lam *
old_solution.norm_sqr() * old_solution;
affine_constraints.set_zero(residual);
if (residual.linfty_norm() < nonlinear_solver_tol)
break;
double old_solution_normsq = -old_solution.norm_sqr();
const unsigned int k = old_solution.size();
FullMatrix<double>Kronecker_matrix(k,k);
for(unsigned int i=0; i<k; ++i)
for(unsigned int j=0; j<k; ++j)
Kronecker_matrix(i,j) = old_solution[i]*old_solution[j];
FullMatrix<double>nonlinear_part_full;
nonlinear_part_full.copy_from(nonlinear_part);
double nonlinear_part_coef = - kappa * (1.- theta) * lam *
old_solution_normsq;
double Kronecker_matrix_coef = -2.* lam * kappa * (1- theta);
nonlinear_part_full *= nonlinear_part_coef; // -k(1- theta) lambda
|Psi|^2*X
Kronecker_matrix *= Kronecker_matrix_coef; //(-2.* lam * kappa * (1-
theta)* Kronecker_matrix)
nonlinear_part_full.add(1.,Kronecker_matrix); //-k(1- theta) lambda
|Psi|^2*X - 2k(1-theta)lambda Kronecker_matrix
SparseMatrix<double> Nonlinear_part;
Nonlinear_part.reinit(offline_data.sparsity_pattern);
Nonlinear_part.copy_from(nonlinear_part_full);
linear_part.add(1., Nonlinear_part);
auto system_matrix = linear_operator(linear_part);
SolverControl solver_control(linear_solver_limit, linear_solver_tol);
SolverGMRES<> solver(solver_control);
auto system_matrix_inverse = inverse_operator(system_matrix, solver,
linear_part_inverse);
Vector<double> update = system_matrix_inverse * (-1. * residual);
affine_constraints.set_zero(update);
new_solution += update;
}
On Sunday, October 11, 2020 at 5:38:12 PM UTC-4 Wolfgang Bangerth wrote:
>
> Nikki,
> we can continue to speculate, but your description of what is happening is
> not
> specific enough to really know. You'll have to show us the code you use to
> be
> sure.
>
> I don't understand the connection between the code you show and the outer
> product. The outer product is a dense matrix, but the sparsity pattern you
> create is sparse. Were you hoping to put the outer product of two vectors
> into
> a sparse matrix?
>
> Best
> W.
>
>
> On 10/10/20 3:27 PM, Nikki Holtzer wrote:
> > My sparsity pattern was created in the following way:
> >
> > DynamicSparsityPattern c_sparsity(dof_handler.n_dofs(),
> dof_handler.n_dofs());
> >
> > DoFTools::make_sparsity_pattern(dof_handler, c_sparsity,
> > affine_constraints, true);
> >
> >
> > which I have used throughout my code with no problems.
> >
> >
> > The FullMatrix, Mat1, that I form comes from the outer product of vec1
> and
> > vec2 both of size dof_handler.n_dofs.
> >
> > I create a Sparse Matrix, Mat2 that i initialize with the sparsity
> pattern
> > written above. I am not seeing why Mat1 of size dof_handler.n_dofs X
> > dof_handler.n_dofs does not fit into a sparsity pattern that's defined
> as
> > DynamicSparsityPattern c_sparsity(dof_handler.n_dofs(),
> dof_handler.n_dofs());
> >
> > I am indeed getting an error that there are not enough entries in the
> > SparseMatrix, Mat2.
> >
> > Thank you!
> >
> >
> > On Wednesday, October 7, 2020 at 4:58:45 PM UTC-4 Wolfgang Bangerth
> wrote:
> >
> > On 10/7/20 2:51 PM, Nikki Holtzer wrote:
> > >
> > > When doing so I receive the following error:
> > >
> > >
> > > An error occurred in line <195> of file
> > > <dealii-9.1.1/include/deal.II/lac/sparse_matrix.templates.h> in
> function
> > >
> > > dealii::SparseMatrix<Number>&
> > > dealii::SparseMatrix<number>::operator=(double) [with number = double]
> > >
> > > The violated condition was:
> > >
> > > cols != nullptr
> > >
> > > Additional information:
> > >
> > > (none)
> >
> > You haven't associated a sparsity pattern with the sparse matrix yet.
> You'll
> > have to set that first, and you need to make sure that it has an entry
> for
> > each nonzero in the full matrix you are hoping to copy over!
> >
> > Best
> > W.
> >
> >
> > --
> > ------------------------------------------------------------------------
> > Wolfgang Bangerth email: [email protected]
> > www: http://www.math.colostate.edu/~bangerth/
> >
> > --
> > The deal.II project is located at http://www.dealii.org/
> > <
> https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.dealii.org%2F&data=02%7C01%7CWolfgang.Bangerth%40colostate.edu%7Cb8d612f3bf054629da0108d86d6341e7%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C637379620364329787&sdata=%2FrnFtYKQyv6SAq3VPanNNWuc44wda22mCApTE3eKs38%3D&reserved=0
> >
> > For mailing list/forum options, see
> > https://groups.google.com/d/forum/dealii?hl=en
> > <
> https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fforum%2Fdealii%3Fhl%3Den&data=02%7C01%7CWolfgang.Bangerth%40colostate.edu%7Cb8d612f3bf054629da0108d86d6341e7%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C637379620364334764&sdata=nj5OkstMbznZWPpEdCGgBDRHF1h1tzib22BmHu4Cj7s%3D&reserved=0
> >
> > ---
> > You received this message because you are subscribed to the Google
> Groups
> > "deal.II User Group" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected]
> > <mailto:[email protected]>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/dealii/56cca082-6ae2-4b51-af0d-85ed95661ac4n%40googlegroups.com
>
> > <
> https://nam01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fdealii%2F56cca082-6ae2-4b51-af0d-85ed95661ac4n%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=02%7C01%7CWolfgang.Bangerth%40colostate.edu%7Cb8d612f3bf054629da0108d86d6341e7%7Cafb58802ff7a4bb1ab21367ff2ecfc8b%7C0%7C0%7C637379620364334764&sdata=xb9XCcWPhiW116ZH7j13ZnGpkwxxCDLVDnLI8pxoBzk%3D&reserved=0
> >.
>
>
> --
> ------------------------------------------------------------------------
> Wolfgang Bangerth email: [email protected]
> www: http://www.math.colostate.edu/~bangerth/
>
>
--
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see
https://groups.google.com/d/forum/dealii?hl=en
---
You received this message because you are subscribed to the Google Groups
"deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/dealii/4a8290b6-9b1b-4070-9b2e-7654157e1d10n%40googlegroups.com.