Skip to content
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

Merged
merged 7 commits into from Apr 14, 2017

Conversation

MFraters
Copy link
Member

@MFraters MFraters commented Mar 7, 2017

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.

Copy link
Member

@gassmoeller gassmoeller left a 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.

{
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();
Copy link
Member

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?

{
const double dx=x[i+1]-x[i];
const double dy=y[i+1]-y[i];
dxs.push_back(dx);
Copy link
Member

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.

}

// get m_a parameter
m_c.resize(1);
Copy link
Member

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)

Copy link
Member

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.


const double invDx = 1/dxs[i];
const double common0 = c1 + m_c[i+1] - m0 - m0;
m_b.push_back((m0 - c1 - common0) * invDx);
Copy link
Member

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

// Get b and c coefficients
for (unsigned int i = 0; i < m_c.size()-1; i++)
{
const double c1=m_c[i];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please correct spaces

@MFraters
Copy link
Member Author

@gassmoeller I hope this adresses all your comments, or do you want me to restyle the whole spline code?
I will try do design a good test later.

@MFraters
Copy link
Member Author

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.

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "monotone"

{
dxs[i] = x[i+1]-x[i];
dys[i] = y[i+1]-y[i];
ms[i] = double(dys[i])/double(dxs[i]);
Copy link
Member

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

@gassmoeller
Copy link
Member

Thanks! This looks ready to merge except for the two minor things Timo mentioned. Lets see what the tester says.

@gassmoeller
Copy link
Member

/run-tests

@MFraters
Copy link
Member Author

MFraters commented Apr 4, 2017

Any other comments? :)

@@ -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);
Copy link
Contributor

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?

Copy link
Contributor

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.

/**
* 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
Copy link
Contributor

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spine -> spline

@gassmoeller gassmoeller merged commit 26e088d into geodynamics:master Apr 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants