<commit_before>#include <sirius.h>

using namespace sirius;

void create_supercell(cmd_args& args__)
{
    matrix3d<int> scell;
    std::stringstream s(args__.value<std::string>("supercell"));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++) s >> scell(j, i);
    }
    
    std::cout << std::endl;
    std::cout << "supercell vectors (lattice coordinates) : " << std::endl;
    for (int i = 0; i < 3; i++)
    {
        std::cout << "A" << i << " : ";
        for (int j = 0; j < 3; j++) std::cout << scell(j, i) << " ";
        std::cout << std::endl;
    }

    Simulation_context ctx("sirius.json", mpi_comm_self());

    matrix3d<double> scell_lattice_vectors = ctx.unit_cell().lattice_vectors() * matrix3d<double>(scell);

    std::cout << "supercell vectors (Cartesian coordinates) : " << std::endl;
    for (int i = 0; i < 3; i++)
    {
        std::cout << "A" << i << " : ";
        for (int x = 0; x < 3; x++) std::cout << scell_lattice_vectors(x, i) << " ";
        std::cout << std::endl;
    }

    std::cout << "volume ratio : " << std::abs(matrix3d<int>(scell).det()) << std::endl;

    Simulation_context ctx_sc(mpi_comm_self());

    vector3d<double> a0, a1, a2;
    for (int x = 0; x < 3; x++)
    {
        a0[x] = scell_lattice_vectors(x, 0);
        a1[x] = scell_lattice_vectors(x, 1);
        a2[x] = scell_lattice_vectors(x, 2);
    }
    ctx_sc.unit_cell().set_lattice_vectors(a0, a1, a2);

    for (int iat = 0; iat < ctx.unit_cell().num_atom_types(); iat++)
    {
        auto label = ctx.unit_cell().atom_type(iat).label();
        ctx_sc.unit_cell().add_atom_type(label, "");
    }
   
    for (int iat = 0; iat < ctx.unit_cell().num_atom_types(); iat++)
    {
        auto label = ctx.unit_cell().atom_type(iat).label();

        for (int i = 0; i < ctx.unit_cell().atom_type(iat).num_atoms(); i++)
        {
            int ia = ctx.unit_cell().atom_type(iat).atom_id(i);
            auto va = ctx.unit_cell().atom(ia).position();

            for (int i0 = -10; i0 <= 10; i0++)
            {
                for (int i1 = -10; i1 <= 10; i1++)
                {
                    for (int i2 = -10; i2 <= 10; i2++)
                    {
                        vector3d<double> T(i0, i1, i2);
                        vector3d<double> vc = ctx.unit_cell().get_cartesian_coordinates(va + T);
                        vector3d<double> vf = ctx_sc.unit_cell().get_fractional_coordinates(vc);

                        auto vr = Utils::reduce_coordinates(vf);
                        bool add_atom = (ctx_sc.unit_cell().atom_id_by_position(vr.first) == -1);
                        //==if (add_atom && iat == 2)
                        //=={
                        //==    double r = type_wrapper<double>::random();
                        //==    if (r < 0.99) add_atom = false;
                        //==}

                        if (add_atom) ctx_sc.unit_cell().add_atom(label, vr.first);
                    }
                }
            }
        }
    }
    printf("number of atoms in the supercell: %i\n", ctx_sc.unit_cell().num_atoms());
    
    ctx_sc.unit_cell().get_symmetry();
    ctx_sc.unit_cell().print_info();
    ctx_sc.unit_cell().write_json();
    ctx_sc.unit_cell().write_cif();
}

int main(int argn, char** argv)
{
    cmd_args args;
    args.register_key("--supercell=", "{string} transformation matrix (9 numbers)");
    args.register_key("--qe", "create input for QE");

    args.parse_args(argn, argv);
    if (args.exist("help"))
    {
        printf("Usage: %s [options]\n", argv[0]);
        args.print_help();
        return 0;
    }

    sirius::initialize(1);
    if (args.exist("supercell")) create_supercell(args);


    sirius::finalize();

//==<commit_msg>small fix in unit_cell_tools<commit_after>#include <sirius.h>

using namespace sirius;

void create_supercell(cmd_args& args__)
{
    matrix3d<int> scell;
    std::stringstream s(args__.value<std::string>("supercell"));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++) s >> scell(j, i);
    }
    
    std::cout << std::endl;
    std::cout << "supercell vectors (lattice coordinates) : " << std::endl;
    for (int i = 0; i < 3; i++)
    {
        std::cout << "A" << i << " : ";
        for (int j = 0; j < 3; j++) std::cout << scell(j, i) << " ";
        std::cout << std::endl;
    }

    Simulation_context ctx("sirius.json", mpi_comm_self());

    matrix3d<double> scell_lattice_vectors = ctx.unit_cell().lattice_vectors() * matrix3d<double>(scell);

    std::cout << "supercell vectors (Cartesian coordinates) : " << std::endl;
    for (int i = 0; i < 3; i++)
    {
        std::cout << "A" << i << " : ";
        for (int x = 0; x < 3; x++) std::cout << scell_lattice_vectors(x, i) << " ";
        std::cout << std::endl;
    }

    std::cout << "volume ratio : " << std::abs(matrix3d<int>(scell).det()) << std::endl;

    Simulation_context ctx_sc(mpi_comm_self());

    vector3d<double> a0, a1, a2;
    for (int x = 0; x < 3; x++)
    {
        a0[x] = scell_lattice_vectors(x, 0);
        a1[x] = scell_lattice_vectors(x, 1);
        a2[x] = scell_lattice_vectors(x, 2);
    }
    ctx_sc.unit_cell().set_lattice_vectors(a0, a1, a2);

    for (int iat = 0; iat < ctx.unit_cell().num_atom_types(); iat++)
    {
        auto label = ctx.unit_cell().atom_type(iat).label();
        ctx_sc.unit_cell().add_atom_type(label, "");
    }
   
    for (int iat = 0; iat < ctx.unit_cell().num_atom_types(); iat++)
    {
        auto label = ctx.unit_cell().atom_type(iat).label();

        for (int i = 0; i < ctx.unit_cell().atom_type(iat).num_atoms(); i++)
        {
            int ia = ctx.unit_cell().atom_type(iat).atom_id(i);
            auto va = ctx.unit_cell().atom(ia).position();

            for (int i0 = -10; i0 <= 10; i0++)
            {
                for (int i1 = -10; i1 <= 10; i1++)
                {
                    for (int i2 = -10; i2 <= 10; i2++)
                    {
                        vector3d<double> T(i0, i1, i2);
                        vector3d<double> vc = ctx.unit_cell().get_cartesian_coordinates(va + T);
                        vector3d<double> vf = ctx_sc.unit_cell().get_fractional_coordinates(vc);

                        auto vr = Utils::reduce_coordinates(vf);
                        bool add_atom = (ctx_sc.unit_cell().atom_id_by_position(vr.first) == -1);
                        //==if (add_atom && iat == 2)
                        //=={
                        //==    double r = type_wrapper<double>::random();
                        //==    if (r < 0.99) add_atom = false;
                        //==}

                        if (add_atom) ctx_sc.unit_cell().add_atom(label, vr.first);
                    }
                }
            }
        }
    }
    printf("number of atoms in the supercell: %i\n", ctx_sc.unit_cell().num_atoms());
    
    ctx_sc.unit_cell().get_symmetry();
    ctx_sc.unit_cell().print_info();
    ctx_sc.unit_cell().write_cif();
    json dict;
    dict["unit_cell"] = ctx_sc.unit_cell().serialize();
    if (mpi_comm_world().rank() == 0) {
        std::ofstream ofs("unit_cell.json", std::ofstream::out | std::ofstream::trunc);
        ofs << dict.dump(4);
    }
}

int main(int argn, char** argv)
{
    cmd_args args;
    args.register_key("--supercell=", "{string} transformation matrix (9 numbers)");
    args.register_key("--qe", "create input for QE");

    args.parse_args(argn, argv);
    if (args.exist("help"))
    {
        printf("Usage: %s [options]\n", argv[0]);
        args.print_help();
        return 0;
    }

    sirius::initialize(1);
    if (args.exist("supercell")) create_supercell(args);


    sirius::finalize();

//==
{'fraction_non_alphanumeric': 0.17777489626556017, 'fraction_numerical': 0.01841286307053942, 'mean_word_length': 2.0235201881615055, 'pattern_counts': {'":': 0, '<': 135, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1981616', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2683, 'n_words': 533}