~ubuntu-branches/ubuntu/maverick/dolfin/maverick

« back to all changes in this revision

Viewing changes to demo/pde/functional/cpp/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2008-09-16 08:41:20 UTC
  • Revision ID: james.westby@ubuntu.com-20080916084120-i8k3u6lhx3mw3py3
Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2006-2008 Anders Logg.
 
2
// Licensed under the GNU LGPL Version 2.1.
 
3
//
 
4
// First added:  2006-09-19
 
5
// Last changed: 2008-12-13
 
6
//
 
7
// This demo program computes the value of the functional
 
8
//
 
9
//     M(v) = int v^2 + (grad v)^2 dx
 
10
//
 
11
// on the unit square for v = sin(x) + cos(y). The exact
 
12
// value of the functional is M(v) = 2 + 2*sin(1)*(1-cos(1))
 
13
//
 
14
// The functional M corresponds to the energy norm for a
 
15
// simple reaction-diffusion equation.
 
16
 
 
17
#include <dolfin.h>
 
18
#include "EnergyNorm.h"
 
19
  
 
20
using namespace dolfin;
 
21
 
 
22
int main()
 
23
{
 
24
  // The function v
 
25
  class MyFunction : public Function
 
26
  {
 
27
  public: 
 
28
 
 
29
    MyFunction(const FunctionSpace& V) : Function(V) {}
 
30
 
 
31
    void eval(double* values, const double* x) const
 
32
    {
 
33
      values[0] = sin(x[0]) + cos(x[1]);
 
34
    }
 
35
    
 
36
  };
 
37
 
 
38
  // Define functional
 
39
  UnitSquare mesh(16, 16);
 
40
  EnergyNormCoefficientSpace V(mesh);
 
41
  MyFunction v(V);
 
42
  EnergyNormFunctional M;
 
43
  M.v = v;
 
44
 
 
45
  // Evaluate functional
 
46
  double approximate_value = assemble(M);
 
47
 
 
48
  // Compute exact value
 
49
  double exact_value = 2.0 + 2.0*sin(1.0)*(1.0 - cos(1.0));
 
50
 
 
51
  message("The energy norm of v is: %.15g", approximate_value);
 
52
  message("It should be:            %.15g", exact_value);
 
53
  
 
54
  return 0;
 
55
}