Skip to content

Commit

Permalink
init c++
Browse files Browse the repository at this point in the history
  • Loading branch information
walchko committed Aug 25, 2023
1 parent f2790c7 commit be9c15f
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 80 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- name: Install packages
run: |
echo "Installing dependencies"
cd python
python3 -m venv .venv
source .venv/bin/activate
pip install -U pip setuptools wheel poetry pytest
Expand Down
61 changes: 61 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.25)
PROJECT(rk45 VERSION "2023.08.24.0")

include(FetchContent)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(PROJECT_IS_TOP_LEVEL)
cmake_host_system_information(RESULT HOST QUERY HOSTNAME)
# cmake_host_system_information(RESULT CORES QUERY NUMBER_OF_LOGICAL_CORES)
cmake_host_system_information(RESULT OSN QUERY OS_NAME)
cmake_host_system_information(RESULT OS_VERSION QUERY OS_RELEASE)
cmake_host_system_information(RESULT PROC QUERY PROCESSOR_DESCRIPTION)

message(STATUS "-------------------------------------")
message(STATUS " Project: ${PROJECT_NAME}")
message(STATUS " C++ ${CMAKE_CXX_STANDARD}")
message(STATUS "-------------------------------------")
message(STATUS " ${HOST}")
message(STATUS " ${OSN}: ${OS_VERSION}")
message(STATUS " ${PROC}")
message(STATUS "-------------------------------------")

set(BUILD_EXAMPLES ON)
set(BUILD_GTESTS ON)

# GTest -----------------
FetchContent_Declare(gtest
# GIT_REPOSITORY "https://github.com/google/googletest"
# GIT_TAG "origin/main"
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
list(APPEND libs gtest)
else()
message(STATUS "-> ${PROJECT_NAME} is submodule")
set(BUILD_EXAMPLES OFF)
set(BUILD_GTESTS OFF)
endif()

# if other fetched content, append them to libs too
# list(APPEND libs)

FetchContent_MakeAvailable( ${libs} )

add_library(${PROJECT_NAME} INTERFACE)
target_include_directories(${PROJECT_NAME} INTERFACE src/)
# target_link_libraries(${PROJECT_NAME} INTERFACE library1 library2 ... ) # don't add gtest here

# Examples -----------------------------------------------------------
message(STATUS "Building ${PROJECT_NAME} examples is ${BUILD_EXAMPLES}")
if(${BUILD_EXAMPLES})
add_subdirectory(examples)
endif()

# Tests --------------------------------------------------------------
message(STATUS "Building ${PROJECT_NAME} gtests is ${BUILD_GTESTS}")
if(${BUILD_GTESTS})
add_subdirectory(gtests)
endif()
9 changes: 9 additions & 0 deletions cpp/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
list(APPEND examples
main
)

# message(STATUS "Examples ----------------------")
foreach(app ${examples})
add_executable(${app} ${app}.cpp)
target_link_libraries(${app} ${PROJECT_NAME})
endforeach()
5 changes: 5 additions & 0 deletions cpp/examples/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


int main() {
return 0;
}
12 changes: 12 additions & 0 deletions cpp/gtests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

enable_testing()

list(APPEND gtests
main.cpp
)

# run all tests
add_executable(test-all ${gtests})
target_link_libraries(test-all GTest::gtest_main ${PROJECT_NAME})
include(GoogleTest)
gtest_discover_tests(test-all)
9 changes: 9 additions & 0 deletions cpp/gtests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(gciSensors, dummy) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
24 changes: 24 additions & 0 deletions cpp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Runge Kutta

# MIT License

**Copyright (c) 2015 Kevin J. Walchko**

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions cpp/src/rk.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


#pragma once

class RK45 {
public:
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
102 changes: 102 additions & 0 deletions python/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
![Header pic](https://github.com/walchko/pyrk/raw/master/pics/math2.jpg)

# Runge-Kutta

[![Actions Status](https://github.com/walchko/pyrk/workflows/pytest/badge.svg)](https://github.com/walchko/pyrk/actions)
![PyPI - License](https://img.shields.io/pypi/l/pyrk.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrk.svg)
![PyPI - Format](https://img.shields.io/pypi/format/pyrk.svg)
![PyPI](https://img.shields.io/pypi/v/pyrk.svg)

A simple implementation of
[Runge-Kutta](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods)
for python.

## Usage

Integrates a function x_dot = f(time, x, u). See the examples in the
[docs](https://github.com/walchko/pyrk/blob/master/doc/runge-kutta.ipynb)
folder or a simple one:

``` python
from pyrk import RK4
import numpy as np
import matplotlib.pyplot as plt

def vanderpol(t, xi, u):
dx, x = xi
mu = 4.0 # damping

ddx = mu*(1-x**2)*dx-x
dx = dx

return np.array([ddx, dx])

rk = RK4(vanderpol)
t, y = rk.solve(np.array([0, 1]), .01, 200)

y1 = []
y2 = []
for v in y:
y1.append(v[0])
y2.append(v[1])

plt.plot(y1, y2)
plt.ylabel('velocity')
plt.xlabel('position')
plt.grid(True)
plt.show()
```

## Alternative

If you want to use `scipy` (which is good, but big), you can do:

```python
from scipy.integrate import solve_ivp as rk45

def func(t,x,u):
# cool differential equations
# ...
return x

t = 0
dt = 0.01
y = np.array([0,0,0]) # initial state

for _ in tqdm(range(steps)):
u = np.array([1,2,3]) # some inputs to func (i.e., control effort)

y = rk45(func, [t, t+step], y, args=(u,))

if y.success == False:
print("Oops")

y = y.y[:,-1]

# probably save t, u and y into arrays for plotting
t += step
```

# MIT License

**Copyright (c) 2015 Kevin J. Walchko**

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File renamed without changes.
82 changes: 2 additions & 80 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,4 @@
![Header pic](https://github.com/walchko/pyrk/raw/master/pics/math2.jpg)

# Runge-Kutta

[![Actions Status](https://github.com/walchko/pyrk/workflows/pytest/badge.svg)](https://github.com/walchko/pyrk/actions)
![PyPI - License](https://img.shields.io/pypi/l/pyrk.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyrk.svg)
![PyPI - Format](https://img.shields.io/pypi/format/pyrk.svg)
![PyPI](https://img.shields.io/pypi/v/pyrk.svg)

A simple implementation of
[Runge-Kutta](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods)
for python.

## Usage

Integrates a function x_dot = f(time, x, u). See the examples in the
[docs](https://github.com/walchko/pyrk/blob/master/doc/runge-kutta.ipynb)
folder or a simple one:

``` python
from pyrk import RK4
import numpy as np
import matplotlib.pyplot as plt

def vanderpol(t, xi, u):
dx, x = xi
mu = 4.0 # damping

ddx = mu*(1-x**2)*dx-x
dx = dx

return np.array([ddx, dx])

rk = RK4(vanderpol)
t, y = rk.solve(np.array([0, 1]), .01, 200)

y1 = []
y2 = []
for v in y:
y1.append(v[0])
y2.append(v[1])

plt.plot(y1, y2)
plt.ylabel('velocity')
plt.xlabel('position')
plt.grid(True)
plt.show()
```

## Alternative

If you want to use `scipy` (which is good, but big), you can do:

```python
from scipy.integrate import solve_ivp as rk45

def func(t,x,u):
# cool differential equations
# ...
return x

t = 0
dt = 0.01
y = np.array([0,0,0]) # initial state

for _ in tqdm(range(steps)):
u = np.array([1,2,3]) # some inputs to func (i.e., control effort)

y = rk45(func, [t, t+step], y, args=(u,))

if y.success == False:
print("Oops")

y = y.y[:,-1]

# probably save t, u and y into arrays for plotting
t += step
```
# Runge Kutta

# MIT License

Expand All @@ -99,4 +21,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

0 comments on commit be9c15f

Please sign in to comment.