From c2e7e0759055947953617f0f919862a13d0bf647 Mon Sep 17 00:00:00 2001 From: bangerth Date: Tue, 22 Nov 2011 15:14:35 +0000 Subject: [PATCH] Add VectorFunctionFromScalarFunctionObject. git-svn-id: https://svn.dealii.org/trunk@24758 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 1 + deal.II/include/deal.II/base/function.h | 80 ++++++++++++++++++++++++- deal.II/source/base/function.cc | 52 +++++++++++++++- 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 5a8a6d7646..2f578603f6 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -82,6 +82,7 @@ This is in accordance to the other matrix classes.
  • New: The class ScalarFunctionFromFunctionObject provides a quick way to convert an arbitrary function into a function object that can be passed to part of the library that require the Function@ interface. +The VectorFunctionFromScalarFunctionObject class does a similar thing.
    (Wolfgang Bangerth, 2011/11/15) diff --git a/deal.II/include/deal.II/base/function.h b/deal.II/include/deal.II/base/function.h index 5073ee55ce..e356655847 100644 --- a/deal.II/include/deal.II/base/function.h +++ b/deal.II/include/deal.II/base/function.h @@ -607,7 +607,7 @@ class ConstantFunction : public ZeroFunction * This is a constant vector-valued function, in which one or more * components of the vector have a constant value and all other * components are zero. It is especially useful as a weight function - * for VectorTools::integrate_difference, where it allows to + * for VectorTools::integrate_difference, where it allows to * integrate only one or a few vector components, rather than the * entire vector-valued solution. See the step-20 * tutorial program for a detailed explanation. @@ -813,6 +813,84 @@ private: }; +/** + * This class is similar to the ScalarFunctionFromFunctionObject class in that it + * allows for the easy conversion of a function object to something that + * satisfies the interface of the Function base class. The difference is + * that here, the given function object is still a scalar function + * (i.e. it has a single value at each space point) but that the Function + * object generated is vector valued. The number of vector components + * is specified in the constructor, where one also selectes a single + * one of these vector components that should be filled by the passed + * object. The result is a vector Function object that returns zero in + * each component except the single selected one where it returns the + * value returned by the given as the first argument to the constructor. + * + * @note In the above discussion, note the difference between the + * (scalar) "function object" (i.e., a C++ object x that can + * be called as in x(p)) and the capitalized (vector valued) + * "Function object" (i.e., an object of a class that is derived from + * the Function base class). + * + * @author Wolfgang Bangerth, 2011 + */ +template +class VectorFunctionFromScalarFunctionObject : public Function +{ +public: + /** + * Given a function object that takes a Point and returns a double + * value, convert this into an object that matches the Function@ + * interface. + * + * @param function_object The scalar function that will form one component + * of the resulting Function object. + * @param n_components The total number of vector components of the + * resulting Function object. + * @param selected_component The single component that should be + * filled by the first argument. + **/ + VectorFunctionFromScalarFunctionObject (const std_cxx1x::function &)> &function_object, + const unsigned int n_components, + const unsigned int selected_component); + + /** + * Return the value of the + * function at the given + * point. Returns the value the + * function given to the constructor + * produces for this point. + */ + virtual double value (const Point &p, + const unsigned int component = 0) const; + + /** + * Return all components of a + * vector-valued function at a + * given point. + * + * values shall have the right + * size beforehand, + * i.e. #n_components. + */ + virtual void vector_value (const Point &p, + Vector &values) const; + +private: + /** + * The function object which we call when this class's value() or + * value_list() functions are called. + **/ + const std_cxx1x::function &)> function_object; + + /** + * The vector component whose value is to be filled by the + * given scalar function. + */ + const unsigned int selected_component; +}; + + DEAL_II_NAMESPACE_CLOSE #endif diff --git a/deal.II/source/base/function.cc b/deal.II/source/base/function.cc index f558d533e6..47296a638f 100644 --- a/deal.II/source/base/function.cc +++ b/deal.II/source/base/function.cc @@ -66,9 +66,8 @@ double Function::value (const Point &, template -void Function::vector_value ( - const Point& p, - Vector& v) const +void Function::vector_value (const Point& p, + Vector& v) const { AssertDimension(v.size(), this->n_components); for (unsigned int i=0;in_components;++i) @@ -538,6 +537,50 @@ ScalarFunctionFromFunctionObject::value (const Point &p, +template +VectorFunctionFromScalarFunctionObject:: +VectorFunctionFromScalarFunctionObject (const std_cxx1x::function &)> &function_object, + const unsigned int n_components, + const unsigned int selected_component) +: +Function(n_components), +function_object (function_object), +selected_component (selected_component) +{} + + + +template +double +VectorFunctionFromScalarFunctionObject::value (const Point &p, + const unsigned int component) const +{ + Assert (component < this->n_components, + ExcIndexRange (component, 0, this->n_components)); + + if (component == selected_component) + return function_object (p); + else + return 0; +} + + + +template +void +VectorFunctionFromScalarFunctionObject:: +vector_value (const Point &p, + Vector &values) const +{ + AssertDimension(values.size(), this->n_components); + + // set everything to zero, and then the right component to its correct value + values = 0; + values(selected_component) = function_object (p); +} + + + // explicit instantiations template class Function<1>; @@ -545,18 +588,21 @@ template class ZeroFunction<1>; template class ConstantFunction<1>; template class ComponentSelectFunction<1>; template class ScalarFunctionFromFunctionObject<1>; +template class VectorFunctionFromScalarFunctionObject<1>; template class Function<2>; template class ZeroFunction<2>; template class ConstantFunction<2>; template class ComponentSelectFunction<2>; template class ScalarFunctionFromFunctionObject<2>; +template class VectorFunctionFromScalarFunctionObject<2>; template class Function<3>; template class ZeroFunction<3>; template class ConstantFunction<3>; template class ComponentSelectFunction<3>; template class ScalarFunctionFromFunctionObject<3>; +template class VectorFunctionFromScalarFunctionObject<3>; DEAL_II_NAMESPACE_CLOSE -- 2.39.5