Last active
July 29, 2021 06:02
-
-
Save balos1/fe0f361139d03dad32673b11fc308961 to your computer and use it in GitHub Desktop.
SUNDIALS Github Issue #66: https://github.com/LLNL/sundials/issues/66
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
BSD 3-Clause License | |
Copyright (c) 2002-2021, Lawrence Livermore National Security and Southern Methodist University. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of the copyright holder nor the names of its | |
contributors may be used to endorse or promote products derived from | |
this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#include <chrono> | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <nvector/nvector_serial.h> | |
struct lotka_volterra { | |
static constexpr double rtol=1.e-12; | |
static constexpr double atol=1.e-12; | |
static constexpr int max_num_steps=1000000; | |
const std::vector<double>& theta; | |
long int n_eval; | |
lotka_volterra(const std::vector<double>& theta0) : | |
theta(theta0), n_eval(0) | |
{} | |
void print_n_eval(std::string info) { | |
std::cout << "# of RHS evals ( " << info << " ) :" << n_eval << "\n"; | |
} | |
void operator()(double t_in, N_Vector& y, N_Vector& ydot) { | |
n_eval++; | |
double alpha = theta[0]; | |
double beta = theta[1]; | |
double gamma = theta[2]; | |
double delta = theta[3]; | |
NV_Ith_S(ydot, 0) = (alpha - beta * NV_Ith_S(y, 1)) * NV_Ith_S(y, 0); | |
NV_Ith_S(ydot, 1) = (-gamma + delta * NV_Ith_S(y, 0)) * NV_Ith_S(y, 1); | |
/* double *ydata = NV_DATA_S(y); */ | |
/* double *yddata = NV_DATA_S(ydot); */ | |
/* yddata[0] = (alpha - beta * ydata[1]) * ydata[0]; */ | |
/* yddata[1] = (-gamma + delta * ydata[0]) * ydata[1]; */ | |
} | |
void operator()(const std::vector<double>& y, std::vector<double>& ydot, double t) { | |
n_eval++; | |
double alpha = theta[0]; | |
double beta = theta[1]; | |
double gamma = theta[2]; | |
double delta = theta[3]; | |
ydot[0] = (alpha - beta * y[1]) * y[0]; | |
ydot[1] = (-gamma + delta * y[0]) * y[1]; | |
} | |
}; | |
void test_boost() | |
{ | |
int n = 2; | |
std::vector<double> theta{1.5, 1.05, 1.5, 2.05}; | |
std::vector<double> y0{0.3, 0.8}; | |
lotka_volterra ode(theta); | |
std::vector<double> ydot(n); | |
const long int n_eval = 99999999; | |
std::chrono::time_point<std::chrono::system_clock> start, end; | |
std::chrono::duration<double, std::milli> elapsed; | |
start = std::chrono::system_clock::now(); | |
for (long int i = 0; i < n_eval; ++i) { | |
ode(y0, ydot, 0.1); | |
y0[0] = ydot[0]; | |
y0[1] = ydot[1]; | |
} | |
end = std::chrono::system_clock::now(); | |
elapsed = (end - start); | |
std::cout << "odeint RHS elapsed time: " << elapsed.count() << " ms\n"; | |
std::cout << "odeint RHS count: " << ode.n_eval << "\n"; | |
} | |
void test_arkode() | |
{ | |
int n = 2; | |
std::vector<double> theta{1.5, 1.05, 1.5, 2.05}; | |
std::vector<double> y0{0.3, 0.8}; | |
std::vector<double> ydot(n); | |
lotka_volterra ode(theta); | |
N_Vector nvy0 = N_VMake_Serial(2, y0.data()); | |
N_Vector nvydot = N_VMake_Serial(2, ydot.data()); | |
const long int n_eval = 99999999; | |
std::chrono::time_point<std::chrono::system_clock> start, end; | |
std::chrono::duration<double, std::milli> elapsed; | |
start = std::chrono::system_clock::now(); | |
for (long int i = 0; i < n_eval; ++i) { | |
ode(0.1, nvy0, nvydot); | |
y0[0] = ydot[0]; | |
y0[1] = ydot[1]; | |
} | |
end = std::chrono::system_clock::now(); | |
elapsed = (end - start); | |
std::cout << "arkode RHS elapsed time: " << elapsed.count() << " ms\n"; | |
std::cout << "arkode RHS count: " << ode.n_eval << "\n"; | |
} | |
int main() | |
{ | |
test_boost(); | |
test_arkode(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ c++ --version | |
Apple clang version 12.0.0 (clang-1200.0.32.27) | |
Target: x86_64-apple-darwin19.6.0 | |
Thread model: posix | |
InstalledDir: /Library/Developer/CommandLineTools/usr/bin | |
$ c++ -std=c++11 -O3 -Isundials-master/instdir/include compare_rhs.cpp -o compare_rhs.03 -L sundials-master/instdir/lib/ -lsundials_arkode |
Built sundials 5.7.0 with the commands:
$ git clone https://github.com/LLNL/sundials.git sundials-master
$ cd sundials-master
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=$(which cc) -DCMAKE_INSTALL_PREFIX=../instdir -DEXAMPLES_INSTALL=OFF ../
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: