7676#include < dune/fem/space/common/restrictprolongtuple.hh>
7777#include < dune/fem/function/blockvectorfunction.hh>
7878#include < dune/fem/misc/capabilities.hh>
79+
80+ #if HAVE_PETSC
81+ #include < dune/fem/operator/linear/petscoperator.hh>
7982#endif
83+ #include < dune/fem/operator/linear/istloperator.hh>
84+ #include < dune/fem/operator/linear/spoperator.hh>
85+ #endif // endif HAVE_DUNE_FEM
8086
8187#include < limits>
8288#include < list>
@@ -130,7 +136,66 @@ SET_TYPE_PROP(FvBaseDiscretization, DiscExtensiveQuantities, Ewoms::FvBaseExtens
130136// ! Calculates the gradient of any quantity given the index of a flux approximation point
131137SET_TYPE_PROP (FvBaseDiscretization, GradientCalculator, Ewoms::FvBaseGradientCalculator<TypeTag>);
132138
133- // ! Set the type of a global jacobian matrix from the solution types
139+ // ! Set the type of a global Jacobian matrix from the solution types
140+ #if HAVE_DUNE_FEM
141+ SET_PROP (FvBaseDiscretization, DiscreteFunction)
142+ {
143+ private:
144+ typedef typename GET_PROP_TYPE (TypeTag, DiscreteFunctionSpace) DiscreteFunctionSpace;
145+ typedef typename GET_PROP_TYPE (TypeTag, PrimaryVariables) PrimaryVariables;
146+ public:
147+ // discrete function storing solution data
148+ typedef Dune::Fem::ISTLBlockVectorDiscreteFunction<DiscreteFunctionSpace, PrimaryVariables> type;
149+ };
150+ #endif
151+
152+ #if USE_DUNE_FEM_SOLVERS
153+ SET_PROP (FvBaseDiscretization, SparseMatrixAdapter)
154+ {
155+ private:
156+ typedef typename GET_PROP_TYPE (TypeTag, DiscreteFunctionSpace) DiscreteFunctionSpace;
157+ typedef typename GET_PROP_TYPE (TypeTag, Scalar) Scalar;
158+ // discrete function storing solution data
159+ typedef Dune::Fem::ISTLBlockVectorDiscreteFunction<DiscreteFunctionSpace> DiscreteFunction;
160+
161+ #if USE_DUNE_FEM_PETSC_SOLVERS
162+ #warning "Using Dune-Fem PETSc solvers"
163+ typedef Dune::Fem::PetscLinearOperator< DiscreteFunction, DiscreteFunction > LinearOperator;
164+ #elif USE_DUNE_FEM_VIENNACL_SOLVERS
165+ #warning "Using Dune-Fem ViennaCL solvers"
166+ typedef Dune::Fem::SparseRowLinearOperator < DiscreteFunction, DiscreteFunction > LinearOperator;
167+ #else
168+ #warning "Using Dune-Fem ISTL solvers"
169+ typedef Dune::Fem::ISTLLinearOperator < DiscreteFunction, DiscreteFunction > LinearOperator;
170+ #endif
171+
172+ struct FemMatrixBackend : public LinearOperator
173+ {
174+ typedef LinearOperator ParentType;
175+ typedef typename LinearOperator :: MatrixType Matrix;
176+ typedef typename ParentType :: MatrixBlockType MatrixBlock;
177+ template <class Simulator >
178+ FemMatrixBackend ( const Simulator& simulator )
179+ : LinearOperator(" eWoms::Jacobian" , simulator.model().space(), simulator.model().space() )
180+ {}
181+
182+ void commit ()
183+ {
184+ this ->flushAssembly ();
185+ }
186+
187+ template < class LocalBlock >
188+ void addToBlock ( const size_t row, const size_t col, const LocalBlock& block )
189+ {
190+ this ->addBlock ( row, col, block );
191+ }
192+
193+ void clearRow ( const size_t row, const Scalar diag = 1.0 ) { this ->unitRow ( row ); }
194+ };
195+ public:
196+ typedef FemMatrixBackend type;
197+ };
198+ #else
134199SET_PROP (FvBaseDiscretization, SparseMatrixAdapter)
135200{
136201private:
@@ -141,6 +206,7 @@ private:
141206public:
142207 typedef typename Ewoms::Linear::IstlSparseMatrixAdapter<Block> type;
143208};
209+ #endif
144210
145211// ! The maximum allowed number of timestep divisions for the
146212// ! Newton solver
@@ -342,13 +408,15 @@ class FvBaseDiscretization
342408
343409 typedef typename LocalResidual::LocalEvalBlockVector LocalEvalBlockVector;
344410
411+ typedef typename GET_PROP_TYPE (TypeTag, DiscreteFunctionSpace) DiscreteFunctionSpace;
412+
345413 class BlockVectorWrapper
346414 {
347415 protected:
348416 SolutionVector blockVector_;
349417 public:
350- BlockVectorWrapper (const std::string& name OPM_UNUSED, const size_t size )
351- : blockVector_(size)
418+ BlockVectorWrapper (const std::string& name OPM_UNUSED, const DiscreteFunctionSpace& space )
419+ : blockVector_(space. size() )
352420 {}
353421
354422 SolutionVector& blockVector ()
@@ -358,14 +426,12 @@ class FvBaseDiscretization
358426 };
359427
360428#if HAVE_DUNE_FEM
361- typedef typename GET_PROP_TYPE (TypeTag, DiscreteFunctionSpace) DiscreteFunctionSpace;
362-
363429 // discrete function storing solution data
364- typedef Dune::Fem::ISTLBlockVectorDiscreteFunction<DiscreteFunctionSpace, PrimaryVariables> DiscreteFunction;
430+ typedef typename GET_PROP_TYPE (TypeTag, DiscreteFunction) DiscreteFunction;
365431
366432 // problem restriction and prolongation operator for adaptation
367- typedef typename GET_PROP_TYPE (TypeTag, Problem) Problem;
368- typedef typename Problem :: RestrictProlongOperator ProblemRestrictProlongOperator;
433+ typedef typename GET_PROP_TYPE (TypeTag, Problem) Problem;
434+ typedef typename Problem :: RestrictProlongOperator ProblemRestrictProlongOperator;
369435
370436 // discrete function restriction and prolongation operator for adaptation
371437 typedef Dune::Fem::RestrictProlongDefault< DiscreteFunction > DiscreteFunctionRestrictProlong;
@@ -374,7 +440,6 @@ class FvBaseDiscretization
374440 typedef Dune::Fem::AdaptationManager<Grid, RestrictProlong > AdaptationManager;
375441#else
376442 typedef BlockVectorWrapper DiscreteFunction;
377- typedef size_t DiscreteFunctionSpace;
378443#endif
379444
380445 // copying a discretization object is not a good idea
@@ -394,14 +459,14 @@ public:
394459 , elementMapper_(gridView_)
395460 , vertexMapper_(gridView_)
396461#endif
397- , newtonMethod_(simulator)
398- , localLinearizer_(ThreadManager::maxThreads())
399- , linearizer_(new Linearizer())
400462#if HAVE_DUNE_FEM
401- , space_ ( simulator.vanguard().gridPart() )
463+ , discreteFunctionSpace_ ( simulator.vanguard().gridPart() )
402464#else
403- , space_ ( asImp_().numGridDof() )
465+ , discreteFunctionSpace_ ( asImp_().numGridDof() )
404466#endif
467+ , newtonMethod_(simulator)
468+ , localLinearizer_(ThreadManager::maxThreads())
469+ , linearizer_(new Linearizer( ))
405470 , enableGridAdaptation_( EWOMS_GET_PARAM(TypeTag, bool , EnableGridAdaptation) )
406471 , enableIntensiveQuantityCache_(EWOMS_GET_PARAM(TypeTag, bool , EnableIntensiveQuantityCache))
407472 , enableStorageCache_(EWOMS_GET_PARAM(TypeTag, bool , EnableStorageCache))
@@ -426,7 +491,7 @@ public:
426491
427492 size_t numDof = asImp_ ().numGridDof ();
428493 for (unsigned timeIdx = 0 ; timeIdx < historySize; ++timeIdx) {
429- solution_[timeIdx].reset (new DiscreteFunction (" solution" , space_ ));
494+ solution_[timeIdx].reset (new DiscreteFunction (" solution" , discreteFunctionSpace_ ));
430495
431496 if (storeIntensiveQuantities ()) {
432497 intensiveQuantityCache_[timeIdx].resize (numDof);
@@ -1091,6 +1156,16 @@ public:
10911156 SolutionVector& solution (unsigned timeIdx)
10921157 { return solution_[timeIdx]->blockVector (); }
10931158
1159+ template <class BVector >
1160+ void communicate ( BVector& x ) const
1161+ {
1162+ #if HAVE_DUNE_FEM
1163+ typedef Dune::Fem::ISTLBlockVectorDiscreteFunction<DiscreteFunctionSpace, typename BVector::block_type> DF;
1164+ DF tmpX (" temp-x" , discreteFunctionSpace_, x);
1165+ tmpX.communicate ();
1166+ #endif
1167+ }
1168+
10941169 protected:
10951170 /* !
10961171 * \copydoc solution(int) const
@@ -1508,7 +1583,7 @@ public:
15081583 void resetLinearizer ()
15091584 {
15101585 delete linearizer_;
1511- linearizer_ = new Linearizer;
1586+ linearizer_ = new Linearizer () ;
15121587 linearizer_->init (simulator_);
15131588 }
15141589
@@ -1742,6 +1817,11 @@ public:
17421817 solution (timeIdx).resize (numDof);
17431818
17441819 auxMod->applyInitial ();
1820+
1821+ #if DUNE_VERSION_NEWER( DUNE_FEM, 2, 7 )
1822+ discreteFunctionSpace_.extendSize ( asImp_ ().numAuxiliaryDof () );
1823+ #endif
1824+
17451825 }
17461826
17471827 /* !
@@ -1808,6 +1888,11 @@ public:
18081888 const Ewoms::Timer& updateTimer () const
18091889 { return updateTimer_; }
18101890
1891+ #if HAVE_DUNE_FEM
1892+ const DiscreteFunctionSpace& space () const { return discreteFunctionSpace_; }
1893+ #endif
1894+
1895+
18111896protected:
18121897 void resizeAndResetIntensiveQuantitiesCache_ ()
18131898 {
@@ -1878,9 +1963,18 @@ protected:
18781963 ElementMapper elementMapper_;
18791964 VertexMapper vertexMapper_;
18801965
1966+ DiscreteFunctionSpace discreteFunctionSpace_;
1967+
18811968 // a vector with all auxiliary equations to be considered
18821969 std::vector<BaseAuxiliaryModule<TypeTag>*> auxEqModules_;
18831970
1971+ mutable std::array< std::unique_ptr< DiscreteFunction >, historySize > solution_;
1972+
1973+ #if HAVE_DUNE_FEM
1974+ std::unique_ptr< RestrictProlong > restrictProlong_;
1975+ std::unique_ptr< AdaptationManager> adaptationManager_;
1976+ #endif
1977+
18841978 NewtonMethod newtonMethod_;
18851979
18861980 Ewoms::Timer prePostProcessTimer_;
@@ -1899,15 +1993,6 @@ protected:
18991993 mutable IntensiveQuantitiesVector intensiveQuantityCache_[historySize];
19001994 mutable std::vector<bool > intensiveQuantityCacheUpToDate_[historySize];
19011995
1902- DiscreteFunctionSpace space_;
1903- mutable std::array< std::unique_ptr< DiscreteFunction >, historySize > solution_;
1904-
1905- #if HAVE_DUNE_FEM
1906- std::unique_ptr<RestrictProlong> restrictProlong_;
1907- std::unique_ptr<AdaptationManager> adaptationManager_;
1908- #endif
1909-
1910-
19111996 std::list<BaseOutputModule<TypeTag>*> outputModules_;
19121997
19131998 Scalar gridTotalVolume_;
0 commit comments