Skip to content

Commit

Permalink
Feature: Added json array support. Implement init json (#3612)
Browse files Browse the repository at this point in the history
  • Loading branch information
grysgreat authored Feb 23, 2024
1 parent dfbe021 commit c9c0c7a
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ dist
toolchain.tar.gz
time.json
*.pyc
__pycache__
__pycache__
abacus.json
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ OBJS_IO=input.o\
para_json.o\
abacusjson.o\
general_info.o\
init_info.o\


OBJS_IO_LCAO=cal_r_overlap_R.o\
Expand Down
3 changes: 2 additions & 1 deletion source/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ void Driver::init()
INPUT.close_log();

// (5) output the json file
Json::json_output();
//Json::create_Json(&GlobalC::ucell.symm,GlobalC::ucell.atoms,&INPUT);
Json::create_Json(&GlobalC::ucell,&INPUT);
return;
}

Expand Down
9 changes: 8 additions & 1 deletion source/module_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "module_io/print_info.h"
#include "module_base/timer.h"
#include "module_io/input.h"
#include "module_io/json_output/init_info.h"


//--------------Temporary----------------
#include "module_base/global_variable.h"
Expand Down Expand Up @@ -475,7 +477,12 @@ namespace ModuleESolver

ModuleBase::timer::tick(this->classname, "Run");
}

// add nkstot,nkstot_ibz to output json
#ifdef __RAPIDJSON
int Jnkstot = this->pelec->klist->nkstot;
int Jnkstot_ibz = this->pelec->klist->nkstot_ibz;
Json::add_nkstot(Jnkstot,Jnkstot_ibz);
#endif //__RAPIDJSON
return;
};

Expand Down
2 changes: 1 addition & 1 deletion source/module_io/json_output/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
list(APPEND objects_json
abacusjson.cpp
general_info.cpp

init_info.cpp
)

add_library(
Expand Down
39 changes: 35 additions & 4 deletions source/module_io/json_output/abacusjson.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef ABACUS_JSON_H
#define ABACUS_JSON_H

#include <fstream>
#include <iostream>
#include <string>
Expand All @@ -8,15 +11,40 @@
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"

//define of AbacusJson
/**
* @brief Define of AbacusJson:These macro definitions simplify the complex parameters
* required to add objects in rapidjson, making it easy to use.
* @usage: 1. create the json value by Type: eg.
* Json::jsonValue object(JobjectType);
* Json::jsonValue array(JarrayType);
* 2 - object. add the parameter by using correct function. If the constructed object contains
* type std::string, select the correct macro definition function in the key
* or value position based on std::string.
* eg. key is std::string, using: object.JaddStringK(str,val)
* eg. val is std::string, using: object.JaddStringV (str,val)
* eg. both key,val is std::string, using: object.JaddStringKV(str,val)
* eg. none of key,val is std::string, using: object.JaddNormal(str,val)
*
* 2 - array. using: array.JPushBack(val)
* or : JPushBackString(val)
*/

#define JobjectType rapidjson::kObjectType
#define JarrayType rapidjson::kArrayType
#define Get_Jallocator Json::AbacusJson::allocator()
#define Set_JString(str) Json::jsonValue().SetString(str.c_str(),str.length(),Get_Jallocator)
#define JaddString(str,val) AddMember(str, Set_JString(val), Get_Jallocator)


#define JaddStringV(str,val) AddMember(str, Set_JString(val), Get_Jallocator)
#define JaddStringK(str,val) AddMember(Set_JString(str), val, Get_Jallocator)
#define JaddStringKV(str,val) AddMember(Set_JString(str), Set_JString(val), Get_Jallocator)
#define JaddNormal(str,val) AddMember(str, val, Get_Jallocator)
#define JPushBack(val) PushBack(val, Get_Jallocator)

#define JPushBackString(val) PushBack(Set_JString(val), Get_Jallocator)
/**
* @brief The json processing module in abacus contains basic meta-operations such as
* adding, modifying, and checking json.
*/

namespace Json
{
Expand All @@ -26,7 +54,7 @@ using jsonValue = rapidjson::Value;
class AbacusJson
{
public:
// Add a key value pair to the json
// The template specialization method adds a key-val object to the doc tree
template <typename T>
static void add_json(std::vector<std::string> keys, const T& value,bool IsArray)
{
Expand All @@ -38,6 +66,7 @@ class AbacusJson
add_nested_member(keys.begin(), keys.end(), val, doc, doc.GetAllocator(),IsArray);
}


// Output the json to a file
static void write_to_json(std::string filename);

Expand Down Expand Up @@ -66,4 +95,6 @@ void AbacusJson::add_json(std::vector<std::string> keys, const rapidjson::Value&


} // namespace Json
#endif

#endif
16 changes: 9 additions & 7 deletions source/module_io/json_output/general_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include "module_base/parallel_global.h"
#include "module_io/input.h"
#include "version.h"

//Add json objects to gener_info
namespace Json
{

#ifdef __RAPIDJSON
void gen_general_info()
void gen_general_info(Input *input)
{

#ifdef VERSION
Expand All @@ -24,7 +26,7 @@ void gen_general_info()
#endif

// start_time
std::time_t start_time = INPUT.get_start_time();
std::time_t start_time = input->get_start_time();
std::string start_time_str;
convert_time(start_time, start_time_str);

Expand All @@ -43,13 +45,13 @@ void gen_general_info()

AbacusJson::add_json({"general_info", "version"}, version,false);
AbacusJson::add_json({"general_info", "commit"}, commit,false);
AbacusJson::add_json({"general_info", "device"}, INPUT.device,false);
AbacusJson::add_json({"general_info", "device"}, input->device,false);
AbacusJson::add_json({"general_info", "mpi_num"}, mpi_num,false);
AbacusJson::add_json({"general_info", "omp_num"}, omp_num,false);
AbacusJson::add_json({"general_info", "pseudo_dir"}, INPUT.pseudo_dir,false);
AbacusJson::add_json({"general_info", "orbital_dir"}, INPUT.orbital_dir,false);
AbacusJson::add_json({"general_info", "stru_file"}, INPUT.stru_file,false);
AbacusJson::add_json({"general_info", "kpt_file"}, INPUT.kpoint_file,false);
AbacusJson::add_json({"general_info", "pseudo_dir"}, input->pseudo_dir,false);
AbacusJson::add_json({"general_info", "orbital_dir"}, input->orbital_dir,false);
AbacusJson::add_json({"general_info", "stru_file"}, input->stru_file,false);
AbacusJson::add_json({"general_info", "kpt_file"}, input->kpoint_file,false);
AbacusJson::add_json({"general_info", "start_time"}, start_time_str,false);
AbacusJson::add_json({"general_info", "end_time"}, end_time_str,false);
}
Expand Down
3 changes: 2 additions & 1 deletion source/module_io/json_output/general_info.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "module_io/input.h"

//Add json objects to gener_info
namespace Json
{
#ifdef __RAPIDJSON
void gen_general_info();
void gen_general_info(Input *input);
#endif
}
58 changes: 58 additions & 0 deletions source/module_io/json_output/init_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "init_info.h"
#include "module_io/input.h"
#include "../para_json.h"
#include "abacusjson.h"


//Add json objects to init
namespace Json
{

#ifdef __RAPIDJSON


// void gen_init(ModuleSymmetry::Symmetry *symm,Atom *atoms){
// std::string pgname = symm->pgname;
// std::string spgname = symm->spgname;
// AbacusJson::add_json({"init", "pgname"}, pgname,false);
// AbacusJson::add_json({"init", "spgname"}, spgname,false);


// int numAtoms = atoms->na;
// AbacusJson::add_json({"init", "natom"}, numAtoms,false);
// AbacusJson::add_json({"init", "nband"}, INPUT.nbands,false);


// }

void gen_init(UnitCell *ucell){
std::string pgname = ucell->symm.pgname;
std::string spgname = ucell->symm.spgname;
AbacusJson::add_json({"init", "point_group"}, pgname,false);
AbacusJson::add_json({"init", "point_group_in_space"}, spgname,false);


int numAtoms = ucell->atoms->na;
AbacusJson::add_json({"init", "natom"}, numAtoms,false);
AbacusJson::add_json({"init", "nband"}, GlobalV::NBANDS,false);

int ntype = ucell->ntype;
AbacusJson::add_json({"init", "nelectron"}, ntype,false);

for (int it = 0; it < ntype; it++)
{
std::string label = ucell->atoms[it].label;
int number = ucell->atoms[it].ncpp.zv;
AbacusJson::add_json({"init", "nelectron_each_type",label}, number,false);
}

}


void add_nkstot(int nkstot,int nkstot_ibz){
Json::AbacusJson::add_json({"init", "nkstot"}, nkstot,false);
Json::AbacusJson::add_json({"init", "nkstot_ibz"}, nkstot_ibz,false);
}

#endif
} // namespace Json
17 changes: 17 additions & 0 deletions source/module_io/json_output/init_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "module_cell/module_symmetry/symmetry.h"
#include "module_cell/atom_spec.h"
#include "module_cell/unitcell.h"


//Add json objects to init
namespace Json
{
#ifdef __RAPIDJSON
// void gen_init(ModuleSymmetry::Symmetry *symm,Atom *atoms);
void gen_init(UnitCell *ucell);

void add_nkstot(int nkstot,int nkstot_ibz);


#endif
}
5 changes: 3 additions & 2 deletions source/module_io/json_output/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ remove_definitions(-DUSE_PAW)

AddTest(
TARGET io_json_output_json
LIBS ${math_libs} base device
SOURCES para_json_test.cpp ../general_info.cpp ../../para_json.cpp ../abacusjson.cpp ../../input.cpp
LIBS ${math_libs} base device cell_info
SOURCES para_json_test.cpp ../general_info.cpp ../init_info.cpp
../../para_json.cpp ../abacusjson.cpp ../../input.cpp ../../output.cpp
)
Loading

0 comments on commit c9c0c7a

Please sign in to comment.