簡體   English   中英

如何在 C++ 中更改特定的坐標向量?

[英]How to change a particular coordinate vector in C++?

這是我正在處理的代碼:

#include <inq/inq.hpp>
#include <iostream>
#include <fstream>
#include <input/parse_xyz.hpp>

int main(int argc, char **argv) {
  using namespace inq;
  using namespace inq::magnitude;

  input::environment env(argc, argv);
  auto comm = boost::mpi3::environment::get_world_instance();

  auto atoms = input::parse_xyz("zr.xyz");
  //atoms.push_back("Zr" | input::coord(4.8585, 7.01264, 5.172));

  auto box = input::cell::orthorhombic(9.717_A, 11.22023_A, 5.172_A);

  systems::ions ions(box, atoms);
  systems::electrons electrons(comm, ions, input::basis::cutoff_energy(30.0_Ha));
  ground_state::initial_guess(ions, electrons);

  auto result = ground_state::calculate(ions, electrons, input::interaction::pbe(), input::scf::scf_steps(200));

    double const d_0   = 9.77366;
    double const del   = 0.3257886667;
    int    const n_max = 30;


    std::ofstream ofs{"Zr_e_vs_d5.dat"};

    for(int n = 0; n != n_max; ++n){
        double const distance = d_0 - n*del;
        atoms.push_back("Zr" | input::coord(9.18123, 13.2519, distance));
        auto result = ground_state::calculate(ions, electrons, input::interaction::pbe(), input::scf::scf_steps(15));
        //ofs << distance << '\t' << zr_energy(distance) << '\n';
    ofs << distance << '\t' << result.energy.total() << '\n';
    }
 }

我從 .xyz 文件中讀取了 50 個原子坐標。 然后我將一個額外的原子推入下面循環中的向量中。 我想改變原子的最后一個坐標(Z坐標),以模擬在Z方向移動的原子。 我對 C++ 很陌生,無法弄清楚如何更改該特定坐標。 請幫忙

這是供參考的輸入 .xyz 文件:

Zr    0.000000    1.870038    2.586000
Zr    9.717000    1.870038    2.586000
Zr    1.619500    4.675094    2.586000
Zr    0.000000    0.000000    0.000000

在向量中查找特定元素的一般方法是使用find() 您需要在文件中包含<algorithm>

auto ref = find(v.begin(), v.end(), element);

如果 ref 不在向量末尾的下一個,這意味着找到了元素,則對應的索引將是:

if(ref != v.end()){
    int index = ref - v.begin();
}

現在您可以像這樣訪問和更改 vector 中索引處的元素:

v.at(index) = newCoOrdinate;

那會做你所要求的。

查看庫,似乎inq不是一個特別受歡迎的庫,文檔很多,因此很可能有一個特定的 api 可以完全滿足您的需求。

但是,您應該能夠使用以下方法更新 z 坐標:

for(auto & atom : atoms)
{
    atom.position += math::vector3<int>({0, 0, z_movement_distance});
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM