Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-tectonic
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
agnumpde
dune-tectonic
Commits
f983033e
Commit
f983033e
authored
13 years ago
by
Elias Pipping
Committed by
Elias Pipping
13 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Move SampleFunction to separate header file
parent
cb619c10
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/Makefile.am
+2
-1
2 additions, 1 deletion
src/Makefile.am
src/bisection-example-new.cc
+1
-135
1 addition, 135 deletions
src/bisection-example-new.cc
src/samplefunctional.hh
+143
-0
143 additions, 0 deletions
src/samplefunctional.hh
with
146 additions
and
136 deletions
src/Makefile.am
+
2
−
1
View file @
f983033e
...
...
@@ -7,7 +7,8 @@ noinst_PROGRAMS = \
bisection_example_new_SOURCES
=
\
bisection-example-new.cc
\
mynonlinearity.cc
\
properscalarincreasingconvexfunction.hh
properscalarincreasingconvexfunction.hh
\
samplefunctional.hh
check-am
:
./bisection-example-new
...
...
This diff is collapsed.
Click to expand it.
src/bisection-example-new.cc
+
1
−
135
View file @
f983033e
...
...
@@ -5,142 +5,8 @@
#endif
#include
<dune/common/exceptions.hh>
#include
<dune/common/stdstreams.hh>
#include
<dune/tnnmg/problem-classes/bisection.hh>
#include
<dune/tnnmg/problem-classes/directionalconvexfunction.hh>
#include
"mynonlinearity.cc"
#include
"properscalarincreasingconvexfunction.hh"
template
<
int
dimension
,
class
Function
=
TrivialFunction
>
class
SampleFunctional
{
public:
typedef
Dune
::
FieldVector
<
double
,
dimension
>
SmallVector
;
typedef
Dune
::
FieldMatrix
<
double
,
dimension
,
dimension
>
SmallMatrix
;
typedef
Function
FunctionType
;
SampleFunctional
(
SmallMatrix
_A
,
SmallVector
_b
)
:
A
(
_A
),
b
(
_b
),
func_
()
{}
double
operator
()(
const
SmallVector
v
)
const
{
SmallVector
y
;
A
.
mv
(
v
,
y
);
// y = Av
y
/=
2
;
// y = 1/2 Av
y
-=
b
;
// y = 1/2 Av - b
return
y
*
v
+
func_
(
v
.
two_norm
());
// <1/2 Av - b,v> + H(|v|)
}
SmallVector
descentDirection
(
const
SmallVector
x
)
const
{
if
(
x
==
SmallVector
(
0.0
))
{
SmallVector
d
=
SmoothGrad
(
x
);
// Decline of the smooth part in the negative gradient direction
double
smoothDecline
=
-
(
d
*
d
);
double
nonlinearDecline
=
func_
.
rightDifferential
(
0.0
)
*
d
.
two_norm
();
// TODO: is this correct?
double
combinedDecline
=
smoothDecline
+
nonlinearDecline
;
return
(
combinedDecline
<
0
)
?
d
:
SmallVector
(
0.0
);
}
SmallVector
const
pg
=
PlusGrad
(
x
);
SmallVector
const
mg
=
MinusGrad
(
x
);
SmallVector
ret
;
// TODO: collinearity checks suck
if
(
pg
*
x
==
pg
.
two_norm
()
*
x
.
two_norm
()
&&
-
(
mg
*
x
)
==
mg
.
two_norm
()
*
x
.
two_norm
())
{
return
SmallVector
(
0
);
}
else
if
(
pg
*
x
>=
0
&&
mg
*
x
>=
0
)
{
ret
=
pg
;
}
else
if
(
pg
*
x
<=
0
&&
mg
*
x
<=
0
)
{
ret
=
mg
;
}
else
{
ret
=
project
(
SmoothGrad
(
x
),
x
);
}
ret
*=
-
1
;
return
ret
;
}
SmallMatrix
A
;
SmallVector
b
;
private
:
Function
func_
;
// Gradient of the smooth part
SmallVector
SmoothGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
;
A
.
mv
(
x
,
y
);
// y = Av
y
-=
b
;
// y = Av - b
return
y
;
}
SmallVector
PlusGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
=
SmoothGrad
(
x
);
y
.
axpy
(
func_
.
rightDifferential
(
x
.
two_norm
())
/
x
.
two_norm
(),
x
);
return
y
;
}
SmallVector
MinusGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
=
SmoothGrad
(
x
);
y
.
axpy
(
func_
.
leftDifferential
(
x
.
two_norm
())
/
x
.
two_norm
(),
x
);
return
y
;
}
// No normalising is done!
SmallVector
project
(
const
SmallVector
z
,
const
SmallVector
x
)
const
{
SmallVector
y
=
z
;
y
.
axpy
(
-
(
z
*
x
)
/
x
.
two_norm2
(),
x
);
return
y
;
}
};
template
<
class
Functional
>
void
minimise
(
const
Functional
J
,
const
typename
Functional
::
SmallVector
x
,
typename
Functional
::
SmallVector
&
corr
)
{
typedef
typename
Functional
::
SmallVector
SmallVector
;
SmallVector
descDir
=
J
.
descentDirection
(
x
);
if
(
descDir
==
SmallVector
(
0.0
))
{
corr
=
SmallVector
(
0.0
);
return
;
}
// {{{ Construct a restriction of J to the line x + t * descDir
/* We have
1/2 <A(u+xv),u+xv>-<b,u+xv> = 1/2 <Av,v> x^2 - <b-Au,v> x + <1/2 Au-b,u>
since A is symmetric.
*/
SmallVector
tmp
;
J
.
A
.
mv
(
descDir
,
tmp
);
// Av
double
const
JRestA
=
tmp
*
descDir
;
// <Av,v>
J
.
A
.
mv
(
x
,
tmp
);
// Au
double
const
JRestb
=
(
J
.
b
-
tmp
)
*
descDir
;
// <b-Au,v>
typedef
MyNonlinearity
<
SmallVector
::
dimension
,
typename
Functional
::
FunctionType
>
MyNonlinearityType
;
MyNonlinearityType
phi
;
typedef
DirectionalConvexFunction
<
MyNonlinearityType
>
MyDirectionalConvexFunctionType
;
MyDirectionalConvexFunctionType
JRest
(
JRestA
,
JRestb
,
phi
,
x
,
descDir
);
// }}}
// FIXME: default values are used
Bisection
bisection
;
int
count
;
// FIXME: does x_old = 1 make any sense?!
double
const
stepsize
=
bisection
.
minimize
(
JRest
,
0.0
,
1.0
,
count
);
Dune
::
dverb
<<
"Number of iterations in the bisection method: "
<<
count
<<
std
::
endl
;
;
corr
=
descDir
;
corr
*=
stepsize
;
}
#include
"samplefunctional.hh"
template
<
int
dim
,
class
Function
>
void
functionTester
(
...
...
This diff is collapsed.
Click to expand it.
src/samplefunctional.hh
0 → 100644
+
143
−
0
View file @
f983033e
/* -*- mode:c++; mode:semantic -*- */
#ifndef SAMPLE_FUNCTIONAL_HH
#define SAMPLE_FUNCTIONAL_HH
#include
<dune/common/stdstreams.hh>
#include
<dune/tnnmg/problem-classes/bisection.hh>
#include
<dune/tnnmg/problem-classes/directionalconvexfunction.hh>
#include
"mynonlinearity.cc"
#include
"properscalarincreasingconvexfunction.hh"
template
<
int
dimension
,
class
Function
=
TrivialFunction
>
class
SampleFunctional
{
public:
typedef
Dune
::
FieldVector
<
double
,
dimension
>
SmallVector
;
typedef
Dune
::
FieldMatrix
<
double
,
dimension
,
dimension
>
SmallMatrix
;
typedef
Function
FunctionType
;
SampleFunctional
(
SmallMatrix
_A
,
SmallVector
_b
)
:
A
(
_A
),
b
(
_b
),
func_
()
{}
double
operator
()(
const
SmallVector
v
)
const
{
SmallVector
y
;
A
.
mv
(
v
,
y
);
// y = Av
y
/=
2
;
// y = 1/2 Av
y
-=
b
;
// y = 1/2 Av - b
return
y
*
v
+
func_
(
v
.
two_norm
());
// <1/2 Av - b,v> + H(|v|)
}
SmallVector
descentDirection
(
const
SmallVector
x
)
const
{
if
(
x
==
SmallVector
(
0.0
))
{
SmallVector
d
=
SmoothGrad
(
x
);
// Decline of the smooth part in the negative gradient direction
double
smoothDecline
=
-
(
d
*
d
);
double
nonlinearDecline
=
func_
.
rightDifferential
(
0.0
)
*
d
.
two_norm
();
// TODO: is this correct?
double
combinedDecline
=
smoothDecline
+
nonlinearDecline
;
return
(
combinedDecline
<
0
)
?
d
:
SmallVector
(
0.0
);
}
SmallVector
const
pg
=
PlusGrad
(
x
);
SmallVector
const
mg
=
MinusGrad
(
x
);
SmallVector
ret
;
// TODO: collinearity checks suck
if
(
pg
*
x
==
pg
.
two_norm
()
*
x
.
two_norm
()
&&
-
(
mg
*
x
)
==
mg
.
two_norm
()
*
x
.
two_norm
())
{
return
SmallVector
(
0
);
}
else
if
(
pg
*
x
>=
0
&&
mg
*
x
>=
0
)
{
ret
=
pg
;
}
else
if
(
pg
*
x
<=
0
&&
mg
*
x
<=
0
)
{
ret
=
mg
;
}
else
{
ret
=
project
(
SmoothGrad
(
x
),
x
);
}
ret
*=
-
1
;
return
ret
;
}
SmallMatrix
A
;
SmallVector
b
;
private
:
Function
func_
;
// Gradient of the smooth part
SmallVector
SmoothGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
;
A
.
mv
(
x
,
y
);
// y = Av
y
-=
b
;
// y = Av - b
return
y
;
}
SmallVector
PlusGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
=
SmoothGrad
(
x
);
y
.
axpy
(
func_
.
rightDifferential
(
x
.
two_norm
())
/
x
.
two_norm
(),
x
);
return
y
;
}
SmallVector
MinusGrad
(
const
SmallVector
x
)
const
{
SmallVector
y
=
SmoothGrad
(
x
);
y
.
axpy
(
func_
.
leftDifferential
(
x
.
two_norm
())
/
x
.
two_norm
(),
x
);
return
y
;
}
// No normalising is done!
SmallVector
project
(
const
SmallVector
z
,
const
SmallVector
x
)
const
{
SmallVector
y
=
z
;
y
.
axpy
(
-
(
z
*
x
)
/
x
.
two_norm2
(),
x
);
return
y
;
}
};
template
<
class
Functional
>
void
minimise
(
const
Functional
J
,
const
typename
Functional
::
SmallVector
x
,
typename
Functional
::
SmallVector
&
corr
)
{
typedef
typename
Functional
::
SmallVector
SmallVector
;
SmallVector
descDir
=
J
.
descentDirection
(
x
);
if
(
descDir
==
SmallVector
(
0.0
))
{
corr
=
SmallVector
(
0.0
);
return
;
}
// {{{ Construct a restriction of J to the line x + t * descDir
/* We have
1/2 <A(u+xv),u+xv>-<b,u+xv> = 1/2 <Av,v> x^2 - <b-Au,v> x + <1/2 Au-b,u>
since A is symmetric.
*/
SmallVector
tmp
;
J
.
A
.
mv
(
descDir
,
tmp
);
// Av
double
const
JRestA
=
tmp
*
descDir
;
// <Av,v>
J
.
A
.
mv
(
x
,
tmp
);
// Au
double
const
JRestb
=
(
J
.
b
-
tmp
)
*
descDir
;
// <b-Au,v>
typedef
MyNonlinearity
<
SmallVector
::
dimension
,
typename
Functional
::
FunctionType
>
MyNonlinearityType
;
MyNonlinearityType
phi
;
typedef
DirectionalConvexFunction
<
MyNonlinearityType
>
MyDirectionalConvexFunctionType
;
MyDirectionalConvexFunctionType
JRest
(
JRestA
,
JRestb
,
phi
,
x
,
descDir
);
// }}}
// FIXME: default values are used
Bisection
bisection
;
int
count
;
// FIXME: does x_old = 1 make any sense?!
double
const
stepsize
=
bisection
.
minimize
(
JRest
,
0.0
,
1.0
,
count
);
Dune
::
dverb
<<
"Number of iterations in the bisection method: "
<<
count
<<
std
::
endl
;
;
corr
=
descDir
;
corr
*=
stepsize
;
}
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment