Merge pull request #4542 from akohlmey/add-json-lib

Integrate header-only JSON library
This commit is contained in:
Axel Kohlmeyer
2025-04-17 13:56:16 -04:00
committed by GitHub
9 changed files with 24895 additions and 0 deletions

View File

@ -34,6 +34,7 @@
#include "group.h" #include "group.h"
#include "improper.h" #include "improper.h"
#include "input.h" #include "input.h"
#include "json.h"
#include "lmpfftsettings.h" #include "lmpfftsettings.h"
#include "modify.h" #include "modify.h"
#include "neighbor.h" #include "neighbor.h"
@ -291,6 +292,7 @@ void Info::command(int narg, char **arg)
utils::print(out,"\nCompiler: {} with {}\nC++ standard: {}\n", utils::print(out,"\nCompiler: {} with {}\nC++ standard: {}\n",
platform::compiler_info(),platform::openmp_standard(),platform::cxx_standard()); platform::compiler_info(),platform::openmp_standard(),platform::cxx_standard());
fputs(get_fmt_info().c_str(), out); fputs(get_fmt_info().c_str(), out);
fputs(get_json_info().c_str(), out);
fputs("\nActive compile time flags:\n\n",out); fputs("\nActive compile time flags:\n\n",out);
if (has_gzip_support()) fputs("-DLAMMPS_GZIP\n",out); if (has_gzip_support()) fputs("-DLAMMPS_GZIP\n",out);
@ -1320,6 +1322,16 @@ std::string Info::get_fmt_info()
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
std::string Info::get_json_info()
{
return fmt::format("Embedded JSON class version: {}.{}.{}\n",
NLOHMANN_JSON_VERSION_MAJOR,
NLOHMANN_JSON_VERSION_MINOR,
NLOHMANN_JSON_VERSION_PATCH);
}
/* ---------------------------------------------------------------------- */
void Info::get_memory_info(double *meminfo) void Info::get_memory_info(double *meminfo)
{ {
double bytes = 0; double bytes = 0;

View File

@ -50,6 +50,7 @@ class Info : public Command {
const std::string &); const std::string &);
static std::string get_fft_info(); static std::string get_fft_info();
static std::string get_fmt_info(); static std::string get_fmt_info();
static std::string get_json_info();
static bool has_gpu_device(); static bool has_gpu_device();
static std::string get_gpu_device_info(); static std::string get_gpu_device_info();
static std::string get_accelerator_info(const std::string &pkg = ""); static std::string get_accelerator_info(const std::string &pkg = "");

25
src/json.h Normal file
View File

@ -0,0 +1,25 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LMP_JSON_H
#define LMP_JSON_H
// wrapper around including the JSON parsing and writing class
// Do NOT include in any header file
#include "../third_party/nlohmann/json.hpp"
namespace LAMMPS_NS {
using json = ::nlohmann_lmp::json;
}
#endif

View File

@ -1456,6 +1456,7 @@ void LAMMPS::print_config(FILE *fp)
platform::compiler_info(),platform::openmp_standard(), platform::compiler_info(),platform::openmp_standard(),
platform::cxx_standard()); platform::cxx_standard());
fputs(Info::get_fmt_info().c_str(),fp); fputs(Info::get_fmt_info().c_str(),fp);
fputs(Info::get_json_info().c_str(),fp);
int major,minor; int major,minor;
std::string infobuf = platform::mpi_info(major,minor); std::string infobuf = platform::mpi_info(major,minor);

View File

@ -17,6 +17,8 @@
#include "input.h" #include "input.h"
#include "library.h" #include "library.h"
#include "json.h"
#include <cstdlib> #include <cstdlib>
#include <mpi.h> #include <mpi.h>
#include <new> #include <new>
@ -75,6 +77,11 @@ int main(int argc, char **argv)
finalize(); finalize();
MPI_Abort(MPI_COMM_WORLD, 1); MPI_Abort(MPI_COMM_WORLD, 1);
exit(1); exit(1);
} catch (json::exception &je) {
fprintf(stderr, "\nJSON library error %d: %s\n", je.id, je.what());
finalize();
MPI_Abort(MPI_COMM_WORLD, 1);
exit(1);
} catch (std::bad_alloc &ae) { } catch (std::bad_alloc &ae) {
fprintf(stderr, "C++ memory allocation failed: %s\n", ae.what()); fprintf(stderr, "C++ memory allocation failed: %s\n", ae.what());
finalize(); finalize();

1
third_party/README vendored Normal file
View File

@ -0,0 +1 @@
This folder contains copies of third-party software

24766
third_party/nlohmann/json.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -49,6 +49,10 @@ add_executable(test_fmtlib test_fmtlib.cpp)
target_link_libraries(test_fmtlib PRIVATE lammps GTest::GMockMain) target_link_libraries(test_fmtlib PRIVATE lammps GTest::GMockMain)
add_test(NAME FmtLib COMMAND test_fmtlib) add_test(NAME FmtLib COMMAND test_fmtlib)
add_executable(test_json test_json.cpp)
target_link_libraries(test_json PRIVATE lammps GTest::GMockMain)
add_test(NAME JSON COMMAND test_json)
add_executable(test_math_eigen_impl test_math_eigen_impl.cpp) add_executable(test_math_eigen_impl test_math_eigen_impl.cpp)
target_include_directories(test_math_eigen_impl PRIVATE ${LAMMPS_SOURCE_DIR}) target_include_directories(test_math_eigen_impl PRIVATE ${LAMMPS_SOURCE_DIR})
add_test(NAME MathEigen COMMAND test_math_eigen_impl 10 5) add_test(NAME MathEigen COMMAND test_math_eigen_impl 10 5)

View File

@ -0,0 +1,78 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://www.lammps.org/, Sandia National Laboratories
LAMMPS Development team: developers@lammps.org
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "lmptype.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <exception>
#include <string>
#include "json.h"
using namespace LAMMPS_NS;
using ::testing::Eq;
#define STRINGIFY(val) XSTR(val)
#define XSTR(val) #val
// this tests a subset of the JSON class that is most relevant to LAMMPS
TEST(JSON, namespace)
{
std::string expected = "nlohmann_lmp::json_abi";
expected += "_v" STRINGIFY(NLOHMANN_JSON_VERSION_MAJOR);
expected += "_" STRINGIFY(NLOHMANN_JSON_VERSION_MINOR);
expected += "_" STRINGIFY(NLOHMANN_JSON_VERSION_PATCH);
const std::string ns{STRINGIFY(NLOHMANN_JSON_NAMESPACE)};
ASSERT_THAT(expected, Eq(ns));
}
TEST(JSON, serialize_deserialize)
{
json j1;
j1["pi"] = 3.141;
j1["happy"] = true;
j1["name"] = "Niels";
j1["nothing"] = nullptr;
std::string expected = "{\"happy\":true,\"name\":\"Niels\",\"nothing\":null,\"pi\":3.141}";
std::string dumped = j1.dump();
ASSERT_THAT(expected, Eq(dumped));
json j2 = json::parse(expected);
ASSERT_TRUE(j1 == j2);
}
TEST(JSON, init_vs_incremental)
{
json j1;
j1["pi"] = 3.141;
j1["happy"] = true;
j1["name"] = "Niels";
j1["nothing"] = nullptr;
j1["answer"]["everything"] = 42;
j1["list"] = {1, 0, 2};
j1["object"] = {{"currency", "USD"}, {"value", 42.99}};
json j2 = {{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42}}},
{"list", {1, 0, 2}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
ASSERT_TRUE(j1 == j2);
}