From 6baa0e0d76e839adecbfbcaa6cdcecbc47f920a1 Mon Sep 17 00:00:00 2001 From: Ryker Fish Date: Tue, 24 Mar 2026 16:04:09 -0600 Subject: [PATCH 1/2] fix: remove old cpp examples, update python example to new API, and add example run to CI --- .github/workflows/ci.yml | 15 ++--- examples/{python => }/README.md | 0 examples/cpp/CMakeLists.txt | 41 ----------- examples/cpp/README.md | 20 ------ examples/cpp/example.cu | 98 --------------------------- examples/cpp/example_gpu.cu | 112 ------------------------------- examples/{python => }/example.py | 2 +- 7 files changed, 8 insertions(+), 280 deletions(-) rename examples/{python => }/README.md (100%) delete mode 100644 examples/cpp/CMakeLists.txt delete mode 100644 examples/cpp/README.md delete mode 100644 examples/cpp/example.cu delete mode 100644 examples/cpp/example_gpu.cu rename examples/{python => }/example.py (98%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4d54113e..ce04971d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -54,13 +54,6 @@ jobs: make html shell: micromamba-shell {0} - - name: Compile examples - run: | - mkdir examples/cpp/build && cd examples/cpp/build - cmake -DCMAKE_VERBOSE_MAKEFILE=yes -DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX .. - make -j3 - shell: micromamba-shell {0} - - name: Try to import libMobility run: | python -c "import libMobility" @@ -73,7 +66,13 @@ jobs: #Only SelfMobility tests can run in the CPU pytest -sv -k SelfMobility tests/test*py shell: micromamba-shell {0} - + + - name: Run examples + run: | + cd examples/ + python example.py + shell: micromamba-shell {0} + - name: Install via pip run: | pip install -vv . diff --git a/examples/python/README.md b/examples/README.md similarity index 100% rename from examples/python/README.md rename to examples/README.md diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt deleted file mode 100644 index bafbdf24..00000000 --- a/examples/cpp/CMakeLists.txt +++ /dev/null @@ -1,41 +0,0 @@ -cmake_minimum_required(VERSION 3.22) -project(libMobility_example) -enable_language(CUDA) -# add_compile_definitions(PUBLIC CUDA_ENABLED) # uncommenting this makes the code not compile- I don't know why -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") -set(CMAKE_BUILD_TYPE Release) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CUDA_STANDARD 20) -set(CMAKE_CUDA_STANDARD_REQUIRED ON) -set(CMAKE_CUDA_SEPARABLE_COMPILATION OFF) - -set(ROOT_DIR ../../) -# enable the line below if libmobility is compiled in double -# add_compile_definitions(PUBLIC DOUBLE_PRECISION) -include(FetchContent) -FetchContent_Declare( - uammd - GIT_REPOSITORY https://github.com/RaulPPelaez/uammd/ - GIT_TAG v2.8.5 -) -FetchContent_MakeAvailable(uammd) -FetchContent_Declare( - lanczos - GIT_REPOSITORY https://github.com/RaulPPelaez/LanczosAlgorithm - GIT_TAG v1.0.1 -) -FetchContent_MakeAvailable(lanczos) -include_directories(${lanczos_SOURCE_DIR}/include) - -find_package(BLAS REQUIRED) -find_package(LAPACK REQUIRED) - -include_directories(${ROOT_DIR}/include) -include_directories(${ROOT_DIR}) -link_libraries(libMobility_PSE libMobility_NBody) - -add_executable(example example.cu) -add_executable(example_cuda example_gpu.cu) -uammd_setup_target(example) -uammd_setup_target(example_cuda) diff --git a/examples/cpp/README.md b/examples/cpp/README.md deleted file mode 100644 index 5fbd34b5..00000000 --- a/examples/cpp/README.md +++ /dev/null @@ -1,20 +0,0 @@ -## Usage example for the libMobility C++ interface - -The code example.cpp showcases how a solver can be included and used with libMobility. - - -### Compilation - -After [installing libMobility](https://libmobility.readthedocs.io) you can use CMake to compile this file: - -```bash - mkdir build - cd build - cmake .. - make -``` - - -### Running -A valid CUDA environment and a GPU are needed to run the example. -After succesfully running, the mobilities of a group of randomly placed particles (with random forces acting on them) will be computed via an open boundary solver (NBody) and a triply periodic one (PSE) with a large box size. The displacements obtained by both methods are then printed. diff --git a/examples/cpp/example.cu b/examples/cpp/example.cu deleted file mode 100644 index a7cb604c..00000000 --- a/examples/cpp/example.cu +++ /dev/null @@ -1,98 +0,0 @@ -/*Raul P. Pelaez 2021-2025. Example usage of the NBody mobility solver. - All available solvers are used in a similar way, providing, in each case, the required parameters. - For instance, a triply periodic algorithm will need at least a box size. - */ -#include"MobilityInterface/MobilityInterface.h" -#include"solvers/NBody/mobility.h" -#include"solvers/PSE/mobility.h" -#include -#include -#include -#include -#include -using namespace std; - -using scalar = libmobility::real; -using MobilityBase = libmobility::Mobility; -using Configuration = libmobility::Configuration; -using libmobility::Parameters; -//Configures, initializes any solver (between PSE and NBody) -//The same function can be extended to create any solver. -//We need it to desambiguate by calling the solver-dependent setParameters function when necessary. For instance, see PSE below -template -auto initializeSolver(Parameters par, int numberParticles){ - std::shared_ptr solver; - if(std::is_same::value){ - auto nbody = std::make_shared(Configuration{.periodicityX = libmobility::periodicity_mode::open, - .periodicityY = libmobility::periodicity_mode::open, - .periodicityZ = libmobility::periodicity_mode::open}); - nbody->setParametersNBody({nbody_rpy::algorithm::advise, 1, numberParticles}); - solver = nbody; - } - if(std::is_same::value){ - auto pse = std::make_shared(Configuration{.periodicityX = libmobility::periodicity_mode::periodic, - .periodicityY = libmobility::periodicity_mode::periodic, - .periodicityZ = libmobility::periodicity_mode::periodic}); - scalar lx,ly,lz; - lx=ly=lz=128; - scalar split = 1.0; - scalar shearStrain = 0.0; - pse->setParametersPSE({split, lx,ly,lz, shearStrain}); - solver = pse; - } - solver->initialize(par); - return solver; -} - -//An example of a function that works for any solver -auto computeMFWithSolver(std::shared_ptr solver, - std::vector &ipos, - std::vector &iforces){ - std::vector result(ipos.size(), 0); - solver->setPositions(ipos); - solver->Mdot(iforces, {}, result, {}); - return result; -} - -//Lets compute the deterministic and stochastic displacements of a group of particles -int main(){ - - //Create some arbitrary positions and forces - int numberParticles = 10; - std::vector pos(3*numberParticles); - auto forces = pos; - mt19937 mersenne_engine {1234}; - uniform_real_distribution dist {-10, 10}; - std::generate(pos.begin(), pos.end(),[&](){return dist(mersenne_engine);}); - std::generate(forces.begin(), forces.end(),[&](){return dist(mersenne_engine);}); - - //Set up parameters generic to any solver - Parameters par; - par.hydrodynamicRadius = {1}; - par.viscosity = 1; - par.tolerance = 1e-4; - par.includeAngular = false; - - //Create two different solvers - auto solver_pse = initializeSolver(par, numberParticles); - auto solver_nbody = initializeSolver(par, numberParticles); - - //Compute the displacements - auto resultNBody = computeMFWithSolver(solver_nbody, pos, forces); - auto resultPSE = computeMFWithSolver(solver_pse, pos, forces); - - //The solvers can be used to compute stochastic displacements, even if they do not provide a specific way to compute them (defaults to using the lanczos algorithm - std::vector noiseNBody(pos.size(), 0); - scalar prefactor = 1.0; - solver_nbody->sqrtMdotW(noiseNBody, {}, prefactor); - - //Remember to clean up when done - solver_nbody->clean(); - solver_pse->clean(); - - std::cout<<"NBody\tPSE\n"; - for(int i = 0; i -#include -#include -#include -#include -#include -using namespace std; - -using scalar = libmobility::real; -using MobilityBase = libmobility::Mobility; -using Configuration = libmobility::Configuration; -using libmobility::Parameters; -// Configures, initializes any solver (between PSE and NBody) -// The same function can be extended to create any solver. -// We need it to desambiguate by calling the solver-dependent setParameters -// function when necessary. For instance, see PSE below -template auto initializeSolver(Parameters par, int numberParticles) { - std::shared_ptr solver; - if (std::is_same::value) { - auto nbody = std::make_shared( - Configuration{.periodicityX = libmobility::periodicity_mode::open, - .periodicityY = libmobility::periodicity_mode::open, - .periodicityZ = libmobility::periodicity_mode::open}); - nbody->setParametersNBody( - {nbody_rpy::algorithm::advise, 1, numberParticles}); - solver = nbody; - } - if (std::is_same::value) { - auto pse = std::make_shared( - Configuration{.periodicityX = libmobility::periodicity_mode::periodic, - .periodicityY = libmobility::periodicity_mode::periodic, - .periodicityZ = libmobility::periodicity_mode::periodic}); - scalar lx, ly, lz; - lx = ly = lz = 128; - scalar split = 1.0; - scalar shearStrain = 0.0; - pse->setParametersPSE({split, lx, ly, lz, shearStrain}); - solver = pse; - } - solver->initialize(par); - return solver; -} - -// An example of a function that works for any solver -auto computeMFWithSolver(std::shared_ptr solver, - thrust::device_vector &ipos, - thrust::device_vector &iforces) { - thrust::device_vector result(ipos.size(), 0); - solver->setPositions(ipos); - solver->Mdot(iforces, {}, result, {}); - return result; -} - -// Lets compute the deterministic and stochastic displacements of a group of -// particles -int main() { - { - // Create some arbitrary positions and forces - int numberParticles = 10; - std::vector h_pos(3 * numberParticles); - auto h_forces = h_pos; - mt19937 mersenne_engine{1234}; - uniform_real_distribution dist{-10, 10}; - std::generate(h_pos.begin(), h_pos.end(), - [&]() { return dist(mersenne_engine); }); - std::generate(h_forces.begin(), h_forces.end(), - [&]() { return dist(mersenne_engine); }); - - thrust::device_vector pos = h_pos; - thrust::device_vector forces = h_forces; - // Set up parameters generic to any solver - Parameters par; - par.hydrodynamicRadius = {1}; - par.viscosity = 1; - par.tolerance = 1e-4; - par.includeAngular = false; - - // Create two different solvers - auto solver_pse = initializeSolver(par, numberParticles); - auto solver_nbody = initializeSolver(par, numberParticles); - - // Compute the displacements - auto resultNBody = computeMFWithSolver(solver_nbody, pos, forces); - auto resultPSE = computeMFWithSolver(solver_pse, pos, forces); - - // The solvers can be used to compute stochastic displacements, even if they - // do not provide a specific way to compute them (defaults to using the - // lanczos algorithm - thrust::device_vector noiseNBody(pos.size(), 0); - scalar prefactor = 1.0; - solver_nbody->sqrtMdotW(noiseNBody, {}, prefactor); - - // Remember to clean up when done - solver_nbody->clean(); - solver_pse->clean(); - - std::cout << "NBody\tPSE\n"; - for (int i = 0; i < resultNBody.size(); i++) { - std::cout << resultNBody[i] << "\t" << resultPSE[i] << std::endl; - } - } - return 0; -} diff --git a/examples/python/example.py b/examples/example.py similarity index 98% rename from examples/python/example.py rename to examples/example.py index 5e6e7e6f..b90a2c2a 100644 --- a/examples/python/example.py +++ b/examples/example.py @@ -56,6 +56,6 @@ # Some solvers (e.g. SelfMobility) have no thermal drift and return all zeros. # In general, the thermal drift is non-zero and other solvers (e.g. DPStokes) return a non-zero value. -linear_drift, _ = solver.thermalDrift() +linear_drift, _ = solver.divM() print("Thermal drift:", linear_drift) solver.clean() From 2273b6669a9cd4c057e19c9ebbcd46debc2a169b Mon Sep 17 00:00:00 2001 From: Ryker Fish Date: Fri, 27 Mar 2026 12:11:51 -0600 Subject: [PATCH 2/2] build: restrict env to cuda <=13.1 as UAMMD is failing to build on the cccl versions associated with cuda 13.2 --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index 8f47f086..cf2d0e1c 100644 --- a/environment.yml +++ b/environment.yml @@ -5,7 +5,7 @@ channels: dependencies: - python <3.13 - cmake >=3.24 - - cuda-version + - cuda-version <=13.1 - gxx >9,<15 - cuda-libraries-dev - cuda-nvcc