⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.tex

📁 利用C
💻 TEX
字号:
\chapter{Functions}\index{functions}\index{Function}\devnote{Since this chapter was written, the \texttt{Function} class has  seen a number of improvements which are not covered here. Chapter needs  to be updated.}The central concept of a function on a domain $\Omega \subset \R^d$ ismodeled by the class \texttt{Function}, which is used in \dolfin{} torepresent coefficients or solutions of partial differential equations.%------------------------------------------------------------------------------\section{Basic properties}The following basic properties hold for all \texttt{Function}s:\begin{itemize}\item  A \texttt{Function} can be scalar or vector-valued;\item  A \texttt{Function} can be restricted (interpolated) to each local  \texttt{Cell} of a \texttt{Mesh};\item  The underlying representation of a \texttt{Function} may vary.\end{itemize}Depending on the actual underlying representation of a \texttt{Function}, itmay also be possible to evaluate a \texttt{Function} at any given \texttt{Point}.\subsection{Representation}Currently supported representations of \texttt{Function}s include\emph{discrete} \texttt{Function}s, \emph{user-defined}\texttt{Function}s and \emph{constant} \texttt{Function}s. These arediscussed in detail below.\subsection{Assignment}One \texttt{Function} may be assigned to another \texttt{Function}:\begin{code}Function v;Function u = v;\end{code}\subsection{Components and subfunctions}If a \texttt{Function} is vector-valued, or in general \emph{nested}(mixed), a new \texttt{Function} may be created to represent any given\emph{subsystem} (component) of the original \texttt{Function}, asillustrated by the following example:\begin{code}Function u;         // Function with two componentsFunction u0 = u[0]; // First subsystem (component) of uFunction u1 = u[1]; // First subsystem (component) of u\end{code}If a \texttt{Function} represents a nested function (one defined interms of a mixed finite element, see below), then indexing has theeffect of picking out subfunctions. With \texttt{w} a\texttt{Function} representing the solution $w = (u, p)$ of a Stokesor Navier-Stokes system (with $u$ the vector-valued velocity and $p$the scalar pressure), the following example illustrates how to picksubfunctions and components of \texttt{w}:\begin{code}Function w; // Mixed Function (u, p)u = w[0];   // First subfunction (velocity)p = w[1];   // Second subfunction (pressure)u0 = u[0];  // First component of the velocityu1 = u[1];  // Second component of the velocityu2 = u[2];  // Third component of the velocity\end{code}\subsection{Output}\index{ParaView}\index{MayaVi}A \texttt{Function} can be written to a file in various file formats.To write a \texttt{Function}~\texttt{u} to file in VTK~format,suitable for viewing in ParaView or MayaVi, create a file withextension \texttt{.pvd}:\begin{code}File file("solution.pvd");file << u;\end{code}For further details on available file formats, seeChapter~\ref{chapter:io}.%------------------------------------------------------------------------------\section{Discrete functions}A discrete \texttt{Function} is defined in terms of a\texttt{Vector} of degrees of freedom, a \texttt{Mesh}, alocal-to-global mapping (\texttt{DofMap}) and a finite element.  Inparticular, a discrete \texttt{Function} is given by a linearcombinations of basis functions:\begin{equation}  v = \sum_{i=1}^{N} v_i \phi_{i},\end{equation}where $\{\phi_i\}_{i=1}^N$ is the global basis of the finite elementspace defined by the \texttt{Mesh} and the finite element, andthe nodal values $\{v_i\}_{i=1}^N$ are given by the values of a\texttt{Vector}.%------------------------------------------------------------------------------\section{User-defined functions}\index{user-defined functions}In the simplest case, a user-defined \texttt{Function} is just anexpression in terms of the coordinates and is typically used fordefining source terms and initial conditions. For example, a sourceterm could be given by\begin{equation} \label{eq:functionexample}  f = f(x, y, z) = xy \sin(z / \pi).\end{equation}A user-defined \texttt{Function} may be defined by creating a subclass of \texttt{Function} and overloading the \texttt{eval()}function.  The following example illustrates how to create a\texttt{Function} representing the function in(\ref{eq:functionexample}):\begin{code}class Source : public Function{public:      Source(Mesh& mesh) : Function(mesh) {}  real eval(const real* x) const  {    return x[0]*x[1]*sin(x[2] / DOLFIN_PI);  }};Source f;\end{code}\devnote{Write about how to define vector-valued functions.}\subsection{Cell-dependent functions}In some cases, it may be convenient to define a \texttt{Function} interms of properties of the current \texttt{Cell}. One such example isa \texttt{Function} that at any given point takes the value of themesh size at that point.The following example illustrates how to create such as\texttt{Function} by overloading the \texttt{eval()} function:\begin{code}class MeshSize : public Function{public:  MeshSize(Mesh& mesh) : Function(mesh) {}  real eval(const real* x) const  {    return cell().diameter();  }    };MeshSize h;\end{code}Note that the current \texttt{Cell} is only available during assemblyand has no meaning otherwise. For example, it is not possible to write the\texttt{Function}~\texttt{h} to file.\devnote{Write about predefined special functions like\texttt{MeshSize} and \texttt{FacetNormal}.}\subsection{Simple definition of functions in Python}\devnote{This is a verbatim copy from the mailing list. It should beedited for the manual.}\footnotesize\begin{verbatim}On Wed, Jun 04, 2008 at 12:44:17PM +0200, Martin Sandve Alnaes wrote:2008/6/3 Anders Logg <logg@simula.no>:On Tue, Jun 03, 2008 at 11:23:41PM +0200, Martin Sandve Alnaes wrote:>> It's easy with some helper functions I have, I can show you tomorrow.>> It only depends on dolfin::Function (i.e. dolfin.cpp_Function) and Instant.>> ok, nice. Maybe we can add the helper functions to assemble.py.Now you can do things like this:A = assemble(..., coefficients=[myfunction, 1.23, "sin(x[0])"])1.23 is wrapped in a cpp_Function (i.e. a constant Function), andthe string is compiled as a C++ dolfin::Function like "v[0] = ...;".If you want more control over the implementation(i.e. temporary variables in eval for efficiency, if-checks, etc.)you can also do this:code = """class F: dolfin::Function { ...};class G: dolfin::Function { ...};"""f, g = compile_functions(code, mesh)Or, if you just want to precompile but don't need anything too fancy:expressions = ["sin(x[0])",                         ("a*x[1]", "b*x[0]")]f, g = compile_functions(expressions, mesh)assert f.rank() == 0assert g.rank() == 1g.a = 1.23g.b = 3.21Note that:- a tuple is interpreted as a vector expression- a tuple of tuples is interpreted as a matrix expression- variables like a and b in the strings above are detected  automatically, but this requires all builtin names to be  registered in the variable dolfin.compile_functions._builtins,  which currently contains some common mathematical  functions like sqrt, pow, exp, sin, pi etc.- variables are initialized to 0.0, so use compile_functions if  you need control over variable values.- assemble also accepts tuples like in compile_functions- assemble only calls on instant once independently of  the number of string expressions, to reduce compilation time- Error checks are not extensive!\end{verbatim}\normalsize

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -