New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add monotone spline options to utilties. #1403
Conversation
1d5a802
to
6ad9c89
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general a nice functionality. I have some stylistic and performance comments (it looks like this function is called a lot in the S20RTS and Savani initial conditions). Also please add a test for the new functionality (dont know if you already have a model that could be used, but otherwise you could create a simple initial condition plugin that only assigns the depth-dependent temperature of an arbitrarily chosen spline).
Additionally, it looks like the code style regarding spaces of this class is very different from the rest of ASPECT. Would you mind going over the code once and unifying this? In particular adding spaces around equal signs and at reasonable places in equations.
source/utilities.cc
Outdated
{ | ||
assert(x.size()==y.size()); | ||
m_x=x; | ||
m_y=y; | ||
int n=x.size(); | ||
for (int i=0; i<n-1; i++) | ||
unsigned int n=x.size(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for improving this 👍. This can also be const
, and please remove the useless spaces between int and n. Also is it possible to rename n to something useful? Is it n_knots
or something else?
source/utilities.cc
Outdated
{ | ||
const double dx=x[i+1]-x[i]; | ||
const double dy=y[i+1]-y[i]; | ||
dxs.push_back(dx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please modify this so that dys
,dxs
, and ms
to already have the correct size (n-1) upon creation. push_back
can be slow, because the whole vector needs to copied and new memory needs to be allocated.
source/utilities.cc
Outdated
} | ||
|
||
// get m_a parameter | ||
m_c.resize(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, m_c
can be once resized to m_c.resize(dxs.size()+1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upon taking another look, is dxs.size()+1
equivalent to n
? That would be even simpler.
source/utilities.cc
Outdated
|
||
const double invDx = 1/dxs[i]; | ||
const double common0 = c1 + m_c[i+1] - m0 - m0; | ||
m_b.push_back((m0 - c1 - common0) * invDx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. You can resize m_b
and m_a
once before the loop to a size of n
source/utilities.cc
Outdated
// Get b and c coefficients | ||
for (unsigned int i = 0; i < m_c.size()-1; i++) | ||
{ | ||
const double c1=m_c[i]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please correct spaces
@gassmoeller I hope this adresses all your comments, or do you want me to restyle the whole spline code? |
I have now changed the spacing in the whole class anyway. Is this the kind of spacing you where looking for? I have also added a test to test this whole spline class. |
d07c967
to
13f9768
Compare
include/aspect/utilities.h
Outdated
@@ -280,7 +280,8 @@ namespace aspect | |||
*/ | |||
void set_points(const std::vector<double> &x, | |||
const std::vector<double> &y, | |||
bool cubic_spline = true); | |||
bool cubic_spline = true, | |||
bool monotome_spline = false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "monotone"
source/utilities.cc
Outdated
{ | ||
dxs[i] = x[i+1]-x[i]; | ||
dys[i] = y[i+1]-y[i]; | ||
ms[i] = double(dys[i])/double(dxs[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are doubles already, so remove the cast
Thanks! This looks ready to merge except for the two minor things Timo mentioned. Lets see what the tester says. |
/run-tests |
Any other comments? :) |
include/aspect/utilities.h
Outdated
@@ -280,7 +280,8 @@ namespace aspect | |||
*/ | |||
void set_points(const std::vector<double> &x, | |||
const std::vector<double> &y, | |||
bool cubic_spline = true); | |||
bool cubic_spline = true, | |||
bool monotone_spline = false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document this additional argument? Also maybe in the class documentation?
It's not clear to me whether you can have both cubic and monotone splines. Is this possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, also mark both of the bool
arguments as const
.
source/utilities.cc
Outdated
/** | ||
* This monotone spline algorithm is based on the javascript version | ||
* at https://en.wikipedia.org/wiki/Monotone_cubic_interpolation. The | ||
* parameters from this algorithm prevents overshooting in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prevents -> prevent
tests/splines.cc
Outdated
@@ -0,0 +1,55 @@ | |||
/** | |||
* This tests the Utilities::tk::spine class for the linear case, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spine -> spline
This add an algorithm which can produce monotone splines. The code was based on the javascript implementation on https://en.wikipedia.org/wiki/Monotone_cubic_interpolation.