def setUp(self):
        super().setUp()
        self.tf_loc = os.path.join(ref_dir, "elastic_wf")
        self.struct_si = PymatgenTest.get_structure("Si")
        self.struct_si = SpacegroupAnalyzer(
            self.struct_si
        ).get_conventional_standard_structure()
        self.opt_struct = Structure.from_file(
            os.path.join(self.tf_loc, "1", "inputs", "POSCAR")
        )

        # Base WF
        self.base_wf = get_wf(
            self.struct_si, "optimize_only.yaml", params=[{"db_file": ">>db_file<<"}]
        )
        self.base_wf.append_wf(
            get_wf_elastic_constant(
                self.struct_si,
                db_file=">>db_file<<",
                stencils=[[0.01]] * 3 + [[0.03]] * 3,
                copy_vasp_outputs=True,
            ),
            self.base_wf.leaf_fw_ids,
        )
        self.base_wf_noopt = get_wf_elastic_constant(
            self.opt_struct,
            stencils=[[0.01]] * 3 + [[0.03]] * 3,
            copy_vasp_outputs=False,
            sym_reduce=False,
            db_file=">>db_file<<",
        )
        ec_incar_update = {"incar_update": {"EDIFF": 1e-6, "ENCUT": 700}}
        self.base_wf = add_modify_incar(self.base_wf, ec_incar_update)
        self.base_wf_noopt = add_modify_incar(self.base_wf_noopt, ec_incar_update)

        # Full preset WF
        self.preset_wf = wf_elastic_constant(self.struct_si)

        # Minimal WF
        self.minimal_wf = wf_elastic_constant_minimal(self.opt_struct)
        self.minimal_wf = add_modify_incar(self.minimal_wf, ec_incar_update)

        # TOEC WF (minimal)
        self.toec_wf = wf_elastic_constant_minimal(self.struct_si, order=3)
        self.toec_wf = add_modify_incar(self.toec_wf, ec_incar_update)
        toec_data = loadfn(os.path.join(self.tf_loc, "toec_data.json"))
        # Rather than run entire workflow, preload the spec to test the analysis
        toec_analysis = Firework(
            [
                ElasticTensorToDb(
                    structure=self.struct_si, order=3, db_file=">>db_file<<"
                )
            ],
            spec={"deformation_tasks": toec_data["deformation_tasks"]},
        )
        self.toec_analysis = Workflow([toec_analysis])
        # Check 4th order to see if constructed correctly
        self.foec_wf = wf_elastic_constant_minimal(self.struct_si, order=4)
    def setUp(self):
        super(TestElasticWorkflow, self).setUp()
        self.tf_loc = os.path.join(ref_dir, 'elastic_wf')
        self.struct_si = PymatgenTest.get_structure("Si")
        self.struct_si = SpacegroupAnalyzer(
            self.struct_si).get_conventional_standard_structure()
        self.opt_struct = Structure.from_file(
            os.path.join(self.tf_loc, '1', 'inputs', 'POSCAR'))

        # Base WF
        self.base_wf = get_wf(self.struct_si,
                              "optimize_only.yaml",
                              params=[{
                                  "db_file": ">>db_file<<"
                              }])
        self.base_wf.append_wf(
            get_wf_elastic_constant(self.struct_si,
                                    db_file='>>db_file<<',
                                    stencils=[[0.01]] * 3 + [[0.03]] * 3,
                                    copy_vasp_outputs=True),
            self.base_wf.leaf_fw_ids)
        self.base_wf_noopt = get_wf_elastic_constant(self.opt_struct,
                                                     stencils=[[0.01]] * 3 +
                                                     [[0.03]] * 3,
                                                     copy_vasp_outputs=False,
                                                     sym_reduce=False,
                                                     db_file='>>db_file<<')
        ec_incar_update = {'incar_update': {'EDIFF': 1e-6, 'ENCUT': 700}}
        self.base_wf = add_modify_incar(self.base_wf, ec_incar_update)
        self.base_wf_noopt = add_modify_incar(self.base_wf_noopt,
                                              ec_incar_update)

        # Full preset WF
        self.preset_wf = wf_elastic_constant(self.struct_si)

        # Minimal WF
        self.minimal_wf = wf_elastic_constant_minimal(self.opt_struct)
        self.minimal_wf = add_modify_incar(self.minimal_wf, ec_incar_update)

        # TOEC WF (minimal)
        self.toec_wf = wf_elastic_constant_minimal(self.struct_si, order=3)
        self.toec_wf = add_modify_incar(self.toec_wf, ec_incar_update)
        toec_data = loadfn(os.path.join(self.tf_loc, 'toec_data.json'))
        # Rather than run entire workflow, preload the spec to test the analysis
        toec_analysis = Firework(
            [
                ElasticTensorToDb(
                    structure=self.struct_si, order=3, db_file=">>db_file<<")
            ],
            spec={"deformation_tasks": toec_data['deformation_tasks']})
        self.toec_analysis = Workflow([toec_analysis])
Beispiel #3
0
def get_wf_elastic_constant(
    structure,
    strain_states=None,
    stencils=None,
    db_file=None,
    conventional=False,
    order=2,
    vasp_input_set=None,
    analysis=True,
    sym_reduce=False,
    tag="elastic",
    copy_vasp_outputs=False,
    **kwargs,
):
    """
    Returns a workflow to calculate elastic constants.

    Firework 1: write vasp input set for structural relaxation,
                run vasp,
                pass run location,
                database insertion.

    Firework 2 - number of total deformations: static runs on the deformed structures

    last Firework: analyze stress/strain data and fit the elastic tensor

    Args:
        structure (Structure): input structure to be optimized and run.
        strain_states (list of Voigt-notation strains): list of ratios of nonzero elements
            of Voigt-notation strain, e.g. [(1, 0, 0, 0, 0, 0), (0, 1, 0, 0, 0, 0), etc.].
        stencils (list of floats, or list of list of floats): values of strain to multiply
            by for each strain state, i.e. stencil for the perturbation along the strain
            state direction, e.g. [-0.01, -0.005, 0.005, 0.01].  If a list of lists,
            stencils must correspond to each strain state provided.
        db_file (str): path to file containing the database credentials.
        conventional (bool): flag to convert input structure to conventional structure,
            defaults to False.
        order (int): order of the tensor expansion to be determined.  Defaults to 2 and
            currently supports up to 3.
        vasp_input_set (VaspInputSet): vasp input set to be used.  Defaults to static
            set with ionic relaxation parameters set.  Take care if replacing this,
            default ensures that ionic relaxation is done and that stress is calculated
            for each vasp run.
        analysis (bool): flag to indicate whether analysis task should be added and
            stresses and strains passed to that task.
        sym_reduce (bool): whether or not to apply symmetry reductions.
        tag (str): tag for the fireworks.
        copy_vasp_outputs (bool): whether or not to copy previous vasp outputs.
        kwargs (keyword arguments): additional kwargs to be passed to get_wf_deformations.

    Returns:
        Workflow
    """
    # Convert to conventional if specified
    if conventional:
        structure = SpacegroupAnalyzer(
            structure).get_conventional_standard_structure()

    uis_elastic = {"IBRION": 2, "NSW": 99, "ISIF": 2, "ISTART": 1}
    vis = vasp_input_set or MPStaticSet(structure,
                                        user_incar_settings=uis_elastic)
    strains = []
    if strain_states is None:
        strain_states = get_default_strain_states(order)
    if stencils is None:
        stencils = [np.linspace(-0.01, 0.01, 5 +
                                (order - 2) * 2)] * len(strain_states)
    if np.array(stencils).ndim == 1:
        stencils = [stencils] * len(strain_states)
    for state, stencil in zip(strain_states, stencils):
        strains.extend(
            [Strain.from_voigt(s * np.array(state)) for s in stencil])

    # Remove zero strains
    strains = [strain for strain in strains if not (abs(strain) < 1e-10).all()]
    vstrains = [strain.voigt for strain in strains]
    if np.linalg.matrix_rank(vstrains) < 6:
        # TODO: check for sufficiency of input for nth order
        raise ValueError(
            "Strain list is insufficient to fit an elastic tensor")

    deformations = [s.get_deformation_matrix() for s in strains]

    if sym_reduce:
        # Note this casts deformations to a TensorMapping
        # with unique deformations as keys to symmops
        deformations = symmetry_reduce(deformations, structure)

    wf_elastic = get_wf_deformations(
        structure,
        deformations,
        tag=tag,
        db_file=db_file,
        vasp_input_set=vis,
        copy_vasp_outputs=copy_vasp_outputs,
        **kwargs,
    )
    if analysis:
        defo_fws_and_tasks = get_fws_and_tasks(
            wf_elastic,
            fw_name_constraint="deformation",
            task_name_constraint="Transmuted",
        )
        for idx_fw, idx_t in defo_fws_and_tasks:
            defo = wf_elastic.fws[idx_fw].tasks[idx_t][
                "transformation_params"][0]["deformation"]
            pass_dict = {
                "strain": Deformation(defo).green_lagrange_strain.tolist(),
                "stress": ">>output.ionic_steps.-1.stress",
                "deformation_matrix": defo,
            }
            if sym_reduce:
                pass_dict.update({"symmops": deformations[defo]})

            mod_spec_key = f"deformation_tasks->{idx_fw}"
            pass_task = pass_vasp_result(pass_dict=pass_dict,
                                         mod_spec_key=mod_spec_key)
            wf_elastic.fws[idx_fw].tasks.append(pass_task)

        fw_analysis = Firework(
            ElasticTensorToDb(structure=structure,
                              db_file=db_file,
                              order=order,
                              fw_spec_field="tags"),
            name="Analyze Elastic Data",
            spec={"_allow_fizzled_parents": True},
        )
        wf_elastic.append_wf(Workflow.from_Firework(fw_analysis),
                             wf_elastic.leaf_fw_ids)

    wf_elastic.name = "{}:{}".format(structure.composition.reduced_formula,
                                     "elastic constants")

    return wf_elastic