Skip to content

Commit

Permalink
Merge pull request #93 from cindytsai/ClangFormat
Browse files Browse the repository at this point in the history
Code-Formatting and Add Pre-Commit
  • Loading branch information
cindytsai authored Nov 9, 2023
2 parents cf51ce9 + 8c12438 commit e8e43eb
Show file tree
Hide file tree
Showing 54 changed files with 3,142 additions and 3,299 deletions.
44 changes: 44 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
BasedOnStyle: Google
AccessModifierOffset: '-4'
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: 'true'
AlignEscapedNewlines: Right
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowAllConstructorInitializersOnNextLine: 'true'
AllowShortCaseLabelsOnASingleLine: 'false'
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'Yes'
BreakBeforeBraces: Attach
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: AfterColon
ColumnLimit: '120'
CommentPragmas: '''^ *'''
CompactNamespaces: 'false'
IndentCaseLabels: 'true'
IndentPPDirectives: None
IndentWidth: '4'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
DerivePointerAlignment: false
ReflowComments: 'true'
SpaceAfterCStyleCast: 'false'
SpaceAfterLogicalNot: 'false'
SpaceAfterTemplateKeyword: 'false'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeCtorInitializerColon: 'true'
SpaceBeforeInheritanceColon: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
SpacesBeforeTrailingComments: '2'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInContainerLiterals: 'false'
SpacesInParentheses: 'false'
SpacesInSquareBrackets: 'false'
Standard: Cpp11
UseTab: Never

...
105 changes: 47 additions & 58 deletions .github/tools/generate_density_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,53 @@
* data dir to store dumped data file.
*/

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <dirent.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

// grid information
#define NGRID_1D 5 // number of root grids along each direction
#define GRID_DIM 8 // grid dimension (this example assumes cubic grids)
#define REFINE_BY 2 // refinement factor between two AMR levels
#define GHOST_CELL 0 // this must be 0, since we are only generating data here
#define NGRID_1D 5 // number of root grids along each direction
#define GRID_DIM 8 // grid dimension (this example assumes cubic grids)
#define REFINE_BY 2 // refinement factor between two AMR levels
#define GHOST_CELL 0 // this must be 0, since we are only generating data here

// convenient macros
#define SQR(a) ( (a)*(a) )
#define CUBE(a) ( (a)*(a)*(a) )

#define SQR(a) ((a) * (a))
#define CUBE(a) ((a) * (a) * (a))

double set_density(const double x, const double y, const double z, const double t, const double v);

enum yt_dtype : int {
YT_FLOAT = 0, YT_DOUBLE, YT_INT, YT_LONG, YT_DTYPE_UNKNOWN
};
enum yt_dtype : int { YT_FLOAT = 0, YT_DOUBLE, YT_INT, YT_LONG, YT_DTYPE_UNKNOWN };

struct yt_data {
void *data_ptr;
void* data_ptr;
int data_dimensions[3];
yt_dtype data_dtype;
};

struct yt_grid {
double left_edge[3];
double right_edge[3];
long *particle_count_list;
long* particle_count_list;
long grid_particle_count;
long id;
long parent_id;
int grid_dimensions[3];
int level;
int proc_num;
yt_data *field_data;
yt_data* field_data;
};

//-------------------------------------------------------------------------------------------------------
// Function : main
// Description : Main function
//-------------------------------------------------------------------------------------------------------
int main(int argc, char *argv[]) {
int main(int argc, char* argv[]) {
// create data dir if it does not exit, cause dumped data will store here.
DIR *dir = opendir("data");
DIR* dir = opendir("data");
if (dir) {
closedir(dir);
} else if (ENOENT == errno) {
Expand All @@ -65,24 +62,23 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}


// **********************************************
// following mimic the simulation evolution loop
// **********************************************
const int total_steps = 11; // total number of evolution steps
const double velocity = 1.0; // velocity for setting the density field
const double dt = 0.05; // evolution time-step
const double box_size = 1.0; // simulation box size
const double dh0 = box_size / (NGRID_1D * GRID_DIM); // cell size at level 0
const double dh1 = dh0 / REFINE_BY; // cell size at level 1
const int num_fields = 1; // number of fields
const int num_grids = CUBE(NGRID_1D) + CUBE(REFINE_BY); // number of grids
const int total_steps = 11; // total number of evolution steps
const double velocity = 1.0; // velocity for setting the density field
const double dt = 0.05; // evolution time-step
const double box_size = 1.0; // simulation box size
const double dh0 = box_size / (NGRID_1D * GRID_DIM); // cell size at level 0
const double dh1 = dh0 / REFINE_BY; // cell size at level 1
const int num_fields = 1; // number of fields
const int num_grids = CUBE(NGRID_1D) + CUBE(REFINE_BY); // number of grids
double time = 0.0;
double (*field_data)[num_fields][CUBE(GRID_DIM + GHOST_CELL * 2)]
= new double[num_grids][num_fields][CUBE(GRID_DIM + GHOST_CELL * 2)];
double(*field_data)[num_fields][CUBE(GRID_DIM + GHOST_CELL * 2)] =
new double[num_grids][num_fields][CUBE(GRID_DIM + GHOST_CELL * 2)];

for (int step = 0; step < total_steps; step++) {
yt_grid *sim_grids = new yt_grid[num_grids];
yt_grid* sim_grids = new yt_grid[num_grids];

// set level-0 grids
int grid_order[3];
Expand All @@ -107,32 +103,28 @@ int main(int argc, char *argv[]) {
const double z = sim_grids[gid].left_edge[2] + ((k - GHOST_CELL) + 0.5) * dh0;

for (int v = 0; v < num_fields; v++) {
field_data[gid][v][
(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) +
i] = set_density(x, y, z, time, velocity);
field_data[gid][v]
[(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) +
i] = set_density(x, y, z, time, velocity);
}
}
} // for grid_order[0/1/2]

} // for grid_order[0/1/2]

// in this example we refine the root grid with the peak density into REFINE_BY^3 subgrids
const double peak[3] = {0.5 * box_size + velocity * time,
0.5 * box_size + velocity * time,
0.5 * box_size};
const double peak[3] = {0.5 * box_size + velocity * time, 0.5 * box_size + velocity * time, 0.5 * box_size};
const double grid_width = GRID_DIM * dh0;
const int center_idx[3] = {int(peak[0] / grid_width),
int(peak[1] / grid_width),
int(peak[2] / grid_width)};
const int center_idx[3] = {int(peak[0] / grid_width), int(peak[1] / grid_width), int(peak[2] / grid_width)};
const int gid_refine = (center_idx[2] * NGRID_1D + center_idx[1]) * NGRID_1D + center_idx[0];
const int gid_offset = CUBE(NGRID_1D);

for (grid_order[2] = 0; grid_order[2] < REFINE_BY; grid_order[2]++)
for (grid_order[1] = 0; grid_order[1] < REFINE_BY; grid_order[1]++)
for (grid_order[0] = 0; grid_order[0] < REFINE_BY; grid_order[0]++) {
const int gid = (grid_order[2] * REFINE_BY + grid_order[1]) * REFINE_BY
+ grid_order[0] + gid_offset;
const int gid =
(grid_order[2] * REFINE_BY + grid_order[1]) * REFINE_BY + grid_order[0] + gid_offset;
for (int d = 0; d < 3; d++) {
sim_grids[gid].left_edge[d] = sim_grids[gid_refine].left_edge[d] + grid_order[d] * GRID_DIM * dh1;
sim_grids[gid].left_edge[d] =
sim_grids[gid_refine].left_edge[d] + grid_order[d] * GRID_DIM * dh1;
sim_grids[gid].right_edge[d] = sim_grids[gid].left_edge[d] + GRID_DIM * dh1;
sim_grids[gid].grid_dimensions[d] = GRID_DIM;
}
Expand All @@ -148,16 +140,16 @@ int main(int argc, char *argv[]) {
const double z = sim_grids[gid].left_edge[2] + ((k - GHOST_CELL) + 0.5) * dh1;

for (int v = 0; v < num_fields; v++) {
field_data[gid][v][
(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) +
i] = set_density(x, y, z, time, velocity);
field_data[gid][v]
[(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) +
i] = set_density(x, y, z, time, velocity);
}
}
} // for grid_order[0/1/2]
} // for grid_order[0/1/2]

// output density data in each step
for (int gid = 0; gid < num_grids; gid++) {
FILE *fp;
FILE* fp;
char filename[100];

sprintf(filename, "./data/Dens_grid%d_step%d.txt", gid, step);
Expand All @@ -167,7 +159,8 @@ int main(int argc, char *argv[]) {
for (int j = GHOST_CELL; j < GRID_DIM + GHOST_CELL; j++)
for (int i = GHOST_CELL; i < GRID_DIM + GHOST_CELL; i++) {
fprintf(fp, "%.10f\n",
field_data[gid][0][(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) + i]);
field_data[gid][0]
[(k * (GRID_DIM + GHOST_CELL * 2) + j) * (GRID_DIM + GHOST_CELL * 2) + i]);
}
fclose(fp);
}
Expand All @@ -178,27 +171,23 @@ int main(int argc, char *argv[]) {
delete[] sim_grids;

time += dt;
} // for (int step=0; step<total_steps; step++)
} // for (int step=0; step<total_steps; step++)

return EXIT_SUCCESS;

} // FUNCTION : main


} // FUNCTION : main

//-------------------------------------------------------------------------------------------------------
// Function : set_density
// Description : Return density at given coordinates and time
//-------------------------------------------------------------------------------------------------------
double set_density(const double x, const double y, const double z, const double t, const double v) {

const double center[3] = {0.5 + v * t, 0.5 + v * t, 0.5}; // drift with v along (1,1,0)
const double center[3] = {0.5 + v * t, 0.5 + v * t, 0.5}; // drift with v along (1,1,0)
const double sigma = 0.05;
const double amplitude = 1.0e6;
const double background = 1.0;

return amplitude * exp(-0.5 * (SQR(x - center[0]) + SQR(y - center[1]) + SQR(z - center[2])) / SQR(sigma)) +
background;

} // FUNCTION : set_density

} // FUNCTION : set_density
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
ci:
autofix_commit_msg: |
[pre-commit.ci] auto fixes from pre-commit.com hooks
autofix_prs: false
autoupdate_branch: ''
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: weekly
skip: []
submodules: false

repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
types_or: ['c++', 'c']
args: ['-style=file']
Loading

0 comments on commit e8e43eb

Please sign in to comment.