Skip to content

Commit b26be73

Browse files
committed
2_dirichlet
1 parent c4aaa3e commit b26be73

20 files changed

Lines changed: 1310 additions & 2 deletions

simulators/1_mass_spring/include/uti.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ template <typename T>
2323
void search_dir(const DeviceBuffer<T> &grad, const DeviceTripletMatrix<T, 1> &hess, DeviceBuffer<T> &dir);
2424

2525
template <typename T>
26-
void display_vec(const DeviceBuffer<T> &vec);
26+
void display_vec(const DeviceBuffer<T> &vec);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
add_executable(2_dirichlet)
2+
3+
target_compile_options(2_dirichlet PRIVATE -g)
4+
set_target_properties(2_dirichlet PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
5+
6+
target_link_libraries(2_dirichlet PRIVATE muda cusolver cublas cusparse )
7+
8+
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
9+
10+
include_directories(include)
11+
12+
file(GLOB_RECURSE 2_dirichlet_CU_SOURCE CONFIGURE_DEPENDS "src/*.cu")
13+
target_sources(2_dirichlet PRIVATE ${2_dirichlet_CU_SOURCE})
14+
15+
file(GLOB_RECURSE 2_dirichlet_CPP_SOURCE CONFIGURE_DEPENDS "src/*.cpp")
16+
target_sources(2_dirichlet PRIVATE ${2_dirichlet_CPP_SOURCE})
17+
18+
target_link_libraries(2_dirichlet PRIVATE sfml-graphics)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <Eigen/Dense>
5+
#include "square_mesh.h"
6+
#include <muda/muda.h>
7+
#include <muda/container.h>
8+
#include <muda/ext/linear_system.h>
9+
10+
using namespace muda;
11+
12+
template <typename T, int dim>
13+
class GravityEnergy
14+
{
15+
public:
16+
GravityEnergy(int N, T m);
17+
GravityEnergy();
18+
~GravityEnergy();
19+
GravityEnergy(GravityEnergy &&rhs);
20+
GravityEnergy(const GravityEnergy &rhs);
21+
GravityEnergy &operator=(GravityEnergy &&rhs);
22+
GravityEnergy &operator=(const GravityEnergy &rhs);
23+
24+
void update_x(const DeviceBuffer<T> &x);
25+
void update_x_tilde(const DeviceBuffer<T> &x_tilde);
26+
void update_m(T m);
27+
T val(); // Calculate the value of the energy
28+
const DeviceBuffer<T> &grad(); // Calculate the gradient of the energy
29+
30+
private:
31+
// The implementation details of the VecAdder class are placed in the implementation class declared here.
32+
struct Impl;
33+
// The private pointer to the implementation class Impl
34+
std::unique_ptr<Impl> pimpl_;
35+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <Eigen/Dense>
5+
#include "square_mesh.h"
6+
#include <muda/muda.h>
7+
#include <muda/container.h>
8+
#include <muda/ext/linear_system.h>
9+
10+
using namespace muda;
11+
12+
template <typename T, int dim>
13+
class InertialEnergy
14+
{
15+
public:
16+
InertialEnergy(int N, T m);
17+
InertialEnergy();
18+
~InertialEnergy();
19+
InertialEnergy(InertialEnergy &&rhs);
20+
InertialEnergy(const InertialEnergy &rhs);
21+
InertialEnergy &operator=(InertialEnergy &&rhs);
22+
23+
void update_x(const DeviceBuffer<T> &x);
24+
void generate_hess();
25+
void update_x_tilde(const DeviceBuffer<T> &x_tilde);
26+
void update_m(T m);
27+
T val(); // Calculate the value of the energy
28+
const DeviceBuffer<T> &grad(); // Calculate the gradient of the energy
29+
const DeviceTripletMatrix<T, 1> &hess(); // Calculate the Hessian matrix of the energy
30+
31+
private:
32+
// The implementation details of the VecAdder class are placed in the implementation class declared here.
33+
struct Impl;
34+
// The private pointer to the implementation class Impl
35+
std::unique_ptr<Impl> pimpl_;
36+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <Eigen/Dense>
5+
#include "uti.h"
6+
7+
template <typename T, int dim>
8+
class MassSpringEnergy
9+
{
10+
public:
11+
MassSpringEnergy(const std::vector<T> &x, const std::vector<int> &e, const std::vector<T> &l2, const std::vector<T> &k);
12+
MassSpringEnergy();
13+
~MassSpringEnergy();
14+
MassSpringEnergy(MassSpringEnergy &&rhs);
15+
MassSpringEnergy(const MassSpringEnergy &rhs);
16+
MassSpringEnergy &operator=(MassSpringEnergy &&rhs);
17+
18+
void update_x(const DeviceBuffer<T> &x);
19+
void update_e(const std::vector<int> &e);
20+
void update_l2(const std::vector<T> &l2);
21+
void update_k(const std::vector<T> &k);
22+
T val(); // Calculate the value of the energy
23+
const DeviceBuffer<T> &grad(); // Calculate the gradient of the energy
24+
const DeviceTripletMatrix<T, 1> &hess(); // Calculate the Hessian matrix of the energy
25+
26+
private:
27+
// The implementation details of the VecAdder class are placed in the implementation class declared here.
28+
struct Impl;
29+
// The private pointer to the implementation class Impl
30+
std::unique_ptr<Impl> pimpl_;
31+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
template <typename T>
6+
class SparseMatrix
7+
{
8+
public:
9+
SparseMatrix(int size);
10+
SparseMatrix();
11+
~SparseMatrix();
12+
SparseMatrix(SparseMatrix<T> &&rhs);
13+
SparseMatrix<T> &operator=(SparseMatrix<T> &&rhs);
14+
SparseMatrix<T> &operator=(SparseMatrix<T> &rhs);
15+
SparseMatrix<T> &operator*(const T &a);
16+
SparseMatrix(const SparseMatrix<T> &rhs);
17+
void set_value(int row, int col, T val, int loc);
18+
void set_diagonal(T val);
19+
20+
const std::vector<int> &get_row_buffer() const;
21+
const std::vector<int> &get_col_buffer() const;
22+
const std::vector<T> &get_val_buffer() const;
23+
std::vector<int> &set_row_buffer();
24+
std::vector<int> &set_col_buffer();
25+
std::vector<T> &set_val_buffer();
26+
SparseMatrix<T> &combine(const SparseMatrix<T> &other);
27+
28+
int get_size() const;
29+
30+
private:
31+
int size;
32+
std::vector<int> row_idx;
33+
std::vector<int> col_idx;
34+
std::vector<T> val;
35+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <muda/muda.h>
2+
#include <muda/container.h>
3+
#include <muda/cub/device/device_reduce.h>
4+
#include <Eigen/Dense>
5+
// utility functions
6+
template <typename T>
7+
T devicesum(const muda::DeviceBuffer<T> &buffer);
8+
9+
template <typename T, int Size>
10+
void __device__ make_PSD(const Eigen::Matrix<T, Size, Size> &hess, Eigen::Matrix<T, Size, Size> &PSD);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <vector>
4+
#include <cmath>
5+
#include "square_mesh.h"
6+
#include <iostream>
7+
template <typename T, int dim>
8+
class DirichletSimulator
9+
{
10+
public:
11+
DirichletSimulator();
12+
~DirichletSimulator();
13+
DirichletSimulator(DirichletSimulator &&rhs);
14+
DirichletSimulator &operator=(DirichletSimulator &&rhs);
15+
DirichletSimulator(T rho, T side_len, T initial_stretch, T K, T h, T tol, int n_seg);
16+
void run();
17+
18+
private:
19+
// The implementation details of the VecAdder class are placed in the implementation class declared here.
20+
struct Impl;
21+
// The private pointer to the implementation class Impl
22+
std::unique_ptr<Impl> pimpl_;
23+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
#include <vector>
3+
#include <memory>
4+
5+
// Function to generate mesh points and edges
6+
template <typename T>
7+
void generate(T side_length, int n_seg, std::vector<T> &x, std::vector<int> &e);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include <Eigen/Dense>
3+
#include "SparseMatrix.h"
4+
#include <muda/cub/device/device_reduce.h>
5+
#include <muda/container.h>
6+
#include <muda/muda.h>
7+
#include <muda/ext/linear_system.h>
8+
using namespace muda;
9+
10+
template <typename T>
11+
DeviceBuffer<T> add_vector(const DeviceBuffer<T> &a, const DeviceBuffer<T> &b, const T &factor1 = 1, const T &factor2 = 1);
12+
13+
template <typename T>
14+
DeviceBuffer<T> mult_vector(const DeviceBuffer<T> &a, const T &b);
15+
16+
template <typename T>
17+
DeviceTripletMatrix<T, 1> add_triplet(const DeviceTripletMatrix<T, 1> &a, const DeviceTripletMatrix<T, 1> &b, const T &factor1 = 1, const T &factor2 = 1);
18+
19+
template <typename T>
20+
T max_vector(const DeviceBuffer<T> &a);
21+
22+
template <typename T, int dim>
23+
void search_dir(const DeviceBuffer<T> &grad, const DeviceTripletMatrix<T, 1> &hess, DeviceBuffer<T> &dir, const DeviceBuffer<int> &DBC);
24+
25+
template <typename T>
26+
void display_vec(const DeviceBuffer<T> &vec);
27+
28+
template <typename T, int dim>
29+
void set_DBC(DeviceBuffer<T> &grad, DeviceCSRMatrix<T> &hess, const DeviceBuffer<int> &DBC);

0 commit comments

Comments
 (0)