31 auto pgt_torus = std::dynamic_pointer_cast<const torus_geom_trans>(pgt);
33 orig_pgt = pgt_torus ? pgt_torus->get_original_transformation()
36 dim_type n = cvs->dim();
40 xref[2] = std::min(std::max(xref[2], 0.0), 1.0);
41 scalar_type h = 1.0 - xref[2];
42 for (dim_type k = 0; k < 2; ++k)
43 xref[k] = std::min(std::max(xref[k], -h), h);
47 for (
auto &coord : xref) {
48 if (coord < 0.0) coord = 0.0;
49 if (coord > 1.0) coord = 1.0;
53 scalar_type sum_coordinates = 0.0;
54 for (
const auto &coord : xref) sum_coordinates += coord;
56 if (sum_coordinates > 1.0) gmm::scale(xref, 1.0 / sum_coordinates);
60 scalar_type sum_coordinates = 0.0;
61 for (dim_type k = 0; k < n-1; ++k) sum_coordinates += xref[k];
62 if (sum_coordinates > 1.0)
63 for (dim_type k = 0; k < n-1; ++k) xref[k] /= sum_coordinates;
69 bool project_into_element) {
70 bool converged =
true;
71 return invert(x, xref, converged, IN_EPS, project_into_element);
77 bool project_into_element) {
79 xref.resize(pgt->structure()->dim());
82 return invert_lin(x, xref, IN_EPS);
84 return invert_nonlin(x, xref, IN_EPS, converged,
false,
85 project_into_element);
93 base_node xrel(x); gmm::add(gmm::scaled(mat_col(G, 0), -1.0), xrel);
94 mult(transposed(B), xrel, xref);
96 add(gmm::scaled(xrel, -1.0), y);
98 return (pgt->convex_ref()->is_in(xref) < IN_EPS) &&
99 (gmm::vect_norm2(y) < IN_EPS);
102 bool geotrans_inv_convex::update_B() {
104 pgt->compute_K_matrix(G, pc, K);
105 gmm::mult(gmm::transposed(K), K, CS);
106 double det = bgeot::lu_inverse(&(*(CS.begin())), P,
false);
113 base_matrix KT(K.nrows(), K.ncols());
114 pgt->compute_K_matrix(G, pc, KT);
117 double det = bgeot::lu_inverse(&(*(K.begin())), P,
false);
125 class geotrans_inv_convex_bfgs {
126 geotrans_inv_convex &gic;
129 geotrans_inv_convex_bfgs(geotrans_inv_convex &gic_,
130 const base_node &x_) : gic(gic_), x(x_) {}
131 scalar_type operator()(
const base_node& xref)
const {
132 base_node r = gic.pgt->transform(xref, gic.G) - x;
135 void operator()(
const base_node& xref, base_small_vector& gr)
const {
136 gic.pgt->poly_vector_grad(xref, gic.pc);
138 base_node r = gic.pgt->transform(xref, gic.G) - x;
139 gr.resize(xref.size());
140 gmm::mult(gmm::transposed(gic.K), r, gr);
144 void geotrans_inv_convex::update_linearization() {
146 const convex_ind_ct &dir_pt_ind = pgt->structure()->ind_dir_points();
147 const stored_point_tab &ref_nodes = pgt->geometric_nodes();
149 has_linearized_approx =
true;
151 auto n_points = dir_pt_ind.size();
152 auto N_ref = ref_nodes.begin()->size();
154 std::vector<base_node> dir_pts, dir_pts_ref;
155 for (
auto i : dir_pt_ind) {
156 dir_pts.push_back(base_node(N));
157 gmm::copy(mat_col(G, i), dir_pts.back());
158 dir_pts_ref.push_back(ref_nodes[i]);
161 base_matrix K_lin(N, n_points - 1),
162 B_transp_lin(n_points - 1, N),
163 K_ref_lin(N_ref, n_points - 1);
166 P_ref_lin = dir_pts_ref[0];
168 for (
size_type i = 1; i < n_points; ++i) {
169 add(dir_pts[i], gmm::scaled(P_lin, -1.0), mat_col(K_lin, i - 1));
170 add(dir_pts_ref[i], gmm::scaled(P_ref_lin, -1.0),
171 mat_col(K_ref_lin, i - 1));
174 if (K_lin.nrows() == K_lin.ncols()) {
179 base_matrix temp(n_points - 1, n_points - 1);
180 mult(transposed(K_lin), K_lin, temp);
182 mult(temp, transposed(K_lin), B_transp_lin);
185 K_ref_B_transp_lin.base_resize(N_ref, N);
186 mult(K_ref_lin, B_transp_lin, K_ref_B_transp_lin);
193 bool geotrans_inv_convex::invert_nonlin(
const base_node& x,
198 bool project_into_element) {
200 base_node x0_ref(P), x0rel(N), diff(N), xref_prev(P);
202 base_node xrel(x);
gmm::add(gmm::scaled(mat_col(G, 0), -1.0), xrel);
205 x0_ref = pgt->geometric_nodes()[0];
207 for (
size_type j = 1; j < pgt->nb_points(); ++j) {
211 x0_ref = pgt->geometric_nodes()[j];
214 if (res < maxnormG * IN_EPS/100.) {
216 if (project_into_element) project_into_convex(xref, pgt);
217 x0rel = pgt->transform(xref, Grel);
218 add(x0rel, gmm::scaled(xrel, -1.0), diff);
220 if (res < maxnormG * IN_EPS/100. &&
221 pgt->convex_ref()->is_in(xref) < IN_EPS) {
227 scalar_type res0 = std::numeric_limits<scalar_type>::max();
228 if (has_linearized_approx) {
230 add(x, gmm::scaled(P_lin, -1.0), diff);
231 mult(K_ref_B_transp_lin, diff, xref);
234 if (project_into_element) project_into_convex(xref, pgt);
243 add(pgt->transform(xref, Grel), gmm::scaled(xrel, -1.0), diff);
245 scalar_type res0 = std::numeric_limits<scalar_type>::max();
246 scalar_type factor = 1.0;
248 while (res >= maxnormG * IN_EPS/100.) {
249 if ((gmm::abs(res - res0) < IN_EPS/100.) || (factor < IN_EPS)) {
252 converged = (res < maxnormG * IN_EPS/100.);
255 pgt->poly_vector_grad(xref, pc);
257 mult(transposed(B), diff, x0_ref);
259 if (converged && P != N) {
261 gmm::mult(K, gmm::scaled(x0_ref, -1.0), diff, rn);
266 return converged && (pgt->convex_ref()->is_in(xref) < IN_EPS);
270 x0rel = pgt->transform(xref, Grel);
271 add(x0rel, gmm::scaled(xrel, -1.0), diff);
274 if (factor < 1.0-IN_EPS) factor *= 2.0;
277 pgt->poly_vector_grad(xref, pc);
282 mult(transposed(B), diff, x0_ref);
284 add(gmm::scaled(x0_ref, -factor), xref);
285 if (project_into_element) project_into_convex(xref, pgt);
286 x0rel = pgt->transform(xref, Grel);
287 add(x0rel, gmm::scaled(xrel, -1.0), diff);
291 return (pgt->convex_ref()->is_in(xref) < IN_EPS);
Inversion of geometric transformations.
Mesh structure definition.
bool invert(const base_node &x, base_node &xref, scalar_type IN_EPS=1e-12, bool project_into_element=false)
given the node on the real element, returns the node on the reference element (even if it is outside ...
void copy(const L1 &l1, L2 &l2)
*/
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
number_traits< typename linalg_traits< M >::value_type >::magnitude_type mat_maxnorm(const M &m)
*/
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2_sqr(const V &v)
squared Euclidean norm of a vector.
number_traits< typename linalg_traits< V1 >::value_type >::magnitude_type vect_dist2(const V1 &v1, const V2 &v2)
Euclidean distance between two vectors.
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
void add(const L1 &l1, L2 &l2)
*/
Implements BFGS (Broyden, Fletcher, Goldfarb, Shanno) algorithm.
pconvex_structure pyramid_QK_structure(dim_type k)
Give a pointer on the 3D pyramid structure for a degree k = 1 or 2.
std::shared_ptr< const convex_structure > pconvex_structure
Pointer on a convex structure description.
pconvex_structure prism_P1_structure(dim_type nc)
Give a pointer on the structures of a prism of dimension d.
pconvex_structure simplex_structure(dim_type nc)
Give a pointer on the structures of a simplex of dimension d.
size_t size_type
used as the common size type in the library
std::shared_ptr< const bgeot::geometric_trans > pgeometric_trans
pointer type for a geometric transformation