16 #include "projections/ogprojection.h"
18 #include "solver/linear_solver.h"
24 template<
typename Scalar>
29 throw Hermes::Exceptions::NullException(1);
30 if (target_vec ==
nullptr)
31 throw Exceptions::NullException(2);
38 linear_solver.
solve();
40 memcpy(target_vec, linear_solver.
get_sln_vector(), space->get_num_dofs() *
sizeof(Scalar));
43 template<
typename Scalar>
51 proj_wf->add_matrix_form(custom_projection_jacobian);
52 proj_wf->add_vector_form(custom_projection_residual);
55 project_internal(space, proj_wf, target_vec);
58 template<
typename Scalar>
65 Scalar* target_vec = malloc_with_check<Scalar>(space->get_num_dofs());
67 project_global(space, custom_projection_jacobian, custom_projection_residual, target_vec);
73 free_with_check(target_vec);
76 template<
typename Scalar>
83 if (target_vec ==
nullptr)
throw Exceptions::NullException(3);
85 Helpers::check_length(custom_projection_jacobians, spaces);
86 Helpers::check_length(custom_projection_residuals, spaces);
88 int start_index = 0, spaces_size = spaces.size();
89 for (
int i = 0; i < spaces_size; i++)
91 project_global(spaces[i], custom_projection_jacobians[i], custom_projection_residuals[i], target_vec + start_index);
92 start_index += spaces[i]->get_num_dofs();
96 template<
typename Scalar>
103 Helpers::check_length(target_slns, spaces);
104 Helpers::check_length(custom_projection_jacobians, spaces);
105 Helpers::check_length(custom_projection_residuals, spaces);
107 int spaces_size = spaces.size();
108 for (
int i = 0; i < spaces_size; i++)
109 project_global(spaces[i], custom_projection_jacobians[i], custom_projection_residuals[i], target_slns[i]);
112 template<
typename Scalar>
117 if (target_vec->get_size() != space->get_num_dofs())
118 throw Exceptions::ValueException(
"target_vec->size", target_vec->get_size(), space->get_num_dofs());
120 Scalar* vec = malloc_with_check<Scalar>(target_vec->get_size());
121 project_global(space, source_meshfn, vec, proj_norm);
122 target_vec->set_vector(vec);
123 free_with_check(vec);
126 template<
typename Scalar>
128 MeshFunctionSharedPtr<Scalar> source_meshfn, Scalar* target_vec,
132 if (target_vec ==
nullptr)
133 throw Exceptions::NullException(3);
138 if (proj_norm == HERMES_UNSET_NORM)
140 SpaceType space_type = space->get_type();
143 case HERMES_H1_SPACE: norm = HERMES_H1_NORM;
break;
144 case HERMES_HCURL_SPACE: norm = HERMES_HCURL_NORM;
break;
145 case HERMES_HDIV_SPACE: norm = HERMES_HDIV_NORM;
break;
146 case HERMES_L2_SPACE: norm = HERMES_L2_NORM;
break;
147 case HERMES_L2_MARKERWISE_CONST_SPACE: norm = HERMES_L2_NORM;
break;
148 default:
throw Hermes::Exceptions::Exception(
"Unknown space type in OGProjection<Scalar>::project_global().");
151 else norm = proj_norm;
154 WeakFormSharedPtr<Scalar> proj_wf(
new WeakForm<Scalar>(1));
155 proj_wf->set_verbose_output(
false);
156 proj_wf->set_ext(source_meshfn);
158 proj_wf->add_matrix_form(
new MatrixDefaultNormFormVol<Scalar>(0, 0, norm));
160 proj_wf->add_vector_form(
new VectorDefaultNormFormVol<Scalar>(0, norm));
163 project_internal(space, proj_wf, target_vec);
166 template<
typename Scalar>
171 if (proj_norm == HERMES_UNSET_NORM)
173 SpaceType space_type = space->get_type();
176 case HERMES_H1_SPACE: proj_norm = HERMES_H1_NORM;
break;
177 case HERMES_HCURL_SPACE: proj_norm = HERMES_HCURL_NORM;
break;
178 case HERMES_HDIV_SPACE: proj_norm = HERMES_HDIV_NORM;
break;
179 case HERMES_L2_SPACE: proj_norm = HERMES_L2_NORM;
break;
180 case HERMES_L2_MARKERWISE_CONST_SPACE: proj_norm = HERMES_L2_NORM;
break;
181 default:
throw Hermes::Exceptions::Exception(
"Unknown space type in OGProjection<Scalar>::project_global().");
186 Scalar* target_vec = malloc_with_check<Scalar>(space->get_num_dofs());
187 project_global(space, source_sln, target_vec, proj_norm);
193 free_with_check(target_vec);
196 template<
typename Scalar>
198 Scalar* target_vec, std::vector<NormType> proj_norms)
201 Helpers::check_length(source_slns, spaces);
202 if (target_vec ==
nullptr)
203 throw Exceptions::NullException(3);
204 if (!proj_norms.empty())
205 Helpers::check_length(proj_norms, spaces);
207 int start_index = 0, spaces_size = spaces.size();
208 for (
int i = 0; i < spaces_size; i++)
210 if (proj_norms.empty())
211 project_global(spaces[i], source_slns[i], target_vec + start_index, HERMES_UNSET_NORM);
213 project_global(spaces[i], source_slns[i], target_vec + start_index, proj_norms[i]);
214 start_index += spaces[i]->get_num_dofs();
218 template<
typename Scalar>
220 Hermes::Algebra::Vector<Scalar>* target_vec, std::vector<NormType> proj_norms)
225 Scalar* vec = malloc_with_check<Scalar>(target_vec->get_size());
226 project_global(spaces, source_slns, vec, proj_norms);
227 target_vec->set_vector(vec);
228 free_with_check(vec);
231 template<
typename Scalar>
233 std::vector<MeshFunctionSharedPtr<Scalar> > target_slns, std::vector<NormType> proj_norms,
bool delete_old_meshes)
235 int n = spaces.size();
238 if (n != source_slns.size())
239 throw Exceptions::LengthException(1, 2, n, source_slns.size());
240 if (n != target_slns.size())
241 throw Exceptions::LengthException(1, 2, n, target_slns.size());
242 if (!proj_norms.empty() && n != proj_norms.size())
243 throw Exceptions::LengthException(1, 5, n, proj_norms.size());
245 for (
int i = 0; i < n; i++)
247 if (proj_norms.empty())
248 project_global(spaces[i], source_slns[i], target_slns[i], HERMES_UNSET_NORM);
250 project_global(spaces[i], source_slns[i], target_slns[i], proj_norms[i]);
254 template class HERMES_API OGProjection < double > ;
255 template class HERMES_API OGProjection < std::complex<double> > ;
Scalar * get_sln_vector()
Get sln vector.
Used to pass the instances of Space around.
virtual void set_verbose_output(bool to_set)
See Hermes::Mixins::Loggable.
int get_num_dofs() const
Returns the number of basis functions contained in the space.
static void project_internal(SpaceSharedPtr< Scalar > space, WeakFormSharedPtr< Scalar > proj_wf, Scalar *target_vec)
virtual void solve(Scalar *coeff_vec)
Represents the solution of a PDE.
static void project_global(SpaceSharedPtr< Scalar > space, MatrixFormVol< Scalar > *custom_projection_jacobian, VectorFormVol< Scalar > *custom_projection_residual, Scalar *target_vec)
This method allows to specify your own OG-projection form.