Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ OBJS_CELL=atom_pseudo.o\
klist.o\
k_vector_utils.o\
cell_index.o\
cell_tools.o\
check_atomic_stru.o\
update_cell.o\
bcast_cell.o\
Expand Down
1 change: 1 addition & 0 deletions source/source_cell/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(
klist.cpp
parallel_kpoints.cpp
cell_index.cpp
cell_tools.cpp
check_atomic_stru.cpp
update_cell.cpp
magnetism.cpp
Expand Down
117 changes: 117 additions & 0 deletions source/source_cell/cell_tools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* @file cell_tools.cpp
* @brief Implementation of cell tool free functions.
*/
#include "cell_tools.h"

namespace unitcell
{
std::vector<std::string> get_atomLabels(const Atom* atoms, const int ntype)
{
std::vector<std::string> atomLabels(ntype);
for (int it = 0; it < ntype; it++)
{
atomLabels[it] = atoms[it].label;
}
return atomLabels;
}

std::vector<int> get_atomCounts(const Atom* atoms, const int ntype)
{
std::vector<int> atomCounts(ntype);
for (int it = 0; it < ntype; it++)
{
atomCounts[it] = atoms[it].na;
}
return atomCounts;
}

std::vector<std::vector<int>> get_lnchiCounts(const Atom* atoms, const int ntype)
{
std::vector<std::vector<int>> lnchiCounts(ntype);
for (int it = 0; it < ntype; it++)
{
lnchiCounts[it].resize(atoms[it].nwl + 1);
for (int L = 0; L < atoms[it].nwl + 1; L++)
{
lnchiCounts[it][L] = atoms[it].l_nchi[L];
}
}
return lnchiCounts;
}

std::vector<ModuleBase::Vector3<double>> get_target_mag(const Atom* atoms,
const int ntype,
const int nat)
{
std::vector<ModuleBase::Vector3<double>> target_mag(nat);
int iat = 0;
for (int it = 0; it < ntype; it++)
{
for (int ia = 0; ia < atoms[it].na; ia++)
{
target_mag[iat] = atoms[it].m_loc_[ia];
++iat;
}
}
return target_mag;
}

std::vector<ModuleBase::Vector3<double>> get_lambda(const Atom* atoms,
const int ntype,
const int nat)
{
std::vector<ModuleBase::Vector3<double>> lambda(nat);
int iat = 0;
for (int it = 0; it < ntype; it++)
{
for (int ia = 0; ia < atoms[it].na; ia++)
{
lambda[iat] = atoms[it].lambda[ia];
++iat;
}
}
return lambda;
}

std::vector<ModuleBase::Vector3<int>> get_constrain(const Atom* atoms,
const int ntype,
const int nat)
{
std::vector<ModuleBase::Vector3<int>> constrain(nat);
int iat = 0;
for (int it = 0; it < ntype; it++)
{
for (int ia = 0; ia < atoms[it].na; ia++)
{
constrain[iat] = atoms[it].constrain[ia];
++iat;
}
}
return constrain;
}

bool if_atoms_can_move(const Atom* atoms, const int ntype)
{
for (int it = 0; it < ntype; it++)
{
for (int ia = 0; ia < atoms[it].na; ia++)
{
if (atoms[it].mbl[ia].x || atoms[it].mbl[ia].y || atoms[it].mbl[ia].z)
{
return true;
}
}
}
return false;
}

bool if_cell_can_change(const std::vector<int>& lat_axis_free)
{
if (lat_axis_free[0] || lat_axis_free[1] || lat_axis_free[2])
{
return true;
}
return false;
}
}
76 changes: 76 additions & 0 deletions source/source_cell/cell_tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @file cell_tools.h
* @brief Free function tools for extracting cell/atom information.
*/
#ifndef CELL_TOOLS_H
#define CELL_TOOLS_H

#include <string>
#include <vector>

#include "source_base/vector3.h"
#include "source_cell/atom_spec.h"

/**
* @brief Free functions for extracting atom/orbital info from Atom array.
*/
namespace unitcell
{
/// @brief Get atom labels for each atom type.
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @return vector of atom labels, one per type
std::vector<std::string> get_atomLabels(const Atom* atoms, const int ntype);

/// @brief Get atom counts (number of atoms) for each atom type.
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @return vector of atom counts, one per type
std::vector<int> get_atomCounts(const Atom* atoms, const int ntype);

/// @brief Get lnchi counts (number of chi functions per L) for each atom type.
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @return vector of lnchi counts, one vector per type
std::vector<std::vector<int>> get_lnchiCounts(const Atom* atoms, const int ntype);

/// @brief Get target magnetic moment for each atom (used by deltaspin).
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @param nat total number of atoms [in]
/// @return vector of target magnetic moments, one per atom
std::vector<ModuleBase::Vector3<double>> get_target_mag(const Atom* atoms,
const int ntype,
const int nat);

/// @brief Get Lagrange multiplier for each atom (used by deltaspin).
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @param nat total number of atoms [in]
/// @return vector of Lagrange multipliers, one per atom
std::vector<ModuleBase::Vector3<double>> get_lambda(const Atom* atoms,
const int ntype,
const int nat);

/// @brief Get constrain flag for each atom (used by deltaspin).
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @param nat total number of atoms [in]
/// @return vector of constrain flags, one per atom
std::vector<ModuleBase::Vector3<int>> get_constrain(const Atom* atoms,
const int ntype,
const int nat);

/// @brief Judge if any atom can move (any mbl component is non-zero).
/// @param atoms atom pointer [in]
/// @param ntype number of atom types [in]
/// @return true if at least one atom is allowed to move
bool if_atoms_can_move(const Atom* atoms, const int ntype);

/// @brief Judge if any lattice vector can change.
/// @param lat_axis_free lattice-axis freedom flags (size 3) [in]
/// @return true if at least one lattice axis is free to change
bool if_cell_can_change(const std::vector<int>& lat_axis_free);
}

#endif // CELL_TOOLS_H
8 changes: 2 additions & 6 deletions source/source_cell/module_neighbor/test/prepare_unitcell.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ class UcellTestPrepare
//basic info
this->ntype = this->elements.size();
UnitCell* ucell = new UnitCell;
ucell->setup(this->latname,
ucell->setup_from_input(this->latname,
this->ntype,
this->lmaxmax,
this->init_vel,
this->fixed_axes);

ucell->atom_label.resize(ucell->ntype);
ucell->atom_mass.resize(ucell->ntype);
ucell->pseudo_fn.resize(ucell->ntype);
ucell->pseudo_type.resize(ucell->ntype);
ucell->orbital_fn.resize(ucell->ntype);
Expand All @@ -87,8 +85,6 @@ class UcellTestPrepare
ucell->magnet.ux_[2] = 0.0;
for(int it=0;it<ucell->ntype;++it)
{
ucell->atom_label[it] = this->elements[it];
ucell->atom_mass[it] = this->atomic_mass[it];
ucell->pseudo_fn[it] = this->pp_files[it];
ucell->pseudo_type[it] = this->pp_types[it];
ucell->orbital_fn[it] = this->orb_files[it];
Expand Down Expand Up @@ -148,7 +144,7 @@ class UcellTestPrepare
ucell->atoms[it].angle2.resize(ucell->atoms[it].na);
ucell->atoms[it].m_loc_.resize(ucell->atoms[it].na);
ucell->atoms[it].mbl.resize(ucell->atoms[it].na);
ucell->atoms[it].mass = ucell->atom_mass[it]; // mass set here
ucell->atoms[it].mass = this->atomic_mass[it];

for(int ia=0; ia<ucell->atoms[it].na; ++ia)
{
Expand Down
2 changes: 1 addition & 1 deletion source/source_cell/module_symmetry/symm_magnetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using namespace ModuleSymmetry;

#include "symmetry_rotation_spin.h"
#include "source_io/module_parameter/parameter.h"
#include "source_base/global_variable.h"

#include <set>
#include <vector>
Expand Down
26 changes: 24 additions & 2 deletions source/source_cell/print_cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "source_base/formatter.h"
#include "source_base/tool_title.h"
#include "source_base/global_variable.h"
#include "source_base/output.h"

namespace unitcell
{
Expand Down Expand Up @@ -98,8 +99,8 @@ namespace unitcell
for(int it=0; it<ucell.ntype; it++)
{
str += FmtCore::format("%s %8.4f %s %s\n",
ucell.atom_label[it],
ucell.atom_mass[it],
ucell.atoms[it].label,
ucell.atoms[it].mass,
ucell.pseudo_fn[it],
ucell.pseudo_type[it]);
}
Expand Down Expand Up @@ -166,4 +167,25 @@ namespace unitcell
ofs.close();
return;
}

void print_cell(const UnitCell& ucell, std::ofstream& ofs)
{
ModuleBase::GlobalFunc::OUT(ofs, "print_unitcell()");

ModuleBase::GlobalFunc::OUT(ofs, "latName", ucell.latName);
ModuleBase::GlobalFunc::OUT(ofs, "ntype", ucell.ntype);
ModuleBase::GlobalFunc::OUT(ofs, "nat", ucell.nat);
ModuleBase::GlobalFunc::OUT(ofs, "lat0", ucell.lat0);
ModuleBase::GlobalFunc::OUT(ofs, "lat0_angstrom", ucell.lat0_angstrom);
ModuleBase::GlobalFunc::OUT(ofs, "tpiba", ucell.tpiba);
ModuleBase::GlobalFunc::OUT(ofs, "omega", ucell.omega);

output::printM3(ofs, "Lattices Vector (R) : ", ucell.latvec);
output::printM3(ofs, "Supercell lattice vector : ", ucell.latvec_supercell);
output::printM3(ofs, "Reciprocal lattice Vector (G): ", ucell.G);
output::printM3(ofs, "GGT : ", ucell.GGT);

ofs << std::endl;
return;
}
}
8 changes: 8 additions & 0 deletions source/source_cell/print_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ namespace unitcell
const bool& orb = false,
const bool& dpks_desc = false,
const int& iproc = 0);

/**
* @brief Print basic unitcell information to output stream.
*
* @param ucell reference of unitcell [in]
* @param ofs output file stream [in]
*/
void print_cell(const UnitCell& ucell, std::ofstream& ofs);
}

#endif
6 changes: 3 additions & 3 deletions source/source_cell/read_atom_species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bool read_atom_species(std::ifstream& ifa,
std::getline(ifa, one_line);
std::stringstream ss;
ss << one_line;
ss >> ucell.atom_label[i] >> ucell.atom_mass[i];
ss >> ucell.atoms[i].label >> ucell.atoms[i].mass;
ucell.pseudo_fn[i] = "auto";
ucell.pseudo_type[i] = "auto";

Expand Down Expand Up @@ -73,8 +73,8 @@ bool read_atom_species(std::ifstream& ifa,
// Peize Lin test for bsse 2021.04.07
const std::string bsse_label = "empty";
ucell.atoms[i].flag_empty_element =
(search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(),
bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end())
(search( ucell.atoms[i].label.begin(), ucell.atoms[i].label.end(),
bsse_label.begin(), bsse_label.end() ) != ucell.atoms[i].label.end())
? true : false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/source_cell/read_atoms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool unitcell::read_atom_positions(UnitCell& ucell,

if (na > 0)
{
unitcell::allocate_atom_properties(ucell.atoms[it], na, ucell.atom_mass[it]);
unitcell::allocate_atom_properties(ucell.atoms[it], na);
for (int ia = 0;ia < na; ia++)
{
// modify the reading of frozen ions and velocities -- Yuanbo Li 2021/8/20
Expand Down
11 changes: 6 additions & 5 deletions source/source_cell/read_atoms_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "read_stru.h"
#include "print_cell.h"
#include "read_orb.h"
#include "cell_tools.h"
#include <cmath>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -47,7 +48,7 @@ bool validate_coordinate_system(const std::string& Coordinate,
return true;
}

void allocate_atom_properties(Atom& atom, int na, double mass)
void allocate_atom_properties(Atom& atom, int na)
{
atom.tau.resize(na, ModuleBase::Vector3<double>(0,0,0));
atom.dis.resize(na, ModuleBase::Vector3<double>(0,0,0));
Expand All @@ -61,7 +62,6 @@ void allocate_atom_properties(Atom& atom, int na, double mass)
atom.m_loc_.resize(na, ModuleBase::Vector3<double>(0,0,0));
atom.lambda.resize(na, ModuleBase::Vector3<double>(0,0,0));
atom.constrain.resize(na, ModuleBase::Vector3<int>(0,0,0));
atom.mass = mass;
}

void set_atom_movement_flags(Atom& atom, int ia,
Expand Down Expand Up @@ -138,7 +138,7 @@ bool finalize_atom_positions(UnitCell& ucell,
const std::string& esolver_type)
{
// Check if any atom can move in MD
if(!ucell.if_atoms_can_move() && calculation=="md" && esolver_type!="tddft")
if(!unitcell::if_atoms_can_move(ucell.atoms, ucell.ntype) && calculation=="md" && esolver_type!="tddft")
{
ModuleBase::WARNING("read_atoms", "no atoms can move in MD simulations!");
return false;
Expand Down Expand Up @@ -502,13 +502,14 @@ bool read_atom_type_header(int it, UnitCell& ucell,
// (1) read in atom label
// start magnetization
//=======================================
const std::string label_from_species = ucell.atoms[it].label;
ModuleBase::GlobalFunc::READ_VALUE(ifpos, ucell.atoms[it].label);

if(ucell.atoms[it].label != ucell.atom_label[it])
if(ucell.atoms[it].label != label_from_species)
{
ofs_warning << " Label orders in ATOMIC_POSITIONS and ATOMIC_SPECIES sections do not match!" << std::endl;
ofs_warning << " Label read from ATOMIC_POSITIONS is " << ucell.atoms[it].label << std::endl;
ofs_warning << " Label from ATOMIC_SPECIES is " << ucell.atom_label[it] << std::endl;
ofs_warning << " Label from ATOMIC_SPECIES is " << label_from_species << std::endl;
return false;
}
ModuleBase::GlobalFunc::OUT(ofs_running, "Atom label", ucell.atoms[it].label);
Expand Down
Loading
Loading