Added the utility library itself (linked with target astaroth_utils)
This commit is contained in:
10
src/utils/CMakeLists.txt
Normal file
10
src/utils/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
## C++ standard
|
||||||
|
set(CMAKE_C_STANDARD 11)
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
## Compilation flags
|
||||||
|
add_compile_options(-Wall -Wextra -Werror -Wdouble-promotion -Wfloat-conversion -Wshadow)
|
||||||
|
|
||||||
|
## Compile
|
||||||
|
add_library(astaroth_utils STATIC config_loader.c memory.c)
|
||||||
|
target_link_libraries(astaroth_utils PRIVATE astaroth_core)
|
143
src/utils/config_loader.c
Normal file
143
src/utils/config_loader.c
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae.
|
||||||
|
|
||||||
|
This file is part of Astaroth.
|
||||||
|
|
||||||
|
Astaroth is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Astaroth is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* \brief Brief info.
|
||||||
|
*
|
||||||
|
* Detailed info.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include "config_loader.h"
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h> // UINT_MAX
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdint.h> // uint8_t, uint32_t
|
||||||
|
#include <stdio.h> // print
|
||||||
|
#include <string.h> // memset
|
||||||
|
//#include "src/core/math_utils.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Find the index of the keyword in names
|
||||||
|
\return Index in range 0...n if the keyword is in names. -1 if the keyword was
|
||||||
|
not found.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
find_str(const char keyword[], const char* names[], const int n)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
if (!strcmp(keyword, names[i]))
|
||||||
|
return i;
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
parse_config(const char* path, AcMeshInfo* config)
|
||||||
|
{
|
||||||
|
FILE* fp;
|
||||||
|
fp = fopen(path, "r");
|
||||||
|
// For knowing which .conf file will be used
|
||||||
|
printf("Config file path: \n %s \n ", path);
|
||||||
|
assert(fp != NULL);
|
||||||
|
|
||||||
|
const size_t BUF_SIZE = 128;
|
||||||
|
char keyword[BUF_SIZE];
|
||||||
|
char value[BUF_SIZE];
|
||||||
|
int items_matched;
|
||||||
|
while ((items_matched = fscanf(fp, "%s = %s", keyword, value)) != EOF) {
|
||||||
|
|
||||||
|
if (items_matched < 2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
int idx = -1;
|
||||||
|
if ((idx = find_str(keyword, intparam_names, NUM_INT_PARAMS)) >= 0)
|
||||||
|
config->int_params[idx] = atoi(value);
|
||||||
|
else if ((idx = find_str(keyword, realparam_names, NUM_REAL_PARAMS)) >= 0)
|
||||||
|
config->real_params[idx] = (AcReal)(atof(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
update_config(AcMeshInfo* config)
|
||||||
|
{
|
||||||
|
config->int_params[AC_mx] = config->int_params[AC_nx] + STENCIL_ORDER;
|
||||||
|
///////////// PAD TEST
|
||||||
|
// config->int_params[AC_mx] = config->int_params[AC_nx] + STENCIL_ORDER + PAD_SIZE;
|
||||||
|
///////////// PAD TEST
|
||||||
|
config->int_params[AC_my] = config->int_params[AC_ny] + STENCIL_ORDER;
|
||||||
|
config->int_params[AC_mz] = config->int_params[AC_nz] + STENCIL_ORDER;
|
||||||
|
|
||||||
|
// Bounds for the computational domain, i.e. nx_min <= i < nx_max
|
||||||
|
config->int_params[AC_nx_min] = STENCIL_ORDER / 2;
|
||||||
|
config->int_params[AC_nx_max] = config->int_params[AC_nx_min] + config->int_params[AC_nx];
|
||||||
|
config->int_params[AC_ny_min] = STENCIL_ORDER / 2;
|
||||||
|
config->int_params[AC_ny_max] = config->int_params[AC_ny] + STENCIL_ORDER / 2;
|
||||||
|
config->int_params[AC_nz_min] = STENCIL_ORDER / 2;
|
||||||
|
config->int_params[AC_nz_max] = config->int_params[AC_nz] + STENCIL_ORDER / 2;
|
||||||
|
|
||||||
|
// Spacing
|
||||||
|
config->real_params[AC_inv_dsx] = (AcReal)(1.) / config->real_params[AC_dsx];
|
||||||
|
config->real_params[AC_inv_dsy] = (AcReal)(1.) / config->real_params[AC_dsy];
|
||||||
|
config->real_params[AC_inv_dsz] = (AcReal)(1.) / config->real_params[AC_dsz];
|
||||||
|
|
||||||
|
/* Additional helper params */
|
||||||
|
// Int helpers
|
||||||
|
config->int_params[AC_mxy] = config->int_params[AC_mx] * config->int_params[AC_my];
|
||||||
|
config->int_params[AC_nxy] = config->int_params[AC_nx] * config->int_params[AC_ny];
|
||||||
|
config->int_params[AC_nxyz] = config->int_params[AC_nxy] * config->int_params[AC_nz];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\brief Loads data from astaroth.conf into a config struct.
|
||||||
|
\return 0 on success, -1 if there are potentially uninitialized values.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
acLoadConfig(const char* config_path, AcMeshInfo* config)
|
||||||
|
{
|
||||||
|
int retval = 0;
|
||||||
|
assert(config_path);
|
||||||
|
|
||||||
|
// memset reads the second parameter as a byte even though it says int in
|
||||||
|
// the function declaration
|
||||||
|
memset(config, (uint8_t)0xFF, sizeof(*config));
|
||||||
|
|
||||||
|
parse_config(config_path, config);
|
||||||
|
update_config(config);
|
||||||
|
#if VERBOSE_PRINTING // Defined in astaroth.h
|
||||||
|
printf("###############################################################\n");
|
||||||
|
printf("Config dimensions recalculated:\n");
|
||||||
|
acPrintMeshInfo(*config);
|
||||||
|
printf("###############################################################\n");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// sizeof(config) must be a multiple of 4 bytes for this to work
|
||||||
|
assert(sizeof(*config) % sizeof(uint32_t) == 0);
|
||||||
|
for (size_t i = 0; i < sizeof(*config) / sizeof(uint32_t); ++i) {
|
||||||
|
if (((uint32_t*)config)[i] == (uint32_t)0xFFFFFFFF) {
|
||||||
|
fprintf(stderr, "Some config values may be uninitialized. "
|
||||||
|
"See that all are defined in astaroth.conf\n");
|
||||||
|
retval = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
37
src/utils/config_loader.h
Normal file
37
src/utils/config_loader.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae.
|
||||||
|
|
||||||
|
This file is part of Astaroth.
|
||||||
|
|
||||||
|
Astaroth is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Astaroth is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* \brief Functions for loading and updating AcMeshInfo.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "astaroth.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Loads data from the config file */
|
||||||
|
int acLoadConfig(const char* config_path, AcMeshInfo* config);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
64
src/utils/memory.c
Normal file
64
src/utils/memory.c
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae.
|
||||||
|
|
||||||
|
This file is part of Astaroth.
|
||||||
|
|
||||||
|
Astaroth is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Astaroth is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "src/core/errchk.h"
|
||||||
|
|
||||||
|
AcResult
|
||||||
|
acMeshCreate(const AcMeshInfo info, AcMesh* mesh)
|
||||||
|
{
|
||||||
|
mesh->info = info;
|
||||||
|
|
||||||
|
const size_t bytes = acVertexBufferSizeBytes(mesh->info);
|
||||||
|
for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w) {
|
||||||
|
mesh->vertex_buffer[w] = malloc(bytes);
|
||||||
|
ERRCHK_ALWAYS(mesh->vertex_buffer[w]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return AC_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
AcResult
|
||||||
|
acMeshDestroy(AcMesh* mesh)
|
||||||
|
{
|
||||||
|
for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w)
|
||||||
|
free(mesh->vertex_buffer[w]);
|
||||||
|
|
||||||
|
return AC_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
AcResult
|
||||||
|
acMeshSet(const AcReal value, AcMesh* mesh)
|
||||||
|
{
|
||||||
|
const int n = acVertexBufferSize(mesh->info);
|
||||||
|
for (int w = 0; w < NUM_VTXBUF_HANDLES; ++w)
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
mesh->vertex_buffer[w][i] = value;
|
||||||
|
|
||||||
|
return AC_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
AcResult
|
||||||
|
acMeshClear(AcMesh* mesh)
|
||||||
|
{
|
||||||
|
return acMeshSet(0, mesh);
|
||||||
|
}
|
44
src/utils/memory.h
Normal file
44
src/utils/memory.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
Copyright (C) 2014-2019, Johannes Pekkilae, Miikka Vaeisalae.
|
||||||
|
|
||||||
|
This file is part of Astaroth.
|
||||||
|
|
||||||
|
Astaroth is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Astaroth is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* \brief Brief info.
|
||||||
|
*
|
||||||
|
* Detailed info.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
#include "astaroth.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
AcResult acMeshCreate(const AcMeshInfo mesh_info, AcMesh* mesh);
|
||||||
|
|
||||||
|
AcResult acMeshDestroy(AcMesh* mesh);
|
||||||
|
|
||||||
|
AcResult acMeshSet(const AcReal value, AcMesh* mesh);
|
||||||
|
|
||||||
|
AcResult acMeshClear(AcMesh* mesh);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
Reference in New Issue
Block a user