/usr/include/rheolef/field_expr.h is in librheolef-dev 6.6-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | #ifndef _RHEOLEF_FIELD_EXPR_H
#define _RHEOLEF_FIELD_EXPR_H
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///
/// =========================================================================
//
// expressions of field without temporaries, based on boost::proto
// and that uses an iterator for the assignment
//
// author: Pierre.Saramito@imag.fr
//
// date: 23 february 2011
//
#include "rheolef/field.h"
#include "rheolef/field_component.h"
#include "rheolef/promote.h"
#include "rheolef/dis_accumulate.h"
#include "rheolef/dis_inner_product.h"
#include "rheolef/pretty_name.h"
#include "rheolef/form.h"
#include <boost/mpl/bool.hpp>
#include <boost/proto/core.hpp>
#include <boost/proto/debug.hpp>
#include <boost/proto/context.hpp>
#include <boost/proto/transform.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/typeof/std/vector.hpp>
#include <boost/typeof/std/complex.hpp>
#include <boost/type_traits/remove_reference.hpp>
namespace rheolef {
namespace mpl = boost::mpl;
namespace proto = boost::proto;
using proto::_;
// forward declaration:
template <class Expr> struct field_expr;
template <class Expr> class field_nonlinear_expr;
template <class T, class M, class VfTag> class test_basic;
template <class RawExpr, class VfTag> class field_vf_expr;
template <class RawExpr> class form_vf_expr;
// -----------------------------------------------------------
// iterator:
// expr.begin_dof()
// -----------------------------------------------------------
template<class Iterator>
struct field_iterator_wrapper {
typedef Iterator iterator;
explicit field_iterator_wrapper (Iterator iter1) : iter(iter1) {}
mutable Iterator iter;
};
struct field_begin : proto::callable {
template<class Sig>
struct result;
template<class This, class Container>
struct result<This(Container)> : proto::result_of::as_expr
<
field_iterator_wrapper<
typename boost::remove_reference<Container>::type::const_iterator>
> {};
template<class Container>
typename result<field_begin(const Container&)>::type
operator ()(const Container& cont) const {
field_iterator_wrapper<typename Container::const_iterator> iter (cont.begin_dof());
return proto::as_expr (iter);
}
};
// Here is the grammar that replaces field terminals with their begin iterators
struct field_grammar_begin
: proto::or_<
proto::when <proto::terminal<field_basic<_, _> >, field_begin(proto::_value)>
, proto::when <proto::terminal<field_indirect<_, _> >, field_begin(proto::_value)>
, proto::when <proto::terminal<field_indirect_const<_, _> >, field_begin(proto::_value)>
, proto::when <proto::terminal<field_component<_, _> >, field_begin(proto::_value)>
, proto::when <proto::terminal<field_component_const<_, _> >, field_begin(proto::_value)>
, proto::when <proto::terminal<_> >
, proto::when <proto::nary_expr<_, proto::vararg<field_grammar_begin> > >
>
{};
// Here is an evaluation context that dereferences iterator terminals.
struct field_dereference_context {
// Unless this is an iterator terminal, use the default evaluation context
template<class Expr, class EnableIf = void>
struct eval : proto::default_eval<Expr, const field_dereference_context> {};
// Dereference iterator terminals.
template<class Expr>
struct eval<Expr,
typename boost::enable_if<
proto::matches<Expr, proto::terminal<field_iterator_wrapper<_> > >
>::type
> {
typedef typename proto::result_of::value<Expr>::type IteratorWrapper;
typedef typename IteratorWrapper::iterator iterator;
typedef typename std::iterator_traits<iterator>::reference result_type;
result_type operator() (Expr &expr, const field_dereference_context&) const {
return *proto::value(expr).iter;
}
};
};
// Here is an evaluation context that increments iterator
// terminals.
struct field_increment_context {
// Unless this is an iterator terminal, use the default evaluation context
template<class Expr, class EnableIf = void>
struct eval : proto::null_eval<Expr, const field_increment_context> {};
// advance iterator terminals.
template<class Expr> struct eval<Expr,
typename boost::enable_if<
proto::matches<Expr, proto::terminal<field_iterator_wrapper<_> > >
>::type
> {
typedef void result_type;
result_type operator() (Expr &expr, const field_increment_context&) const {
++proto::value(expr).iter;
}
};
};
// -----------------------------------------------------------
// Here is an evaluation context that indexes into a field
// expression and combines the result:
// expr.dof(i)
// -----------------------------------------------------------
struct field_subscript_context {
typedef std::size_t size_type;
field_subscript_context (size_type i) : _i(i) {}
// Unless this is a field terminal, use the default evaluation context
template<class Expr, class EnableIf = void>
struct eval : proto::default_eval<Expr, const field_subscript_context> {};
// Index field terminals with our subscript.
template<class Expr>
struct eval<Expr, typename boost::enable_if<
mpl::or_<
proto::matches<Expr, proto::terminal<field_basic<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect_const<_, _> > >
,proto::matches<Expr, proto::terminal<field_component<_, _> > >
,proto::matches<Expr, proto::terminal<field_component_const<_, _> > >
>
>::type>
{
typedef typename proto::result_of::value<Expr>::type::value_type result_type;
result_type operator() (Expr& expr, const field_subscript_context& ctx) const
{
return proto::value(expr).dof(ctx._i);
}
};
// Index iterator on field terminals
template<class Expr>
struct eval<Expr, typename boost::enable_if<
proto::matches<Expr, proto::terminal<field_iterator_wrapper<_> > > >::type > {
typedef typename proto::result_of::value<Expr>::type IteratorWrapper;
typedef typename IteratorWrapper::iterator iterator;
typedef typename std::iterator_traits<iterator>::reference result_type;
result_type operator() (Expr &expr, const field_subscript_context& ctx) const {
return proto::value(expr).iter [ctx._i];
}
};
mutable size_type _i;
};
// -----------------------------------------------------------
// get size
// expr.ndof()
// -----------------------------------------------------------
struct field_get_size_context {
typedef std::size_t size_type;
// allocator:
field_get_size_context () {}
// eval:
template<class Expr, class EnableIf = void>
struct eval : proto::null_eval<Expr, const field_get_size_context> {};
template<class Expr>
struct eval<Expr, typename boost::enable_if<
mpl::or_<
proto::matches<Expr, proto::terminal<field_basic<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect_const<_, _> > >
,proto::matches<Expr, proto::terminal<field_component<_, _> > >
,proto::matches<Expr, proto::terminal<field_component_const<_, _> > >
>
>::type>
{
typedef void result_type;
result_type operator() (Expr& expr, const field_get_size_context& ctx) const
{
ctx._ownership = proto::value(expr).ownership();
}
};
// accessors:
size_type ndof() const { return _ownership.size(); };
distributor ownership() const { return _ownership; };
// data:
mutable distributor _ownership;
};
// -----------------------------------------------------------
// get space
// expr.get_space()
// -----------------------------------------------------------
template<class T, class M>
struct field_get_space_context {
typedef typename field_basic<T,M>::space_type space_type;
// allocator:
field_get_space_context () {}
// eval:
template<class Expr, class EnableIf = void>
struct eval : proto::null_eval<Expr, const field_get_space_context<T,M> > {};
template<class Expr>
struct eval<Expr, typename boost::enable_if<
mpl::or_<
proto::matches<Expr, proto::terminal<field_basic<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect_const<_, _> > >
,proto::matches<Expr, proto::terminal<field_component<_, _> > >
,proto::matches<Expr, proto::terminal<field_component_const<_, _> > >
>
>::type>
{
typedef void result_type;
result_type operator() (Expr &expr, const field_get_space_context<T,M>& ctx) const
{
ctx._V = proto::value(expr).get_space();
}
};
// accessors:
space_type get_space() const { return _V; };
// data:
mutable space_type _V;
};
// -----------------------------------------------------------
// check that stamp match
// expr.stamp()
// -----------------------------------------------------------
struct field_check_stamp_context {
field_check_stamp_context (std::string stamp) : _stamp(stamp) {}
template<class Expr, class EnableIf = void>
struct eval : proto::null_eval<Expr, const field_check_stamp_context> {};
template<class Expr>
struct eval<Expr, typename boost::enable_if<
mpl::or_<
proto::matches<Expr, proto::terminal<field_basic<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect<_, _> > >
,proto::matches<Expr, proto::terminal<field_indirect_const<_, _> > >
,proto::matches<Expr, proto::terminal<field_component<_, _> > >
,proto::matches<Expr, proto::terminal<field_component_const<_, _> > >
>
>::type>
{
typedef void result_type;
result_type operator() (Expr &expr, const field_check_stamp_context& ctx) const
{
check_macro (ctx._stamp == proto::value(expr).stamp(),
"incompatible spaces " << ctx._stamp << " and "
<< proto::value(expr).stamp() << " in field expression ");
}
};
mutable std::string _stamp;
};
// -----------------------------------------------------------
// A grammar which matches all the assignment operators,
// so we can easily disable them.
// -----------------------------------------------------------
struct field_assign_operators : proto::switch_<struct field_assign_operators_cases> {};
// Here are the cases used by the switch_ above.
struct field_assign_operators_cases {
template<class Tag, int D = 0> struct case_ : proto::not_<_> {};
template<int D> struct case_< proto::tag::plus_assign, D > : _ {};
template<int D> struct case_< proto::tag::minus_assign, D > : _ {};
template<int D> struct case_< proto::tag::multiplies_assign, D > : _ {};
template<int D> struct case_< proto::tag::divides_assign, D > : _ {};
};
// An expression conforms to the field_grammar if it is a terminal or some
// op that is not an assignment op. (Assignment will be handled specially.)
// expr : +expr | -expr | expr + expr | expr - expr
// | c*expr | expr*c | expr/c
// | f(expr) | f(expr,expr)
// | += expr | -= expr
struct field_terminal :
proto::or_<
proto::terminal<field_basic<_, _> >
,proto::terminal<field_indirect<_, _> >
,proto::terminal<field_indirect_const<_, _> >
,proto::terminal<field_component<_, _> >
,proto::terminal<field_component_const<_, _> >
>
{};
struct field_constant_expr :
proto::and_<
proto::terminal<_>
,proto::not_<
proto::or_< // "proto::or" limited to 6 or 8 args
proto::or_<
proto::terminal<field_basic<_, _> >
,proto::terminal<field_indirect<_, _> >
,proto::terminal<field_indirect_const<_, _> >
,proto::terminal<field_component<_, _> >
,proto::terminal<field_component_const<_, _> >
>
,proto::or_<
proto::terminal<form_basic<_, _> >
,proto::terminal<field_nonlinear_expr<_> >
,proto::terminal<test_basic<_,_,_> >
,proto::terminal<field_vf_expr<_,_> >
,proto::terminal<form_vf_expr<_> >
>
>
>
>
{};
struct field_grammar
: proto::or_< // "proto::or" limited to 6 or 8 args
proto::or_<
field_terminal
,field_constant_expr
,proto::unary_plus <field_grammar> // -expr
,proto::negate <field_grammar> // +expr
,proto::plus <field_grammar, field_grammar>
,proto::minus <field_grammar, field_grammar>
>
,proto::or_<
proto::multiplies <field_constant_expr, field_grammar> // as 2*uh
,proto::multiplies <field_grammar, field_constant_expr> // as uh*2
,proto::divides <field_grammar, field_constant_expr> // as uh/2
>
>
{};
// Expressions in the field_domain will be wrapped in field_expr<>
// and must conform to the field_grammar
struct field_domain : proto::domain<proto::generator<field_expr>, field_grammar> {};
// -------------------------------------------------------------------
// Here is field_expr, a wrapper for expression types in the field_domain.
// -------------------------------------------------------------------
template<class Expr>
struct field_expr : proto::extends<Expr, field_expr<Expr>, field_domain> {
// typedefs :
typedef field_get_size_context::size_type size_type;
typedef typename boost::result_of<field_grammar_begin(const Expr&)>::type raw_iterator;
typedef rheo_default_memory_model memory_type; // TODO: compute it at compile time
// allocators:
explicit field_expr (const Expr& expr)
: proto::extends<Expr, field_expr<Expr>, field_domain>(expr)
{}
// accessors:
// TODO: space_type get_space() const;
// pb: space_type depends upon Expr
distributor ownership() const {
const field_get_size_context get_size;
proto::eval (*this, get_size);
return get_size.ownership();
}
size_type ndof() const {
return ownership().size();
}
size_type dis_ndof() const {
return ownership().dis_size();
}
struct const_iterator_begin : raw_iterator {
typedef std::input_iterator_tag iterator_category;
typedef typename proto::result_of::eval<raw_iterator,field_dereference_context>::type value_type;
typedef value_type reference;
typedef value_type* pointer;
typedef std::ptrdiff_t difference_type;
#if BOOST_VERSION < 104601
const_iterator_begin (const field_expr<Expr>& expr)
: raw_iterator(field_grammar_begin() (expr))
{}
#else // BOOST_VERSION < 104601
const_iterator_begin (const field_expr<Expr>& expr)
: raw_iterator(field_grammar_begin() (expr.proto_expr_))
// ICI: il faut recuperer base_type(expr) de type Expr au lieu field_expr<Expr>
{}
#endif // BOOST_VERSION < 104601
const_iterator_begin& operator++ () {
const field_increment_context inc = {};
proto::eval (*this, inc);
return *this;
}
const_iterator_begin operator++ (int) {
const_iterator_begin tmp = *this;
operator++();
return tmp;
}
reference operator* () {
const field_dereference_context deref = {};
typedef typename proto::result_of::eval<raw_iterator,field_dereference_context>::type my_ref;
reference value = proto::eval(*this, deref);
return value;
}
};
typedef const_iterator_begin const_iterator;
const_iterator begin_dof() const { return const_iterator(*this); }
// typedefs:
typedef typename proto::result_of::eval<const Expr, const field_subscript_context>::type value_type;
typedef value_type result_type;
typedef typename scalar_traits<value_type>::type scalar_type;
typedef typename float_traits<value_type>::type float_type;
// random access:
scalar_type dof (size_type i) const {
const field_subscript_context get_subscript(i);
return proto::eval(*this, get_subscript);
}
#ifdef TODO
scalar_type max () const {
scalar_type val = std::numeric_limits<scalar_type>::min();
for (const_iterator iter = begin_dof(), last = end_dof(); iter != last; iter++) {
val = std::max(val, *iter);
}
return val;
}
scalar_type max_abs () const { return abs(*this).max(); }
scalar_type min_abs () const { return abs(*this).min(); }
#endif // TODO
private:
// hide this:
using proto::extends<Expr, field_expr<Expr>, field_domain>::operator[];
};
template <class Expr>
inline
odiststream& operator<< (odiststream& ops, const field_expr<Expr>& uh)
{
typedef typename field_expr<Expr>::scalar_type T;
field_basic<T> tmp = uh; // TODO: add M as <T,M>
return tmp.put (ops);
}
// Define a trait type for detecting field terminals, to
// be used by the BOOST_PROTO_DEFINE_OPERATORS macro below.
template<class T>
struct is_field : mpl::false_ {};
template<class T, class M>
struct is_field<field_basic<T, M> > : mpl::true_ {};
template<class T, class M>
struct is_field<field_indirect<T, M> > : mpl::true_ {};
template<class T, class M>
struct is_field<field_indirect_const<T, M> > : mpl::true_ {};
template<class T, class M>
struct is_field<field_component<T, M> > : mpl::true_ {};
template<class T, class M>
struct is_field<field_component_const<T, M> > : mpl::true_ {};
// -------------------------------------------
// x = expr
// -------------------------------------------
namespace field_detail {
template<class FwdIter, class Expr, class Op>
void evaluate (FwdIter begin, FwdIter end, const Expr& expr, Op op) {
const field_increment_context inc = {};
const field_dereference_context deref = {};
typename boost::result_of<field_grammar_begin(const Expr&)>::type expr_begin
= field_grammar_begin() (expr);
for (; begin != end; ++begin) {
op (*begin, proto::eval(expr_begin, deref));
proto::eval (expr_begin, inc);
}
}
struct assign_op {
template<class T, class U>
void operator() (T &t, const U &u) const { t = u; }
};
} // namespace field_detail
template<class T, class M>
template<class Expr>
inline
field_basic<T,M>&
field_basic<T,M>::operator= (const field_expr<Expr>& expr) {
dis_dof_update_needed();
// get and check sizes:
const field_get_size_context get_size;
proto::eval (proto::as_expr<field_domain>(expr), get_size);
size_type expr_size = get_size.ndof();
if (stamp() == "") {
const field_get_space_context<T,M> get_space;
proto::eval (proto::as_expr<field_domain>(expr), get_space);
space_type V = get_space.get_space();
resize (V);
} else {
const field_check_stamp_context check_stamp (stamp());
proto::eval (proto::as_expr<field_domain>(expr), check_stamp); // error if the spaces arent compatibles
}
// perform all computations here:
field_detail::evaluate (begin_dof(), end_dof(), proto::as_expr<field_domain>(expr), field_detail::assign_op());
return *this;
}
template<class T, class M>
template<class Expr>
inline
field_component<T,M>&
field_component<T,M>::operator= (const field_expr<Expr>& expr) {
// get and check sizes:
const field_get_size_context get_size;
proto::eval (proto::as_expr<field_domain>(expr), get_size);
size_type expr_size = get_size.ndof();
const field_check_stamp_context check_stamp (stamp());
proto::eval (proto::as_expr<field_domain>(expr), check_stamp); // error if the spaces arent compatibles
field_detail::evaluate (begin_dof(), end_dof(), proto::as_expr<field_domain>(expr), field_detail::assign_op());
return *this;
}
template<class T, class M>
template<class Expr>
inline
field_basic<T,M>::field_basic (const field_expr<Expr>& expr)
: _V (),
_u (),
_b (),
_dis_dof_update_needed(true)
{
operator= (expr);
}
// ---------------------------------------------------------------------------
// TODO: form := diag(field_expr) => how to deduce M from field_expr<Expr> ?
// ---------------------------------------------------------------------------
template<class Expr>
inline
form_basic<typename field_expr<Expr>::scalar_type, rheo_default_memory_model>
diag (const field_expr<Expr>& expr)
{
typedef typename field_expr<Expr>::scalar_type T;
return diag(field_basic<T,rheo_default_memory_model>(expr));
}
} // namespace rheolef
#endif // _RHEOLEF_FIELD_EXPR_H
|