18 #ifndef FIELD_VALUES_HH_ 19 #define FIELD_VALUES_HH_ 22 #include <boost/core/explicit_operator_bool.hpp> 23 #include <boost/exception/detail/error_info_impl.hpp> 24 #include <boost/exception/info.hpp> 25 #include <boost/format.hpp> 26 #include <boost/optional/optional.hpp> 34 #include <type_traits> 87 template<
int NRows,
int NCols,
class ET>
96 template <
int NRows,
class ET>
101 template<
int NRows,
int NCols>
115 template <
class RT>
inline RT &
set_raw_scalar(RT &,
double *raw_data) {
return *raw_data;}
116 template <
class RT>
inline RT &
set_raw_scalar(RT &,
int *raw_data) {
return *raw_data;}
119 template <
class RT>
inline RT &
set_raw_vec(RT &val,
double *raw_data) { arma::access::rw(val.mem) = raw_data;
return val;}
120 template <
class RT>
inline RT &
set_raw_vec(RT &val,
int *raw_data) { arma::access::rw(val.mem) = raw_data;
return val;}
121 template <
class RT>
inline RT &
set_raw_vec(RT &val,
FieldEnum *raw_data) { arma::access::rw(val.mem) = raw_data;
return val;}
123 template <
class RT>
inline RT &
set_raw_fix(RT &val,
double *raw_data) { val = RT(raw_data);
return val;}
124 template <
class RT>
inline RT &
set_raw_fix(RT &val,
int *raw_data) { val = RT(raw_data);
return val;}
143 template<
class MatrixType>
145 typedef typename MatrixType::elem_type ET;
146 unsigned int nrows = value.n_rows;
147 unsigned int ncols = value.n_cols;
150 if (it->size() == 1 && nrows == ncols) {
155 if (rec.
size() == 1) {
157 ET scalar=*(it->begin<ET>());
158 for(
unsigned int i=0; i< nrows; i++) value.at(i,i)=scalar;
159 }
else if (rec.
size() == nrows) {
161 for(
unsigned int i=0; i< nrows; i++, ++it) value.at(i,i)=*(it->begin<ET>());
162 }
else if (rec.
size() == (nrows+1)*nrows/2) {
163 for(
unsigned int row=0; row<nrows; row++)
164 for(
unsigned int col=0; col<ncols; col++)
166 value.at(row,col) = *(it->begin<ET>());
168 }
else value.at(row,col) = value.at(col,row);
172 boost::str(
boost::format(
"Initializing symmetric matrix %dx%d by vector of wrong size %d, should be 1, %d, or %d.")
173 % nrows % ncols % rec.
size() % nrows % ((nrows+1)*nrows/2)))
181 if (rec.
size() == nrows && it->size() == ncols) {
183 for (
unsigned int row = 0; row < nrows; row++, ++it) {
184 if (it->size() != ncols)
185 THROW( ExcFV_Input() << EI_InputMsg(
"Wrong number of columns.")
188 for (
unsigned int col = 0; col < ncols; col++, ++col_it)
189 value.at(row, col) = *col_it;
194 boost::str(
boost::format(
"Initializing matrix %dx%d by matrix of wrong size %dx%d.")
195 % nrows % ncols % rec.
size() % it->size() ))
207 template<
class VectorType>
209 typedef typename VectorType::elem_type ET;
210 unsigned int nrows = value.n_rows;
215 if ( rec.
size() == 1 ) {
216 for(
unsigned int i=0; i< nrows; i++)
218 }
else if ( rec.
size() == nrows ) {
219 for(
unsigned int i=0; i< nrows; i++, ++it) {
225 boost::str(
boost::format(
"Initializing vector of size %d by vector of size %d.")
226 % nrows % rec.
size() ))
247 template <
int NRows,
int NCols,
class ET>
254 const static int NRows_ = NRows;
255 const static int NCols_ = NCols;
256 const static int rank_ = 2;
260 if (NRows == NCols) {
277 return value_.memptr();
282 return arr.template mat<NRows, NCols>(idx);
294 inline ET &operator() (
unsigned int i,
unsigned int j)
295 {
return value_.at(i,j); }
296 inline ET operator() (
unsigned int i,
unsigned int j)
const 297 {
return value_.at(i,j); }
298 inline operator return_type()
const 315 return arma::max(arma::max(arma::abs(value_ - other))) < 4*std::numeric_limits<ET>::epsilon();
320 for(
unsigned int row=0; row<n_rows(); row++)
321 for(
unsigned int col=0; col<n_cols(); col++)
322 value_.at(row,col) = scale_coef * value_.at(row,col);
342 const static int NRows_ = 1;
343 const static int NCols_ = 1;
344 const static int rank_ = 0;
362 const ET *
mem_ptr()
const {
return &(value_); }
376 inline ET &operator() (
unsigned int,
unsigned int )
378 inline ET operator() (
unsigned int,
unsigned int)
const 380 inline operator return_type()
const 396 return std::abs((
double)value_ - other) < 4*std::numeric_limits<ET>::epsilon();
401 value_ = scale_coef * value_;
420 const static int NRows_ = 0;
421 const static int NCols_ = 1;
422 const static int rank_ = 10;
433 const ET *
mem_ptr()
const {
return value_.memptr(); }
437 return arr.template vec<NRows_>(idx);
447 void set_n_comp(
unsigned int n_comp) { value_ = return_type(n_comp,1); };
451 {
return value_.n_rows; }
452 inline ET &operator() (
unsigned int i,
unsigned int )
453 {
return value_.at(i); }
454 inline ET operator() (
unsigned int i,
unsigned int )
const 455 {
return value_.at(i); }
457 inline operator return_type()
const 474 return arma::max(arma::abs(value_ - other)) < 4*std::numeric_limits<ET>::epsilon();
479 for(
unsigned int i=0; i< n_rows(); i++) {
480 value_.at(i) = scale_coef * value_.at(i);
491 template <
int NRows,
class ET>
498 const static int NRows_ = NRows;
499 const static int NCols_ = 1;
500 const static int rank_ = 1;
513 const ET *
mem_ptr()
const {
return value_.memptr(); }
517 return arr.template vec<NRows>(idx);
529 inline ET &operator() (
unsigned int i,
unsigned int )
530 {
return value_.at(i); }
531 inline ET operator() (
unsigned int i,
unsigned int )
const 532 {
return value_.at(i); }
534 inline operator return_type()
const 551 return arma::max(arma::abs(value_ - other)) < 4*std::numeric_limits<ET>::epsilon();
556 for(
unsigned int i=0; i< n_rows(); i++) {
557 value_.at(i) = scale_coef * value_.at(i);
579 : n_rows(n_rows),n_cols(n_cols), values_(n_rows*n_cols) {}
581 std::string &
at(
unsigned int row) {
return at(row,0); }
582 std::string &
at(
unsigned int row,
unsigned int col) {
return values_[col*n_rows+row]; }
585 for(
auto &elem: values_) elem =
"0.0";
595 template<
int NRows,
int NCols>
600 if (NRows == NCols) {
620 tensor.
at(0,0) = input;
644 template <
int spacedim>
void init_from_input(AccessType val)
internal::ReturnType< 1, 1, ET >::return_type return_type
arma::Col< ET > return_type
internal::InputType< ET >::type ElementInputType
static const return_type & from_raw(return_type &val, ET *raw_data)
static IT::Array get_input_type()
void set_n_comp(unsigned int)
arma::Col< unsigned int > return_type
std::vector< std::string > values_
RT & set_raw_scalar(RT &, double *raw_data)
internal::InputType< ET >::type ElementInputType
bool equal_to(const return_type &other)
FieldValue_< 0, 1, int > IntVector
static return_type get_from_array(const Armor::Array< element_type > &arr, uint idx)
Casts value stored in Armor::Array to return type.
static const return_type & from_raw(return_type &val, ET *raw_data)
void set_n_comp(unsigned int n_comp)
std::string format(CStringRef format_str, ArgList args)
const ET * mem_ptr() const
void set_n_comp(unsigned int)
void init_from_input(AccessType rec)
void scale(double scale_coef)
static std::string type_name()
RT & set_raw_vec(RT &val, double *raw_data)
void set_n_comp(unsigned int)
unsigned int n_cols() const
internal::ReturnType< NRows, NCols, ET >::return_type return_type
FieldValue_(return_type &val)
void init_from_input(AccessType rec)
static return_type get_from_array(const Armor::Array< element_type > &arr, uint idx)
Casts value stored in Armor::Array to return type.
FieldValue_< 1, 1, FieldEnum > Enum
void init_matrix_from_input(MatrixType &value, Input::Array rec)
void init_from_input(AccessType rec)
static constexpr bool value
arma::Col< unsigned int >::template fixed< NRows > return_type
StringTensor(unsigned int n_rows, unsigned int n_cols)
internal::InputType< ET >::type ElementInputType
static IT::Array get_input_type()
Type scalar(uint mat_index) const
static std::string type_name()
FieldValue_< 0, 1, FieldEnum > EnumVector
const ET * mem_ptr() const
static constexpr bool is_scalable()
internal::InputType< ET >::type ElementInputType
const ET * mem_ptr() const
internal::ReturnType< NRows, 1, ET >::return_type return_type
void scale(double scale_coef)
bool equal_to(const return_type &other)
static constexpr bool is_scalable()
std::string & at(unsigned int row, unsigned int col)
FieldValue_< spacedim, 1, double > VectorFixed
bool equal_to(const return_type &other)
void init_vector_from_input(VectorType &value, Input::Array rec)
static std::string type_name()
unsigned int n_rows() const
RT & set_raw_fix(RT &val, double *raw_data)
arma::Mat< ET >::template fixed< NRows, NCols > return_type
internal::ReturnType< 0, 1, ET >::return_type return_type
static constexpr bool is_scalable()
TYPEDEF_ERR_INFO(EI_InputMsg, const string)
std::string & at(unsigned int row)
unsigned int n_cols() const
static IT::Array get_input_type()
unsigned int n_rows() const
arma::Mat< unsigned int >::template fixed< NRows, NCols > return_type
unsigned int n_cols() const
void scale(double scale_coef)
FieldValue_< 1, 1, int > Integer
FieldValue_(return_type &val)
FieldValue_< 0, 1, double > Vector
internal::AccessTypeDispatch< ET >::type AccessType
static std::string type_name()
DECLARE_INPUT_EXCEPTION(ExcFV_Input,<< "Wrong field value input: "<< EI_InputMsg::val)
static const return_type & from_raw(return_type &val, ET *raw_data)
FieldValue_(return_type &val)
void scale(double scale_coef)
static const return_type & from_raw(return_type &val, ET *raw_data)
FieldValue_< spacedim, spacedim, double > TensorFixed
unsigned int n_rows() const
unsigned int n_cols() const
arma::Col< ET >::template fixed< NRows > return_type
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
static return_type get_from_array(const Armor::Array< element_type > &arr, uint idx)
Casts value stored in Armor::Array to return type.
static return_type get_from_array(const Armor::Array< element_type > &arr, uint idx)
Casts value stored in Armor::Array to return type.
static IT::Parameter get_input_type()
FieldValue_< 1, 1, double > Scalar
const ET * mem_ptr() const
bool equal_to(const return_type &other)
FieldValue_(return_type &val)
unsigned int n_rows() const
static constexpr bool is_scalable()