Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-elasticity
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Ansgar Burchardt
dune-elasticity
Commits
49be3cc9
Commit
49be3cc9
authored
19 years ago
by
Oliver Sander
Committed by
sander
19 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add the fd check for the first derivative
[[Imported from SVN: r9278]]
parent
56ee4cb2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/finitedifferencecheck.hh
+87
-0
87 additions, 0 deletions
test/finitedifferencecheck.hh
test/neohooketest.cc
+11
-1
11 additions, 1 deletion
test/neohooketest.cc
with
98 additions
and
1 deletion
test/finitedifferencecheck.hh
0 → 100644
+
87
−
0
View file @
49be3cc9
#ifndef FINITE_DIFFERENCE_CHECK_HH
#define FINITE_DIFFERENCE_CHECK_HH
template
<
class
AssemblerType
,
class
VectorType
>
void
finiteDifferenceCheck
(
AssemblerType
&
assembler
,
const
VectorType
&
x
)
{
VectorType
rhs
(
x
.
size
());
rhs
=
0
;
double
eps
=
1e-6
;
const
int
blocksize
=
VectorType
::
block_type
::
size
;
// ///////////////////////////////////////////////////////////
// Assemble gradient and hessian at the current position
// ///////////////////////////////////////////////////////////
assembler
.
assembleMatrix
(
x
,
rhs
);
// ///////////////////////////////////////////////////////////
// Compare rhs with a finite-difference approximation
// ///////////////////////////////////////////////////////////
for
(
int
i
=
0
;
i
<
rhs
.
size
();
i
++
)
{
for
(
int
j
=
0
;
j
<
blocksize
;
j
++
)
{
VectorType
value0
=
x
;
VectorType
value1
=
x
;
value0
[
i
][
j
]
-=
eps
;
value1
[
i
][
j
]
+=
eps
;
double
fd
=
(
assembler
.
computeEnergy
(
value1
)
-
assembler
.
computeEnergy
(
value0
))
/
(
2
*
eps
);
// relative error. NOTE: assembleMatrix currently assembles the NEGATIVE gradient!
double
error
=
(
-
rhs
[
i
][
j
]
-
fd
)
/
(
-
rhs
[
i
][
j
]
+
fd
);
if
(
std
::
fabs
(
-
rhs
[
i
][
j
]
-
fd
)
>
1
)
{
std
::
cout
<<
"ERROR! gradient: "
<<
rhs
[
i
][
j
]
<<
", fd: "
<<
fd
<<
std
::
endl
;
exit
(
1
);
}
//std::cout << "gradient: " << rhs[i][j] << ", fd: " << fd << std::endl;
}
}
//exit(0);
// ///////////////////////////////////////////////////////////
// Compare hessian with a finite-difference approximation
// ///////////////////////////////////////////////////////////
typedef
Dune
::
BCRSMatrix
<
Dune
::
FieldMatrix
<
double
,
blocksize
,
blocksize
>
>
MatrixType
;
const
MatrixType
&
M
=
*
assembler
.
getMatrix
();
for
(
int
i
=
0
;
i
<
M
.
N
();
i
++
)
{
typename
MatrixType
::
row_type
::
ConstIterator
cIt
=
M
[
i
].
begin
();
typename
MatrixType
::
row_type
::
ConstIterator
cEndIt
=
M
[
i
].
end
();
for
(;
cIt
!=
cEndIt
;
++
cIt
)
{
// Loop over all entries of the matrix block
for
(
int
j
=
0
;
j
<
blocksize
;
j
++
)
{
for
(
int
k
=
0
;
k
<
blocksize
;
k
++
)
{
// Is the matrix symmetric?
if
(
std
::
abs
((
*
cIt
)[
j
][
k
]
-
M
[
cIt
.
index
()][
i
][
k
][
j
])
>
1e-6
)
{
std
::
cout
<<
"Matrix asymmetry: "
<<
(
*
cIt
)[
j
][
k
]
<<
" -- "
<<
M
[
cIt
.
index
()][
i
][
k
][
j
]
<<
std
::
endl
;
//exit(1);
}
//assert( std::abs((*cIt)[j][k] - M[cIt.index()][i][k][j]) < 1e-6);
}
}
}
}
}
#endif
This diff is collapsed.
Click to expand it.
test/neohooketest.cc
+
11
−
1
View file @
49be3cc9
...
...
@@ -6,6 +6,7 @@
#include
"makesymmgrids.hh"
#include
"checksymmetry.hh"
#include
"finitedifferencecheck.hh"
using
namespace
Dune
;
...
...
@@ -33,7 +34,16 @@ int main() try
// Check rhs for symmetry
checkSymmetry
(
rhs
,
perm_tire2d
);
grid
.
globalRefine
(
2
);
x
.
resize
(
grid
.
size
(
grid
.
maxLevel
(),
2
));
x
=
0
;
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
x
[
i
%
x
.
size
()][
i
%
2
]
+=
i
;
// Compare derivatives with a finite-difference approximation
finiteDifferenceCheck
(
assembler
,
x
);
}
return
0
;
}
catch
(
Exception
e
)
{
...
...
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