Skip to content

Commit

Permalink
Matrix in Linear Reconstruction dynamic array replace by static array (
Browse files Browse the repository at this point in the history
…#73)

* Matrix in Linear Reconstruction dynamic array replace by static array

* PR Changes 1

* PR Change 2
  • Loading branch information
dhyan1272 authored Dec 23, 2024
1 parent 4d9e866 commit 23e56ce
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/pmpo_MPMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void MPMesh::calcBasis() {
initArray(basisByArea3d,maxVtxsPerElm,0.0);

// calc basis
getBasisByAreaGblFormSpherical2(position3d, numVtx, v3d, radius, basisByArea3d);
getBasisByAreaGblFormSpherical(position3d, numVtx, v3d, radius, basisByArea3d);

// fill step
for(int i=0; i<= numVtx; i++){
Expand Down Expand Up @@ -339,7 +339,7 @@ void MPMesh::push(){
else
p_MPs->rebuild(); //rebuild pumi-pic
p_MPs->updateMPElmID(); //update mpElm IDs slices
reconstructSlices();
reconstructSlices();
}
while (anyIsMigrating);

Expand Down
13 changes: 6 additions & 7 deletions src/pmpo_MPMesh_assembly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,24 @@ void MPMesh::assemblyVtx1() {
p_MPs->parallel_for(assemble, "assembly");

//Solve Ax=b for each vertex
Kokkos::View<double*[vec4d_nEntries]> VtxCoeffs("VtxMatrices", p_mesh->getNumVertices());
Kokkos::View<double*[vec4d_nEntries]> VtxCoeffs("VtxCoeffs", p_mesh->getNumVertices());
Kokkos::parallel_for("solving Ax=b", numVtx, KOKKOS_LAMBDA(const int vtx){
Vec4d v0 = {VtxMatrices(vtx,0,0), VtxMatrices(vtx,0,1), VtxMatrices(vtx,0,2), VtxMatrices(vtx,0,3)};
Vec4d v1 = {VtxMatrices(vtx,1,0), VtxMatrices(vtx,1,1), VtxMatrices(vtx,1,2), VtxMatrices(vtx,1,3)};
Vec4d v2 = {VtxMatrices(vtx,2,0), VtxMatrices(vtx,2,1), VtxMatrices(vtx,2,2), VtxMatrices(vtx,2,3)};
Vec4d v3 = {VtxMatrices(vtx,3,0), VtxMatrices(vtx,3,1), VtxMatrices(vtx,3,2), VtxMatrices(vtx,3,3)};
Matrix4d A = {v0,v1,v2,v3};

//double f_norm=A.frobeniusNorm();

Matrix4d A = {v0,v1,v2,v3};
double A_trace = A.trace();
Matrix4d A_regularized = {v0, v1, v2, v3};
A_regularized.addToDiag(A_trace*1e-8);

double coeff[vec4d_nEntries]={0.0, 0.0, 0.0, 0.0};
CholeskySolve4d_UnitRHS(A_regularized, coeff);
for (int i=0; i<vec4d_nEntries; i++)
VtxCoeffs(vtx,i)=coeff[i];
});

//Reconstruct
auto reconstruct = PS_LAMBDA(const int& elm, const int& mp, const int& mask) {
if(mask) { //if material point is 'active'/'enabled'
Expand All @@ -189,7 +188,7 @@ void MPMesh::assemblyVtx1() {
}
};
p_MPs->parallel_for(reconstruct, "reconstruct");

Kokkos::parallel_for("assigning", numVtx, KOKKOS_LAMBDA(const int vtx){
for(int k=0; k<numEntries; k++)
meshField(vtx, k) = reconVals(vtx,k);
Expand Down
2 changes: 1 addition & 1 deletion src/pmpo_materialPoints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class MaterialPoints {
auto tgtPosXYZ = MPs->get<MPF_Tgt_Pos_XYZ>();
auto rotLatLonIncr = MPs->get<MPF_Rot_Lat_Lon_Incr>();

auto is_rotated = getRotatedFlag();
auto is_rotated = getRotatedFlag();
auto updateRotLatLon = PS_LAMBDA(const int& elm, const int& mp, const int& mask){
if(mask){
auto rotLat = curPosRotLatLon(mp,0) + rotLatLonIncr(mp,0); // phi
Expand Down
34 changes: 9 additions & 25 deletions src/pmpo_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,37 +214,21 @@ class Matrix4d {

private:

double** data_;
int rows_=4;
int cols_=4;
double data_[4][4];

public:

KOKKOS_INLINE_FUNCTION
Matrix4d(Vec4d v0, Vec4d v1, Vec4d v2, Vec4d v3){
data_ = new double*[rows_];

for (int i=0; i<rows_; i++){
data_[i] = new double[cols_];
}

for (int i=0; i<cols_; i++){
for (int i=0; i<4; i++){
data_[0][i] = v0[i];
data_[1][i] = v1[i];
data_[2][i] = v2[i];
data_[3][i] = v3[i];
}
}

//Destructor
KOKKOS_INLINE_FUNCTION
~Matrix4d(){
for (int i=0; i<4; i++){
delete[] data_[i];
}
delete[] data_;
}


//Retrieval
KOKKOS_INLINE_FUNCTION
double& operator()(int i, int j) { return data_[i][j];}
Expand All @@ -253,8 +237,8 @@ class Matrix4d {
KOKKOS_INLINE_FUNCTION
Matrix4d operator/(double scalar) const {
Matrix4d result(Vec4d(0, 0, 0, 0), Vec4d(0, 0, 0, 0), Vec4d(0, 0, 0, 0), Vec4d(0, 0, 0, 0));
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result(i, j) = data_[i][j] / scalar;
}
}
Expand All @@ -265,8 +249,8 @@ class Matrix4d {
KOKKOS_INLINE_FUNCTION
double frobeniusNorm() const {
double sum = 0.0; // Initialize sum of squares
for (int i = 0; i < rows_; i++) {
for (int j = 0; j < cols_; j++) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
sum += data_[i][j]* data_[i][j]; // Sum of squares
}
}
Expand All @@ -277,7 +261,7 @@ class Matrix4d {
KOKKOS_INLINE_FUNCTION
double trace() const {
double traceSum = 0.0; // Initialize sum of diagonal elements
for (int i = 0; i < rows_; i++) {
for (int i = 0; i < 4; i++) {
traceSum += data_[i][i]; // Sum diagonal elements
}
return traceSum;
Expand All @@ -286,7 +270,7 @@ class Matrix4d {
//Function to regularize the matrix
KOKKOS_INLINE_FUNCTION
void addToDiag(double eps) {
for (int i = 1; i < rows_; i++) {
for (int i = 1; i < 4; i++) {
data_[i][i]=data_[i][i]+eps;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pmpo_wachspressBasis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ void sphericalInterpolation(MPMesh& mpMesh){
initArray(basisByArea3d,maxVtxsPerElm,0.0);

// calc basis
getBasisByAreaGblFormSpherical2(position3d, numVtx, v3d, radius, basisByArea3d);
getBasisByAreaGblFormSpherical(position3d, numVtx, v3d, radius, basisByArea3d);

// interpolation step
for(int entry=0; entry<numEntries; entry++){
Expand Down

0 comments on commit 23e56ce

Please sign in to comment.