Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
teqp_fork_old
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Sven Michael Pohl
teqp_fork_old
Commits
bbb33836
Commit
bbb33836
authored
4 years ago
by
Ian Bell
Browse files
Options
Downloads
Patches
Plain Diff
More generic derivative functions
parent
7cecb455
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/teqp/derivs.hpp
+8
-16
8 additions, 16 deletions
include/teqp/derivs.hpp
src/main.cpp
+6
-6
6 additions, 6 deletions
src/main.cpp
with
14 additions
and
22 deletions
include/teqp/derivs.hpp
+
8
−
16
View file @
bbb33836
...
...
@@ -6,37 +6,29 @@ caller(const FuncType& f, TType T, const ContainerType& rho) {
return
f
(
T
,
rho
);
}
/// Given a
/// Given a function, use complex step derivatives to calculate the derivative with respect to the first variable
/// which here is temperature
template
<
typename
TType
,
typename
ContainerType
,
typename
FuncType
>
typename
std
::
enable_if
<
is_container
<
ContainerType
>::
value
,
typename
ContainerType
::
value_type
>::
type
deriv
1
(
const
FuncType
&
f
,
TType
T
,
const
ContainerType
&
rho
)
{
deriv
T
(
const
FuncType
&
f
,
TType
T
,
const
ContainerType
&
rho
)
{
double
h
=
1e-100
;
return
f
(
std
::
complex
<
TType
>
(
T
,
h
),
rho
).
imag
()
/
h
;
}
template
<
typename
TType
,
typename
ContainerType
,
typename
FuncType
>
/// Given a function, use complex step derivatives to calculate the derivative with respect to the first composition variable
template
<
typename
TType
,
typename
ContainerType
,
typename
FuncType
,
typename
Integer
>
typename
std
::
enable_if
<
is_container
<
ContainerType
>::
value
,
typename
ContainerType
::
value_type
>::
type
deriv
2
(
const
FuncType
&
f
,
TType
T
,
const
ContainerType
&
rho
)
{
deriv
rhoi
(
const
FuncType
&
f
,
TType
T
,
const
ContainerType
&
rho
,
Integer
j
)
{
double
h
=
1e-100
;
using
comtype
=
std
::
complex
<
ContainerType
::
value_type
>
;
std
::
valarray
<
comtype
>
rhocom
(
rho
.
size
());
rhocom
[
0
]
=
comtype
(
rho
[
0
],
h
);
for
(
auto
i
=
1
;
i
<
rho
.
size
();
++
i
)
{
for
(
auto
i
=
0
;
i
<
rho
.
size
();
++
i
)
{
rhocom
[
i
]
=
comtype
(
rho
[
i
],
0.0
);
}
rhocom
[
j
]
=
comtype
(
rho
[
j
],
h
);
return
f
(
T
,
rhocom
).
imag
()
/
h
;
}
template
<
typename
TType
,
typename
ContainerType
,
typename
FuncType
>
typename
std
::
enable_if
<
is_container
<
ContainerType
>::
value
,
typename
ContainerType
::
value_type
>::
type
deriv3
(
const
FuncType
&
f
,
TType
T
,
const
ContainerType
&
rho
)
{
double
h
=
1e-100
;
using
comtype
=
std
::
complex
<
ContainerType
::
value_type
>
;
std
::
valarray
<
comtype
>
rhocom
(
rho
.
size
());
rhocom
[
0
]
=
comtype
(
rho
[
0
],
0.0
);
rhocom
[
1
]
=
comtype
(
rho
[
1
],
h
);
return
f
(
T
,
rhocom
).
imag
()
/
h
;
}
template
<
typename
Model
,
typename
TType
,
typename
RhoType
>
auto
build_Psir_Hessian
(
const
Model
&
model
,
const
TType
T
,
const
RhoType
&
rho
)
{
...
...
This diff is collapsed.
Click to expand it.
src/main.cpp
+
6
−
6
View file @
bbb33836
...
...
@@ -33,8 +33,8 @@ void test_vdW() {
auto
t21
=
std
::
chrono
::
steady_clock
::
now
();
auto
Psir
=
vdW
.
Psir
(
T
,
rhovec
);
auto
dPsirdrho0
=
rhovec
[
0
]
*
deriv
2
([
&
vdW
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
dPsirdrho1
=
rhovec
[
1
]
*
deriv
3
([
&
vdW
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
dPsirdrho0
=
rhovec
[
0
]
*
deriv
rhoi
([
&
vdW
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
,
0
);
auto
dPsirdrho1
=
rhovec
[
1
]
*
deriv
rhoi
([
&
vdW
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
,
1
);
auto
pfromderiv
=
rho
*
R
*
T
-
Psir
+
dPsirdrho0
+
dPsirdrho1
;
auto
t31
=
std
::
chrono
::
steady_clock
::
now
();
...
...
@@ -58,12 +58,12 @@ void test_vdwMix() {
auto
t2
=
std
::
chrono
::
steady_clock
::
now
();
auto
Psir
=
vdW
.
Psir
(
T
,
rhovec
);
auto
dPsirdrho0
=
rhovec
[
0
]
*
deriv
2
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
dPsirdrho1
=
rhovec
[
1
]
*
deriv
3
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
dPsirdrho0
=
rhovec
[
0
]
*
deriv
rhoi
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
,
0
);
auto
dPsirdrho1
=
rhovec
[
1
]
*
deriv
rhoi
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
Psir
(
T
,
rhovec
);
},
T
,
rhovec
,
1
);
auto
pfromderiv
=
rho
*
R
*
T
-
Psir
+
dPsirdrho0
+
dPsirdrho1
;
{
auto
term0
=
rhovec
[
0
]
*
deriv
2
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
alphar
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
term1
=
rhovec
[
1
]
*
deriv
3
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
alphar
(
T
,
rhovec
);
},
T
,
rhovec
);
auto
term0
=
rhovec
[
0
]
*
deriv
rhoi
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
alphar
(
T
,
rhovec
);
},
T
,
rhovec
,
0
);
auto
term1
=
rhovec
[
1
]
*
deriv
rhoi
([
&
vdW
,
rhotot
](
const
auto
&
T
,
const
auto
&
rhovec
)
{
return
vdW
.
alphar
(
T
,
rhovec
);
},
T
,
rhovec
,
1
);
auto
pr
=
(
term0
+
term1
)
*
rhotot
*
R
*
T
;
auto
pfromderiv2
=
rho
*
R
*
T
+
pr
;
auto
err2
=
pfromderiv
/
pfromderiv2
-
1
;
...
...
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