示例#1
0
def TI_sampling(in_gromos_system: Gromos_System,
                project_dir: str,
                step_name="lambda_sampling",
                lambda_values: List[float] = np.arange(0, 1.1, 0.1),
                subSystem: _SubmissionSystem = LOCAL(),
                n_simulation_repetitions: int = 3,
                n_equilibrations: int = 1):

    work_dir = bash.make_folder(project_dir + "/" + step_name)
    in_gromos_system.save(work_dir)
    sys_path = in_gromos_system.save(
        work_dir + "/" + step_name +
        "_input.obj")  #Ugly workaround for deepcopy
    general_system_name_prefix = in_gromos_system.name

    lam_systems = []
    for lam in lambda_values:
        lam = np.round(lam, 2)

        lam_system = Gromos_System.load(sys_path)
        lam_system.name = general_system_name_prefix + "_l_" + str(lam)
        lam_system.work_folder = work_dir

        # IMD
        ## Pertubation
        ###Pertubation of system.
        pert_block = PERTURBATION(NTG=1,
                                  NRDGL=0,
                                  RLAM=lam,
                                  DLAMT=0,
                                  ALPHC=0.5,
                                  ALPHLJ=0.5,
                                  NLAM=2,
                                  NSCALE=0)
        lam_system.imd.add_block(block=pert_block)

        ###Calculate additional lambda points for end-states
        if (not hasattr(lam_system, "PRECALCLAM")):
            precalc_lam_block = PRECALCLAM(NRLAM=2, MINLAM=0, MAXLAM=1)
            lam_system.imd.add_block(block=precalc_lam_block)

        # Submit
        out_gromos_system, jobID = _TI_lam_step(
            in_gromos_system=lam_system,
            project_dir=work_dir,
            step_name=lam_system.name,
            submission_system=subSystem,
            simulation_runs=n_simulation_repetitions,
            equilibration_runs=n_equilibrations)

        out_gromos_system.save(out_gromos_system.work_folder +
                               "/sd_out_system.obj")
        lam_systems.append(out_gromos_system)

    return lam_system, jobID
 def test_rebase(self):
     new_base = bash.make_folder(tmp_test_dir + "/rebase")
     self.subSys_files.work_folder = new_base
     self.subSys_files.rebase_files()
示例#3
0
def chain_submission(
    simSystem: Gromos_System,
    out_dir_path: str,
    out_prefix: str,
    chain_job_repetitions: int,
    worker_script: str,
    job_submission_system: _SubmissionSystem,
    jobname: str,
    run_analysis_script_every_x_runs: int = 0,
    in_analysis_script_path: str = "",
    start_run_index: int = 1,
    prefix_command: str = "",
    previous_job_ID: int = None,
    work_dir: str = None,
    initialize_first_run: bool = True,
    reinitialize_every_run: bool = False,
    verbose: bool = False,
    verbose_lvl: int = 1,
) -> Tuple[int, str, Gromos_System]:
    """
        this function submits a chain of simulation steps to the queuing system and does the file managment.


    Parameters
    ----------
    simSystem : Gromos_System
        simulation system
    out_dir_path : str
        out directory path
    out_prefix : str
        out prefix for simulation files
    chain_job_repetitions : int
        how often, should the simulation be repeated (in continuation)
    worker_script : str
        worker, that should be submitted. This script will be executed at each scheduled job.
    job_submission_system : _SubmissionSystem
        submission system, what type of submission?
    jobname : str
        name of the simulation job
    run_analysis_script_every_x_runs : int, optional
        run analysis in between - (careful will not be overwritten, make sure final analysis is correct.), by default 0
    in_analysis_script_path : str, optional
        analysis script for simulation, that should be applied (will at least be applied after the full simulation chain.), by default ""
    start_run_index : int, optional
        start index of the job chain., by default 1
    prefix_command : str, optional
        any bash prefix commands, before submitting?, by default ""
    previous_job_ID : int, optional
        ID of the prefious job, to be chained to. , by default None
    work_dir : str, optional
        dir to wich the work in progress will be written. if None a tmp-srcatch dir will be used with LSF!, by default None
    initialize_first_run : bool, optional
        should the velocities for the first run be initialized?, by default True
    reinitialize_every_run : bool, optional
        should in every run, the velocities be reinitialized?, by default False
    verbose : bool, optional
        more bla bla, by default False
    verbose_lvl : int, optional
        nicely define ammount of bla bla, by default 1

    Returns
    -------
    Tuple[int, str, Gromos_System]
        Tuple[previous_job_ID, tmp_jobname, simSystem]
        will return the last job_ID, the last tmp_jobname and the final gromosSystem.

    Raises
    ------
    ValueError
        if submission fails. This can habe various reasons, always check also the present files! (*omd etc.)
    """

    if verbose:
        print("\nChainSubmission - " + out_prefix + "\n" + "=" * 30 + "\n")
    if (verbose) and verbose_lvl >= 2:
        print("start_run_index " + str(start_run_index))
    if (verbose) and verbose_lvl >= 2:
        print("job reptitions " + str(chain_job_repetitions))

    if job_submission_system is not LOCAL:
        simSystem._future_promise = True

    ana_id = None
    job_submission_system.job_duration = job_submission_system.job_duration
    for runID in range(start_run_index, chain_job_repetitions + 1):

        if verbose:
            print("\n submit  " + jobname + "_" + str(runID) + "\n" + spacer3)

        tmp_outprefix = out_prefix + "_" + str(runID)
        tmp_jobname = jobname + "_" + str(runID)
        tmp_outdir = out_dir_path + "/" + tmp_outprefix
        tmp_out_cnf = tmp_outdir + "/" + tmp_outprefix + ".cnf"

        # Checks if run should be skipped!
        do_skip, previous_job_ID = do_skip_job(
            tmp_out_cnf=tmp_out_cnf,
            simSystem=simSystem,
            tmp_jobname=tmp_jobname,
            job_submission_system=job_submission_system,
            previous_job=previous_job_ID,
            verbose=verbose,
        )

        if not do_skip:
            bash.make_folder(tmp_outdir)

            # build COMMANDS:
            if len(prefix_command) > 1:
                prefix_command += " && "

            # We will write the arguments to the python script in a bash array
            # to make it simpler to read in our input files.

            md_args = "md_args=(\n"

            md_args += "-out_dir " + tmp_outdir + "\n"
            md_args += "-in_cnf_path " + simSystem.cnf.path + "\n"
            md_args += "-in_imd_path " + simSystem.imd.path + "\n"
            md_args += "-in_top_path " + simSystem.top.path + "\n"
            md_args += "-runID " + str(runID) + "\n"

            # OPTIONAL ARGS
            if simSystem.disres is not None:
                md_args += "-in_disres_path " + simSystem.disres.path + "\n"
            if simSystem.ptp is not None:
                md_args += "-in_perttopo_path " + simSystem.ptp.path + "\n"
            if simSystem.refpos is not None:
                md_args += "-in_refpos_path " + simSystem.refpos.path + "\n"
            if simSystem.qmmm is not None:
                md_args += "-in_qmmm_path " + simSystem.qmmm.path + " "
            if simSystem.posres is not None:
                md_args += "-in_posres_path " + simSystem.posres.path + "\n"

            md_args += "-nmpi " + str(job_submission_system.nmpi) + "\n"
            md_args += "-nomp " + str(job_submission_system.nomp) + "\n"
            md_args += "-initialize_first_run " + str(
                initialize_first_run) + "\n"
            md_args += "-reinitialize_every_run " + str(
                reinitialize_every_run) + "\n"
            md_args += "-gromosXX_bin_dir " + str(
                simSystem.gromosXX.bin) + "\n"
            md_args += "-gromosXX_check_binary_paths " + str(
                simSystem.gromosXX._check_binary_paths) + "\n"

            if work_dir is not None:
                md_args += "-work_dir " + str(work_dir) + "\n"

            if hasattr(simSystem.imd, "WRITETRAJ"):
                if simSystem.imd.WRITETRAJ.NTWX > 0:
                    md_args += "-out_trc " + str(True) + "\n"
                if simSystem.imd.WRITETRAJ.NTWE > 0:
                    md_args += "-out_tre " + str(True) + "\n"
                if simSystem.imd.WRITETRAJ.NTWV > 0:
                    md_args += "-out_trv " + str(True) + "\n"
                if simSystem.imd.WRITETRAJ.NTWF > 0:
                    md_args += "-out_trf " + str(True) + "\n"
                if simSystem.imd.WRITETRAJ.NTWG > 0:
                    md_args += "-out_trg " + str(True) + "\n"

            md_args += "-zip_trajectories " + str(
                job_submission_system.zip_trajectories) + "\n"

            md_args += ")\n"  # closing the bash array which stores all arguments.

            # add zip option here

            # MAIN commands
            md_script_command = prefix_command + "\n\n" + md_args + "\n"
            md_script_command += "python3 " + worker_script + '  "${md_args[@]}" \n'

            if verbose:
                print("PREVIOUS ID: ", previous_job_ID)
                if verbose_lvl >= 2:
                    print("COMMAND: ", md_script_command)

            # SCHEDULE THE COMMANDS
            try:
                if verbose:
                    print("\tSIMULATION")
                os.chdir(tmp_outdir)
                sub_job = Submission_job(
                    command=md_script_command,
                    jobName=tmp_jobname,
                    submit_from_dir=tmp_outdir,
                    queue_after_jobID=previous_job_ID,
                    outLog=tmp_outdir + "/" + out_prefix + "_md.out",
                    errLog=tmp_outdir + "/" + out_prefix + "_md.err",
                    sumbit_from_file=True,
                )
                previous_job_ID = job_submission_system.submit_to_queue(
                    sub_job)
                if verbose:
                    print("SIMULATION ID: ", previous_job_ID)
            except ValueError as err:  # job already in the queue
                raise ValueError("ERROR during submission of main job " +
                                 str(tmp_jobname) + ":\n" +
                                 "\n".join(err.args))

            # OPTIONAL schedule - analysis inbetween.
            if (runID > 1 and run_analysis_script_every_x_runs != 0
                    and runID % run_analysis_script_every_x_runs == 0
                    and runID < chain_job_repetitions):

                if (verbose) and verbose_lvl >= 2:
                    print("\tINBETWEEN ANALYSIS")
                sub_job = Submission_job(
                    command=in_analysis_script_path,
                    jobName=jobname + "_intermediate_ana_run_" + str(runID),
                    outLog=tmp_outdir + "/" + out_prefix + "_inbetweenAna.out",
                    errLog=tmp_outdir + "/" + out_prefix + "_inbetweenAna.err",
                    queue_after_jobID=previous_job_ID,
                )
                try:
                    ana_id = job_submission_system.submit_to_queue(sub_job)
                    if (verbose) and verbose_lvl >= 2:
                        print("\n")
                except ValueError as err:  # job already in the queue
                    print("ERROR during submission of analysis command of " +
                          sub_job.jobName + ":\n")
                    print("\n".join(err.args))
        else:
            if (verbose) and verbose_lvl >= 2:
                print("Did not submit!")
        if (verbose) and verbose_lvl >= 2:
            print("\n")
        if (verbose) and verbose_lvl >= 2:
            print("job_postprocess ")
        prefix_command = ""

        # Resulting cnf is provided to use it in further approaches.
        simSystem.cnf = Cnf(tmp_out_cnf, _future_file=True)

    if ana_id is not None:
        previous_job_ID = ana_id

    return previous_job_ID, tmp_jobname, simSystem
示例#4
0
def do(
    in_simulation_dir: str,
    out_analysis_dir: str,
    sim_prefix: str,  # in_system:Gromos_System,
    n_processes: int = 1,
    control_dict: dict = None,
    verbose: bool = True,
):
    """
        This function is a analysis framework structure, that starts an analysis folder containing a data folder with all concatenated files, from which analysis can be started.

    Parameters
    ----------
    in_simulation_dir : str
        input simulation directory (with succesfully finished simulations)
    out_analysis_dir : str
        output directory
    sim_prefix : str
        prefix of the simulation == name of simulation
    n_processes : int, optional
                WARNING: parallelization is currently not implemented!, by default 1
    control_dict : dict, optional
        control structure, steering the executions, by default None
    verbose : bool, optional
        bla bla, by default True

    """

    if not os.path.exists(out_analysis_dir) and not os.path.isdir(
            out_analysis_dir):
        bash.make_folder(out_analysis_dir)
    if not isinstance(control_dict, dict):
        warnings.warn(
            "Recieved a non dict control-dict! Using the template. \n\tGot:" +
            str(control_dict))
        control_dict = template_control_dict

    #   ORGANIZE FILES
    out_data_dir = out_analysis_dir + "/data"
    if not os.path.isdir(out_data_dir):
        os.mkdir(out_data_dir)

    if control_dict["concat"]["do"]:
        project_concatenation(
            in_folder=in_simulation_dir,
            in_prefix=sim_prefix,
            out_folder=out_data_dir,
            control_dict=control_dict["concat"]["sub"],
            verbose=verbose,
        )
        # in_simSystem=in_system,
    # Other Analysis parts

    print(control_dict)
    if control_dict["simulation_folder"]["do"]:
        sub_dict = control_dict["simulation_folder"]["sub"]
        if os.path.exists(in_simulation_dir):
            if sub_dict["tar"]:
                out_tar_dir = bash.compress_tar(in_path=in_simulation_dir)
                if verbose:
                    print("Tarred simulation folder: " + str(out_tar_dir))
                bash.remove_file(in_simulation_dir, additional_options="-r")

            if sub_dict["remove"]:
                bash.remove_file(in_simulation_dir, additional_options="-r")
        else:
            warnings.warn(
                "Simulation dir was not present. Skipped Compression.\nGiven Path: "
                + in_simulation_dir)
示例#5
0
def work(
    out_dir: str,
    in_cnf_path: str,
    in_imd_path: str,
    in_top_path: str,
    runID: int = 1,
    in_perttopo_path: str = None,
    in_disres_path: str = None,
    in_posres_path: str = None,
    in_refpos_path: str = None,
    in_qmmm_path: str = None,
    out_trc: bool = False,
    out_tre: bool = False,
    out_trg: bool = False,
    out_trv: bool = False,
    out_trf: bool = False,
    out_trs: bool = False,
    nmpi: int = 1,
    nomp: int = 1,
    reinitialize_every_run: bool = False,
    initialize_first_run: bool = True,
    gromosXX_bin_dir: str = None,
    gromosXX_check_binary_paths: bool = True,
    work_dir: str = None,
    zip_trajectories: bool = True,
    **kwargs,
):
    """
    Executed by repex_EDS_long_production_run as workers

    Parameters
    ----------
    out_dir : str
         final output dir
    in_cnf_path : str
        input coordinates
    in_imd_path : str
        input imd-parameter file
    in_top_path : str
        input topology
    in_perttopo_path : str
        input pertubation
    in_disres_path : str
        input disres
    in_qmmm_path : str
        input qmmm
    nmpi : int, optional
        number of mpi cores (def.=1)
    nomp : int, optional
        number of omp cores (def.= 1)
    out_trg : bool, optional
        True if trg shall be written out.
    out_trv : bool, optional
        True if trv shall be written out.
    out_trf : bool, optional
        True if trf shall be written out.
    out_trs : bool, optional
        True if trs shall be written out.
    gromosXX_bin_dir : str, optional
         path to gromos binary directory
    work_dir : str, optional
         work directory
    zip_trajectories: bool
        determines whether trajectories are zipped

    Returns
    -------
    int
        return number

    """
    time.sleep(time_wait_s_for_filesystem)
    # WORKDIR SetUP
    if work_dir is None or work_dir == "None":
        if "TMPDIR" in os.environ:
            work_dir = os.environ["TMPDIR"]
        else:
            print("Could not find TMPDIR!\n Switched to outdir for work")
            work_dir = out_dir
            if not os.path.isdir(work_dir):
                bash.make_folder(work_dir)
    elif isinstance(work_dir, str) and work_dir != "None":
        if work_dir == "out_dir":
            work_dir = out_dir
            if not os.path.isdir(work_dir):
                bash.make_folder(work_dir)
        else:
            if not os.path.isdir(work_dir):
                bash.make_folder(work_dir)
    else:
        raise ValueError(f"work_dir is not a valid path, work_dir: {work_dir}")

    print("workDIR: " + str(work_dir))

    # Check if the calculation is running on multiple nodes:
    if "LSB_HOSTS" in os.environ:
        hosts = os.environ["LSB_HOSTS"].split()
    else:
        hosts = []
    multi_node = True if len(hosts) > 1 else False

    # run a euler script to create tmpdir on all nodes
    if multi_node:
        os.system("remote_tmpdir create")

    os.chdir(work_dir)

    # Prepare IMD file:
    tmp_prefix = os.path.basename(out_dir)
    tmp_imd_path = out_dir + "/" + tmp_prefix + ".imd"
    imd_file = imd.Imd(in_imd_path)
    cnf_file = cnf.Cnf(in_cnf_path)

    # check init_block - if specified!
    # What kind of simulation
    is_stochastic_dynamics_sim = False
    is_vacuum = False

    if imd_file.BOUNDCOND.NTB == 0:
        is_vacuum = True

    if hasattr(imd_file, "STOCHDYN"):
        if imd_file.STOCHDYN.NTSD:
            is_stochastic_dynamics_sim = True

    # Adapt Initializations:
    if reinitialize_every_run or (initialize_first_run and runID == 1):
        imd_file.INITIALISE.NTIVEL = 1

        if hasattr(imd_file, "CONSTRAINT"):
            imd_file.INITIALISE.NTISHK = 0 if (
                imd_file.CONSTRAINT.NTC > 0) else 1

        if hasattr(imd_file, "MULTIBATH"):
            imd_file.INITIALISE.NTINHT = 0 if (
                imd_file.MULTIBATH.ALGORITHM <= 1) else 1

        imd_file.INITIALISE.NTISHI = 0 if (hasattr(cnf_file,
                                                   "LATTICESHIFTS")) else 1

        imd_file.INITIALISE.NTIRTC = 0
        imd_file.INITIALISE.NTICOM = 0
        imd_file.INITIALISE.NTISTI = 0

        if is_stochastic_dynamics_sim:
            imd_file.INITIALISE.NTISTI = 1

    elif initialize_first_run and runID > 1:
        imd_file.INITIALISE.NTIVEL = 0
        imd_file.INITIALISE.NTISHI = 0
        imd_file.INITIALISE.NTISHK = 0
        imd_file.INITIALISE.NTINHT = 0
        imd_file.INITIALISE.NTINHB = 0
        imd_file.INITIALISE.NTIRTC = 0
        imd_file.INITIALISE.NTICOM = 0
        imd_file.INITIALISE.NTISTI = 0

        if is_stochastic_dynamics_sim or is_vacuum:
            imd_file.INITIALISE.NTISTI = 1

    # adjust sim time if continuation:
    if runID > 1:
        imd_file.STEP.T = imd_file.STEP.T + (imd_file.STEP.NSTLIM *
                                             imd_file.STEP.DT) * (runID - 1)

    # Write out:
    tmp_imd_path = imd_file.write(tmp_imd_path)

    # RUN
    print("input: ", type(gromosXX_bin_dir), gromosXX_bin_dir)
    gromosXX = mdGromosXX.GromosXX(
        gromosXX_bin_dir=gromosXX_bin_dir,
        _check_binary_paths=gromosXX_check_binary_paths)
    print("gromosXX._bin: ", type(gromosXX._bin), gromosXX._bin)
    print("gromosXX.bin: ", type(gromosXX.bin), gromosXX.bin)

    try:
        print(spacer + "\n start MD " + str(os.path.basename(tmp_imd_path)) +
              "\n")

        # TODO: This is a stupid workaround as Euler tends to place nans in the euler angles, that should not be there!
        if hasattr(cnf_file, "GENBOX") and any(
            [math.isnan(x) for x in cnf_file.GENBOX.euler]):
            cnf_file.GENBOX.euler = [0.0, 0.0, 0.0]
            cnf_file.write(in_cnf_path)
        try:
            omd_file_path = gromosXX.md_run(
                in_topo_path=in_top_path,
                in_coord_path=in_cnf_path,
                in_imd_path=tmp_imd_path,
                in_pert_topo_path=in_perttopo_path,
                in_disres_path=in_disres_path,
                in_posresspec_path=in_posres_path,
                in_refpos_path=in_refpos_path,
                in_qmmm_path=in_qmmm_path,
                nmpi=nmpi,
                nomp=nomp,
                out_prefix=tmp_prefix,
                out_tre=out_tre,
                out_trc=out_trc,
                out_trg=out_trg,
                out_trs=out_trs,
                out_trf=out_trf,
                out_trv=out_trv,
                verbose=True,
            )

            print("Waiting to find: ", omd_file_path.replace(".omd", ".cnf"))
            bash.wait_for_fileSystem(omd_file_path.replace(".omd", ".cnf"))

            md_failed = False
        except Exception as err:
            print("Failed! process returned: \n Err: \n" + "\n".join(err.args))
            md_failed = True
            traceback.print_exc()
            raise err

        # zip the files after the simulation.
        n_cpu_zip = nmpi if nmpi >= nomp else nomp
        if not multi_node and zip_trajectories:
            zip_files.do(in_simulation_dir=work_dir, n_processes=n_cpu_zip)

        # Copy the files back to the proper directory when calc occured on scratch
        if out_dir != work_dir:
            if not multi_node:
                bash.move_file(work_dir + "/*", out_dir)
            else:
                for host in hosts:
                    command = "ssh " + host + '  "mv ${TMPDIR}/* ' + out_dir + '"'
                    os.system(command)
            os.system(
                "remote_tmpdir delete")  # Works for both multi or single node

        # Note: If job is multi-node, it is simpler to zip things in out_dir after copying back
        if multi_node and zip_trajectories:
            zip_files.do(in_simulation_dir=out_dir, n_processes=n_cpu_zip)

    except Exception as err:
        print("\nFailed during simulations: ", file=sys.stderr)
        print(type(err), file=sys.stderr)
        print(err.args, file=sys.stderr)
        print("GROMOS OUTPUT:")

        omd_outs = glob.glob(out_dir + "/" + tmp_prefix + "*.omd")
        if len(omd_outs) > 1:
            omd_file_content = open(omd_outs[0], "r").read_lines()
            if len(omd_file_content) > 1:
                print("\t" + "\n\t".join(omd_file_content))
            else:
                print("\t None")

        exit(1)
    if md_failed:
        print("\nFailed during simulations: \n Checkout: \n " +
              str(tmp_prefix) + ".omd",
              file=sys.stderr)
        exit(1)
    exit(0)
示例#6
0
def simulation(in_gromos_system: Gromos_System,
               project_dir: str,
               step_name: str = "sim",
               in_imd_path=None,
               submission_system: _SubmissionSystem = LOCAL(),
               simulation_runs: int = 1,
               equilibration_runs: int = 0,
               previous_simulation_run: int = None,
               nmpi: int = 1,
               force_simulation: bool = False,
               analysis_script: callable = simulation_analysis.do,
               analysis_control_dict: dict = None,
               verbose: bool = False) -> (Gromos_System, int):
    #PREPERATIONS
    try:
        try:
            step_dir = project_dir + "/" + step_name
            bash.make_folder(step_dir)

            out_input_dir = step_dir + "/input"
            out_simulation_dir = step_dir + "/simulation"
            out_analysis_dir = step_dir + "/analysis"
            bash.make_folder(out_input_dir)

            ##Prepare gromos system:
            in_gromos_system = copy.deepcopy(
                in_gromos_system)  #don't mutate outer systems
            in_gromos_system.work_folder = out_input_dir
            in_gromos_system.name = step_name
            if (not in_imd_path is None and in_gromos_system.imd.path is None):
                in_gromos_system.imd = in_imd_path
                in_gromos_system.adapt_imd()
            elif (not (in_gromos_system.cnf._future_file
                       or in_gromos_system.imd._future_file)):
                in_gromos_system.adapt_imd()

            out_analysis_cnf = out_analysis_dir + "/data/" + in_gromos_system.name + ".cnf"

            print(spacer)
            print(step_name)
            print(spacer)

            #Write out, all non promised files
            #TODO: REMOVE - print(in_gromos_system.all_file_paths)
            in_gromos_system._update_all_file_paths()
            in_gromos_system.write_files()
            #TODO: REMOVE - print(in_gromos_system.all_file_paths)

            # %%
            ##Write Out Ana Script
            # IN args
            in_analysis_control_dict = analysis_control_dict
            n_analysis_processors = 1  #Maybe in the future: 5 if(nmpi>5) else 1
            verbose = verbose
        except Exception as err:
            raise Exception("Could not prepare the gromos System\n\t" +
                            "\n\t".join(map(str, err.args)))
        # do
        analysis_control_dict = simulation_analysis.template_control_dict if (
            in_analysis_control_dict is None) else in_analysis_control_dict

        analysis_vars = OrderedDict({
            "in_simulation_dir":
            out_simulation_dir,
            "sim_prefix":
            in_gromos_system.name,
            "out_analysis_dir":
            out_analysis_dir,
            "gromosPP_bin_dir":
            in_gromos_system.gromosPP._bin,
            "control_dict":
            analysis_control_dict,
            "n_processes":
            n_analysis_processors,
            "verbose":
            verbose,
        })
        try:
            in_analysis_script_path = utils.write_job_script(
                out_script_path=step_dir + "/job_analysis.py",
                target_function=analysis_script,
                variable_dict=analysis_vars)
        except Exception as err:
            raise Exception("Could not prepare the analysis script\n\t" +
                            "\n\t".join(map(str, err.args)))

        ##Write Out schedulling Script
        ###Build analysis_script
        MD_job_vars = OrderedDict({
            "in_simSystem": in_gromos_system,
            "out_dir_path": out_simulation_dir,
            "simulation_run_num": simulation_runs,
            "equilibration_run_num": equilibration_runs,
            "submission_system": submission_system,
            "analysis_script_path": in_analysis_script_path,
            "nmpi": nmpi,
        })
        try:
            in_scheduler_script_path = utils.write_job_script(
                out_script_path=step_dir + "/schedule_MD_job.py",
                target_function=simulation_scheduler.do,
                variable_dict=MD_job_vars)
        except Exception as err:
            raise Exception("Could not prepare the scheduling script\n\t" +
                            "\n\t".join(map(str, err.args)))
    except Exception as err:
        traceback.print_exception(*sys.exc_info())
        raise Exception("Could not prepare the command block\n\t" +
                        "\n\t".join(map(str, err.args)))

    # %%
    ##schedule
    try:
        if ((os.path.exists(out_analysis_cnf)
             and os.path.exists(out_simulation_dir + ".tar"))
                and not force_simulation):
            print(utils.spacer2 + "FOUND RESULT: " + out_analysis_cnf +
                  "\n GOING TO SKIPT THIS SUBMISSION!")
            #warnings.warn("Skipping active submission, as result CNF was found: \n"+out_analysis_cnf)
            last_jobID = None
        else:
            last_jobID = simulation_scheduler.do(
                in_simSystem=in_gromos_system,
                out_dir_path=out_simulation_dir,
                simulation_run_num=simulation_runs,
                equilibration_run_num=equilibration_runs,
                submission_system=submission_system,
                previous_job_ID=previous_simulation_run,
                analysis_script_path=in_analysis_script_path,
                nmpi=nmpi)
    except Exception as err:
        traceback.print_exception(*sys.exc_info())
        raise Exception("Could not submit the commands\n\t" +
                        "\n\t".join(map(str, err.args)))

    # Return the promise final system
    final_cnf_file = out_analysis_dir + "/data/" + in_gromos_system.name + ".cnf"
    if (os.path.exists(final_cnf_file)):
        in_gromos_system.cnf = cnf.Cnf(final_cnf_file)
    else:
        in_gromos_system.cnf = cnf.Cnf(in_value=None)
        in_gromos_system.cnf._future_file = True
        in_gromos_system.cnf.path = out_analysis_dir + "/data/" + in_gromos_system.name + ".cnf"

    in_gromos_system.work_folder = step_dir
    return in_gromos_system, last_jobID
def _configure_gromosPP_autotools(
    build_dir: str,
    binary_dir: str = None,
    with_omp: bool = False,
    with_debug: bool = False,
    verbose: bool = True,
    _timing_dict: dict = {},
):
    """
        Setting the configurations for the compiling gromosPP process. (uses autotools)


    Parameters
    ----------
    build_dir : str
        directory, that should be used for building
    binary_dir : str, optional
        directory in which the binaries should be written to, by default None
    with_omp : bool, optional
        should gromosPP be compiled with omp, by default False
    with_debug : bool, optional
        set gromos debug flag, by default False
    verbose : bool, optional
        compiling is fun, I can tell you more!, by default True
    _timing_dict : dict, optional
        structure for storing timings of process, by default {}

    """

    root_dir = os.path.dirname(build_dir)
    os.chdir(root_dir)

    # Initial config
    _timing_dict["gromosPP_init_start"] = datetime.now()
    if verbose:
        print(spacer2 + "\t\t> INIT \n" + spacer2)
        print("start time: ", _timing_dict["gromosPP_init_start"])
        print("workdir:", os.getcwd())

    bash.execute("./Config.sh")

    _timing_dict["gromosPP_init_end"] = datetime.now()
    if verbose:
        print(
            "duration: ",
            str(_timing_dict["gromosPP_init_end"] -
                _timing_dict["gromosPP_init_start"]), "\n")

    # Configure
    bash.make_folder(build_dir)
    os.chdir(build_dir)

    _timing_dict["gromosPP_conf_start"] = datetime.now()
    log_file = build_dir + "/configure.log"

    if verbose:
        print(spacer2 + "\t\t> CONFIGURE \n" + spacer2)
        print("start time: ", _timing_dict["gromosPP_conf_start"])
        print("log_file: ", log_file)

    options = {}
    if binary_dir is not None:
        options.update({"--bindir": binary_dir})

    flags = []
    if with_omp:
        flags.append("--enable-openmp")
    if with_debug:
        flags.append("--enable-debug")

    flags.append(
        " --with-gsl=$(gsl-config --prefix) ")  # this is required for gromosPP
    cmd = ("../configure " +
           " ".join([key + "=" + val for key, val in options.items()]) + " " +
           " ".join([flag for flag in flags]))
    if verbose:
        print("command: ", cmd)

    bash.execute(cmd, catch_STD=log_file)

    _timing_dict["gromosPP_conf_end"] = datetime.now()
    if verbose:
        print(
            "duration: ",
            str(_timing_dict["gromosPP_conf_end"] -
                _timing_dict["gromosPP_conf_start"]), "\n")
def _configure_gromosXX_autotools(
    build_dir: str,
    binary_dir: str = None,
    with_cuda_dir: str = None,
    with_omp: bool = False,
    with_mpi: bool = False,
    with_debug: bool = False,
    verbose: bool = True,
    _timing_dict: dict = {},
):
    """
        Setting the configurations for the compiling gromosXX process. (uses autotools)

    Parameters
    ----------
    build_dir : str
        directory, that should be used for building
    binary_dir : str, optional
        directory in which the binaries should be written to, by default None
    with_cuda_dir : str, optional
        use the following cuda path and activate cuda support, by default None
    with_omp : bool, optional
        should gromosXX be compiled with omp - Warning dont combine wiht mpi!, by default False
    with_mpi : bool, optional
        should gromosXX be compiled wiht mpi - Warning dont combine with omp!, by default False
    with_debug : bool, optional
        set gromos debug flag, by default False
    verbose : bool, optional
        compiling is fun, I can tell you more!, by default True
    _timing_dict : dict, optional
        structure for storing timings of process, by default {}

    Raises
    ------
    ValueError
        if wrong value was passed
    """

    root_dir = os.path.dirname(build_dir)
    os.chdir(root_dir)

    # Initial config
    _timing_dict["gromosXX_init_start"] = datetime.now()
    if verbose:
        print(spacer2 + "\t\t> INIT \n" + spacer2)
        print("start time: ", _timing_dict["gromosXX_init_start"])
        print("workdir:", os.getcwd())

    bash.execute("./Config.sh")

    _timing_dict["gromosXX_init_end"] = datetime.now()
    if verbose:
        print(
            "duration: ",
            str(_timing_dict["gromosXX_init_end"] -
                _timing_dict["gromosXX_init_start"]), "\n")

    # Configure
    bash.make_folder(build_dir)
    os.chdir(build_dir)
    log_file = build_dir + "/configure.log"

    _timing_dict["gromosXX_conf_start"] = datetime.now()
    if verbose:
        print(spacer2 + "\t\t> CONFIGURE \n" + spacer2)
        print("start time: ", _timing_dict["gromosXX_conf_start"])
        print("workdir:", os.getcwd())
        print("log_file: ", log_file)

    options = {}
    if binary_dir is not None:
        options.update({"--bindir": binary_dir})
    if with_cuda_dir is not None:
        options.update({"--with-cuda": with_cuda_dir})

    flags = []
    if with_omp and with_mpi:
        raise ValueError("Can not use with_omp and with_mpi at the same time!")
    if with_omp:
        flags.append("--enable-openmp")
    if with_mpi:
        flags.append("--enable-mpi")
    if with_debug:
        flags.append("--enable-debug")

    cmd = ("../configure " +
           " ".join([key + "=" + val for key, val in options.items()]) + " " +
           " ".join([flag for flag in flags]))
    if verbose:
        print("command: ", cmd)

    bash.execute(cmd, catch_STD=log_file)

    _timing_dict["gromosXX_conf_end"] = datetime.now()
    if verbose:
        print(
            "duration: ",
            str(_timing_dict["gromosXX_conf_end"] -
                _timing_dict["gromosXX_conf_start"]), "\n")
示例#9
0
def TI_sampling(
    in_gromos_system: Gromos_System,
    project_dir: str = None,
    step_name="lambda_sampling",
    lambda_values: List[float] = np.arange(0, 1.1, 0.1),
    subSystem: _SubmissionSystem = LOCAL(),
    n_productions: int = 3,
    n_equilibrations: int = 1,
    randomize: bool = False,
    dual_cnf: List[str] = None,
    verbose: bool = True,
):
    """
    This function will automatically submit N independent (different lambda)
    MD simulations with a lambda dependent potential energy.

    Parameters
    ----------
        in_gromos_system: Gromos_System
            input gromos system
        project_dir: str
            directory in which simulation input files are found
        step_name: str
            subdirectory of project_dir, in which we will write the output
            important: allows to run multiple random seeds with a different "step_name"
        lambda_values: List [float]
            List of lambda values for each independent simulation
        subSystem: _SubmissionSystem
            where will the calculation run
        n_productions: int
            number of chunks each independent simulation is broken down into
        n_equilibrations: int
            number of chunks of equilibration preceding the production for each independent simulation
        randomize: bool
            Choose a random number for the initial random seed (same for all lambda values)
        dual_cnf: List [str], optional
            If provided, should be the path to two conformations (matching end states A and B) which
            can be used as initial conformations
            Simulations with a lambda value between 0 and 0.5 will use the first as starting conformation

    Returns
    --------
        lam_system: Gromos_System
            Gromos system of the simulation submitted last
    """

    if project_dir is None:
        project_dir = in_gromos_system.work_folder

    work_dir = bash.make_folder(project_dir + "/" + step_name)

    # Select a random seed here so all lambda windows have the same
    if randomize:
        in_gromos_system.imd.randomize_seed()

    in_gromos_system.save(work_dir)
    general_system_name_prefix = in_gromos_system.name

    lam_systems = []
    for lam in lambda_values:
        lam = np.round(lam, 4)

        lam_system = deepcopy(in_gromos_system)
        lam_system.name = general_system_name_prefix + "_l_" + str(lam)
        lam_system.work_folder = work_dir

        # Choose different conformations depending on lambda point.
        # e.g. this allows to use RE-EDS SSM conformations.
        # dual_cnf[i] contains the path of the cnf to use
        if dual_cnf is not None and lam <= 0.5:
            lam_system.cnf = Cnf(dual_cnf[0])
        elif dual_cnf is not None:
            lam_system.cnf = Cnf(dual_cnf[1])

        # IMD
        # Pertubation
        # Pertubation of system.
        pert_block = PERTURBATION(NTG=1,
                                  NRDGL=0,
                                  RLAM=lam,
                                  DLAMT=0,
                                  ALPHC=0.5,
                                  ALPHLJ=0.5,
                                  NLAM=2,
                                  NSCALE=0)
        lam_system.imd.add_block(block=pert_block)

        # Calculate additional lambda points
        if not hasattr(lam_system, "PRECALCLAM"):
            # Note: This assumes uniformely distributed lambda values
            precalc_lam_block = PRECALCLAM(NRLAM=len(lambda_values),
                                           MINLAM=0,
                                           MAXLAM=1)
            lam_system.imd.add_block(block=precalc_lam_block)

        # Submit
        out_gromos_system = _TI_lam_step(
            in_gromos_system=lam_system,
            project_dir=work_dir,
            step_name=lam_system.name,
            submission_system=subSystem,
            in_imd_path=None,
            simulation_runs=n_productions,
            equilibration_runs=n_equilibrations,
            verbose=verbose,
        )

        out_gromos_system.save(out_gromos_system.work_folder +
                               "/sd_out_system.obj")
        lam_systems.append(out_gromos_system)

    return lam_system
示例#10
0
def project_concatenation(
    in_folder: str,
    in_topology_path: str,
    in_imd: str,
    num_replicas: int,
    control_dict: Dict[str, bool],
    out_folder: str,
    in_ene_ana_lib_path: str,
    out_file_prefix: str = "test",
    fit_traj_to_mol: int = 1,
    starting_time: float = 0,
    include_water_in_trc=True,
    additional_properties: Union[Tuple[str],
                                 List[str]] = ("solvtemp2", "totdisres"),
    n_processes: int = 1,
    gromosPP_bin_dir: str = None,
    verbose: bool = False,
    nofinal=False,
    boundary_conditions: str = "r cog",
) -> dict:
    if verbose:
        print("reading imd file: " + in_imd)

    imd_file = imd.Imd(in_imd)
    dt = float(imd_file.STEP.DT)
    dt_trc = int(imd_file.WRITETRAJ.NTWX) * dt
    dt_tre = int(imd_file.WRITETRAJ.NTWE) * dt

    tmp_dir = out_folder + "/tmp_file"
    if os.path.isdir(tmp_dir):
        bash.make_folder(tmp_dir)

    out_cnfs = out_tres = out_trcs = out_dcd = out_repdat = None
    p_conv = p_cnf = p_repdat = p_trc = p_tre = p_ene_ana = False
    submitted_trc_job = submitted_tre_job = False

    if n_processes > 1:
        p = mult.Pool(n_processes)
        manager = mult.Manager()

    if control_dict["cp_cnf"]:
        if verbose:
            print("\tStart cnfs")
        # find all cnf files in this project
        sim_dir_cnfs = gather_simulation_replica_file_paths(
            in_folder,
            num_replicas,
            filePrefix="",
            fileSuffixes=".cnf",
            verbose=verbose,
            finalNumberingSort=nofinal)

        # do parallel
        if n_processes > 1:
            out_cnfs = manager.dict()
            distribute = [(n, out_cnfs, sim_dir_cnfs,
                           range(n,
                                 len(sim_dir_cnfs) + 1,
                                 n_processes), out_folder, verbose)
                          for n in range(1, n_processes + 1)]
            # _async
            p_cnf = p.starmap(_thread_worker_cnfs, distribute)
        else:
            out_cnfs = {}
            _thread_worker_cnfs(
                job=-1,
                out_cnfs=out_cnfs,
                in_cnfs=sim_dir_cnfs,
                replica_range=list(sim_dir_cnfs.keys()),
                out_folder=out_folder,
                verbose=verbose,
            )
        if verbose:
            print("Out cnfs: ", out_cnfs)

    if control_dict["cat_trc"]:
        print("\tStart Trc Cat")
        # find all trc files in this project
        trc_files = gather_simulation_replica_file_paths(
            in_folder,
            num_replicas,
            filePrefix="",
            fileSuffixes=[".trc", ".trc.gz", ".trc.tar.gz"],
            verbose=verbose,
            finalNumberingSort=nofinal,
        )

        out_prefix = out_folder + "/" + out_file_prefix + "_"

        # concat all files to a single .trc
        if n_processes > 1:  # parallel
            submitted_trc_job = True
            if verbose:
                print("going parallel: n_processes - " + str(n_processes))
            out_trcs = manager.dict()
            distributed_jobs = [(
                n,
                range(n,
                      len(trc_files) + 1, n_processes),
                trc_files,
                out_prefix,
                in_topology_path,
                out_trcs,
                dt_trc,
                starting_time,
                verbose,
                include_water_in_trc,
            ) for n in range(1, n_processes + 1)]
            # _async
            p_trc = p.starmap(_thread_worker_cat_trc, distributed_jobs)
            p.close()
            p.join()

        else:
            out_trcs = {}
            _thread_worker_cat_trc(
                job=-1,
                topology_path=in_topology_path,
                replicaID_range=list(trc_files.keys()),
                trc_files=trc_files,
                out_prefix=out_prefix,
                dt=dt_trc,
                time=starting_time,
                out_trcs=out_trcs,
                verbose=verbose,
                boundary_conditions=boundary_conditions,
                include_all=include_water_in_trc,
            )

    if control_dict["cat_tre"]:
        print("\tStart Tre Cat")

        # find all trc files in this project
        tre_files = gather_simulation_replica_file_paths(
            in_folder,
            num_replicas,
            filePrefix="",
            fileSuffixes=[".tre", ".tre.tar.gz"],
            verbose=verbose,
            finalNumberingSort=nofinal,
        )

        out_prefix = out_folder + "/" + out_file_prefix + "_"
        # concat all files to a single .trc
        if n_processes > 1:
            if verbose:
                print("going parallel: n_processes - " + str(n_processes),
                      " for ", len(tre_files))
            submitted_tre_job = True
            out_tres = manager.dict()
            distributed_jobs = [(n, range(n,
                                          len(tre_files) + 1, n_processes),
                                 tre_files, out_prefix, out_tres, verbose)
                                for n in range(1, n_processes + 1)]
            p = mult.Pool(n_processes)
            p_tre = p.starmap(_thread_worker_cat_tre, distributed_jobs)
            p.close()
            p.join()
        else:
            out_tres = {}
            _thread_worker_cat_tre(
                job=-1,
                replicaID_range=tre_files.keys(),
                tre_files=tre_files,
                out_prefix=out_prefix,
                out_tres=out_tres,
                verbose=verbose,
            )

    if control_dict["ene_ana"]:
        print("\tStart ene ana")

        # wait for async job creating the trcs.
        if submitted_tre_job:
            p_tre.wait()

        # gather potentials
        properties = list(additional_properties)
        # find all trc files in this project
        tre_files = gather_simulation_replica_file_paths(
            in_folder,
            num_replicas,
            filePrefix="",
            fileSuffixes=[".tre", ".tre.gz"],
            verbose=verbose,
            finalNumberingSort=nofinal,
        )  # ".tre.tar.gz"
        # isolate potentials
        if verbose:
            print("Isolate ene_ana:")
        if n_processes > 1:
            p = mult.Pool(n_processes)
            distribute_jobs = [(
                out_folder,
                out_folder,
                properties,
                list(tre_files.keys())[n::n_processes],
                in_ene_ana_lib_path,
                gromosPP_bin_dir,
                out_file_prefix,
                "",
                dt_tre,
                n,
                verbose,
            ) for n in range(n_processes)]
            p_ene_ana = p.starmap_async(thread_worker_isolate_energies,
                                        distribute_jobs)
        else:
            thread_worker_isolate_energies(
                in_en_file_paths=tre_files,
                out_folder=out_folder,
                properties=properties,
                out_prefix=out_file_prefix,
                in_ene_ana_lib=in_ene_ana_lib_path,
                gromosPP_path=gromosPP_bin_dir,
                dt=dt_tre,
                replicas=list(tre_files.keys()),
                verbose=verbose,
            )

    if control_dict["convert_trcs"]:
        print("\tStart Trc Conversion")
        # wait for async job creating the trcs.
        if submitted_trc_job:
            p_trc.wait()

        # get files:
        final_trc_files = list(
            sorted(glob.glob(out_folder + "/*.trc*"),
                   key=lambda x: int(x.split("_")[-1].split(".")[0])))

        if n_processes > 1:
            out_dcd = manager.dict()
            distributed_jobs = [(
                n,
                range(n, num_replicas, n_processes),
                final_trc_files,
                in_topology_path,
                gromosPP_bin_dir,
                out_dcd,
                fit_traj_to_mol,
                verbose,
            ) for n in range(n_processes)]
            p_conv = p.starmap_async(_thread_worker_conv_trc, distributed_jobs)
        else:
            out_dcd = {}
            _thread_worker_conv_trc(
                job=-1,
                replica_range=range(num_replicas),
                trc_files=final_trc_files,
                in_topology_path=in_topology_path,
                gromos_path=gromosPP_bin_dir,
                out_traj=out_dcd,
                fit_traj_to_mol=1,
                verbose=verbose,
                boundary_conditions=boundary_conditions,
            )

    if n_processes > 1:
        # wait for the jobs to finish
        if ((not p_conv or p_conv.wait()) and (not p_cnf or p_cnf.wait())
                and (not p_repdat or p_repdat.wait())
                and (not p_trc or p_trc.wait()) and (not p_tre or p_tre.wait())
                and (not p_ene_ana or p_ene_ana.wait())):
            raise ChildProcessError("A process failed! ")

        p.close()
        p.join()

        out_dict = {
            "out_folder": out_folder,
            "cnfs": dict(out_cnfs),
            "repdat": out_repdat,
            "tres": dict(out_tres),
            "trcs": dict(out_trcs),
            "dcds": dict(out_dcd),
        }
        manager.shutdown()

    else:
        out_dict = {
            "out_folder": out_folder,
            "cnfs": out_cnfs,
            "repdat": out_repdat,
            "tres": out_tres,
            "trcs": out_trcs,
            "dcds": out_dcd,
        }
    if verbose:
        print("all jobs finished")
    return out_dict
示例#11
0
def _thread_worker_cat_trc(
    job: int,
    replicaID_range: List[int],
    trc_files: Dict[int, List[str]],
    out_prefix: str,
    topology_path: str,
    out_trcs: dict,
    dt: float,
    time: float = 0,
    verbose: bool = False,
    boundary_conditions: str = "r cog",
    include_all: bool = False,
):
    """_thread_worker_cat_trc
        This thread worker_scripts concatenates all .trc files of one replica into one file.

    Parameters
    ----------
    job :   rank of this thread
    replicaID_range :   x_range - list of all
    trc_files :     Dict[int, List[str]]
        Dictionary containing all replicas, with list of all trc files concerning one trc.
    out_prefix : str
        output prefix
    verbose : bool
        verbosity?

    Returns
    -------
    None
    """

    gromPP = gromosPP.GromosPP()
    start_dir = os.getcwd()
    if verbose:
        print("JOB " + str(job) + ": range " + str(list(replicaID_range)))
    for replicaID in replicaID_range:
        out_path = out_prefix + str(replicaID) + ".trc"
        compress_out_path = out_path + ".gz"

        out_trcs.update({replicaID: compress_out_path})

        if os.path.exists(
                compress_out_path):  # found perfect compressed trc file:)
            warnings.warn("Skipped generating file as I found: " +
                          compress_out_path)
            if os.path.exists(out_path):
                bash.remove_file(out_path)
            continue
        elif os.path.exists(
                out_path):  # did not find compressed file. will compress
            warnings.warn("Skipped generating file as I found: " + out_path)
            continue
        else:  # concat files
            if verbose:
                print("JOB " + str(job) + ": " + "write out " + out_path +
                      "\n")
            out_dir = os.path.dirname(out_path)
            tmp_dir = bash.make_folder(out_dir + "/TMP_replica_" +
                                       str(replicaID),
                                       additional_option="-p")
            os.chdir(tmp_dir)
            if include_all:
                out_path = gromPP.frameout(
                    in_top_path=topology_path,
                    in_coord_path=" ".join(trc_files[replicaID]),
                    periodic_boundary_condition=boundary_conditions,
                    single_file=True,
                    out_file_format="trc",
                    out_file_path=out_path,
                    time=time,
                    dt=dt,
                    include="ALL",
                )
            else:
                out_path = gromPP.frameout(
                    in_top_path=topology_path,
                    in_coord_path=" ".join(trc_files[replicaID]),
                    periodic_boundary_condition=boundary_conditions,
                    single_file=True,
                    out_file_format="trc",
                    out_file_path=out_path,
                    time=time,
                    dt=dt,
                )
            os.chdir(start_dir)
            bash.wait_for_fileSystem(out_path)
            bash.remove_folder(tmp_dir)
            if verbose:
                print("JOB " + str(job) + ": " + "write out " + out_path +
                      "\t DONE\n")

        if verbose:
            print("JOB " + str(job) + ": " + "compress " + compress_out_path +
                  "\n")
        compressed_trc = bash.compress_gzip(out_path,
                                            out_path=compress_out_path)

        if verbose:
            print("JOB " + str(job) + ": " + "compress " + compressed_trc +
                  "\t DONE\n")
示例#12
0
def simulation(
    in_gromos_simulation_system: Gromos_System,
    override_project_dir: str = None,
    step_name: str = "sim",
    in_imd_path: str = None,
    submission_system: _SubmissionSystem = LOCAL(),
    simulation_runs: int = 1,
    equilibration_runs: int = 0,
    previous_simulation_run: int = None,
    force_simulation: bool = False,
    initialize_first_run=False,
    reinitialize_every_run=False,
    analysis_script: callable = simulation_analysis.do,
    analysis_control_dict: dict = None,
    _no_double_submit_check: bool = False,
    _work_dir: str = None,
    verbose: bool = True,
    verbose_lvl: int = 1,
    _template_imd_path: str = None,
) -> Gromos_System:
    """
        This function is a generic simulation block, that can be used to run and schedule simulations.

    Parameters
    ----------
    in_gromos_simulation_system : Gromos_System
        gromos system that contains the information of the files, that are required for the simulation run.
    override_project_dir : str
        parent project directory
    step_name :  str, optional
        name of the step - used as jobname and folder in the project dir.
    in_imd_path : str, optional
        if a path to an imd file is given here, it will be used!
    submission_system : _SubmissionSystem, optional
        the system, that should be used to submit a job
    simulation_runs : int, optional
        number of sequential simulation runs
    equilibration_runs : int, optional
        number of equilibration runs, that will not appear in the final traj
    previous_simulation_run : int, optional
        job-ID of the previous simulation
    force_simulation : bool, optional
        if simulation already exist, shall it be overwritten?
    analysis_script : callable, optional
        script that is used to analyse the job
    analysis_control_dict : dict, optional
        sub selection of steps, for the analysis script, that should be executed
    verbose : bool, optional
        "baeh, baeh, baeh"  - a wise black-nose sheep from ausserberg.
    verbose_lvl : int, optional
        how much baeh?
    _template_imd_path : str, optional
        default template imd

    Returns
    -------
    Gromos_System,
        returns a new gromos system which is containing the simulation info
    """
    # PREPERATIONS
    try:
        try:
            gromos_system = deepcopy(in_gromos_simulation_system)
            if previous_simulation_run is not None:
                gromos_system._last_jobID = previous_simulation_run

            # check if override dir is given and set project to correct location
            if override_project_dir is not None:
                init_work_folder = override_project_dir
                step_dir = override_project_dir + "/" + step_name
            else:
                init_work_folder = gromos_system.work_folder
                step_dir = gromos_system.work_folder + "/" + step_name

            bash.make_folder(step_dir)
            out_input_dir = step_dir + "/input"
            out_simulation_dir = step_dir + "/simulation"
            out_analysis_dir = step_dir + "/analysis"
            bash.make_folder(out_input_dir)
            os.chdir(step_dir)

            # Prepare gromos system:
            gromos_system.work_folder = out_input_dir
            gromos_system.name = step_name

            if in_imd_path is not None:
                gromos_system.imd = in_imd_path
            elif hasattr(gromos_system.imd, "TITLE"):
                pass
            elif _template_imd_path is not None:
                if verbose:
                    warnings.warn("Template_imd_path was used: " +
                                  _template_imd_path)
                gromos_system.imd = _template_imd_path
                gromos_system.prepare_for_simulation()
            else:
                raise ValueError(
                    "Could not find any .imd path (gromos system has no imd, in_imd_path not given and also no _template_imd_path!)"
                )

            out_analysis_cnf = out_analysis_dir + "/data/" + gromos_system.name + ".cnf"

            if verbose:
                print(spacer)
                print(step_name)
                print(spacer)

            # Write out, all non promised files
            gromos_system.rebase_files()

            # Write Out Ana Script
            in_analysis_control_dict = analysis_control_dict
            n_analysis_processors = 1  # Maybe in the future: 5 if(nmpi>5) else 1
        except Exception as err:
            raise Exception("Could not prepare the gromos System\n\t" +
                            "\n\t".join(map(str, err.args)))
        # do
        if analysis_script is not None:
            analysis_control_dict = (simulation_analysis.template_control_dict
                                     if (in_analysis_control_dict is None) else
                                     in_analysis_control_dict)

            analysis_vars = OrderedDict({
                "in_simulation_dir":
                out_simulation_dir,
                "sim_prefix":
                gromos_system.name,
                "out_analysis_dir":
                out_analysis_dir,
                "gromosPP_bin_dir":
                gromos_system.gromosPP._bin,
                "control_dict":
                analysis_control_dict,
                "n_processes":
                n_analysis_processors,
                "verbose":
                verbose,
                "verbose_lvl":
                verbose_lvl,
            })
            try:
                in_analysis_script_path = utils.write_job_script(
                    out_script_path=step_dir + "/job_analysis.py",
                    target_function=analysis_script,
                    variable_dict=analysis_vars,
                )
            except Exception as err:
                raise Exception("Could not prepare the analysis script\n\t" +
                                "\n\t".join(map(str, err.args)))
        else:
            in_analysis_script_path = None

        # Write Out schedulling Script
        # Build analysis_script
        MD_job_vars = OrderedDict({
            "in_simSystem": gromos_system,
            "out_dir_path": out_simulation_dir,
            "simulation_run_num": simulation_runs,
            "equilibration_run_num": equilibration_runs,
            "submission_system": submission_system,
            "analysis_script_path": in_analysis_script_path,
            "initialize_first_run": initialize_first_run,
            "reinitialize_every_run": reinitialize_every_run,
            "previous_job_ID": gromos_system._last_jobID,
            "_no_double_submit_check": _no_double_submit_check,
            "_work_dir": _work_dir,
            "verbose": verbose,
            "verbose_lvl": verbose_lvl,
        })

        try:
            utils.write_job_script(
                out_script_path=step_dir + "/schedule_MD_job.py",
                target_function=simulation_scheduler.do,
                variable_dict=MD_job_vars,
            )
        except Exception as err:
            raise Exception("Could not prepare the scheduling script\n\t" +
                            "\n\t".join(map(str, err.args)))
    except Exception as err:
        traceback.print_exception(*sys.exc_info())
        raise Exception("Could not prepare the command block\n\t" +
                        "\n\t".join(map(str, err.args)))

    # schedule
    try:
        if (os.path.exists(out_analysis_cnf)
                and os.path.exists(out_simulation_dir +
                                   ".tar")) and not force_simulation:
            if verbose:
                print(utils.spacer2 + "FOUND RESULT: " + out_analysis_cnf +
                      "\n GOING TO SKIPT THIS SUBMISSION!")
            # warnings.warn("Skipping active submission, as result CNF was found: \n"+out_analysis_cnf)
            last_jobID = 0
        else:
            last_jobID = simulation_scheduler.do(**MD_job_vars)
    except Exception as err:
        traceback.print_exception(*sys.exc_info())
        raise Exception("Could not submit the commands\n\t" +
                        "\n\t".join(map(str, err.args)))

    time.sleep(time_wait_s_for_filesystem)

    # Return the promise final system
    if in_analysis_script_path is None and os.path.exists(out_simulation_dir):
        cnfs = list(
            sorted(glob.glob(out_simulation_dir + "/*/*cnf"),
                   key=lambda x: int(x.split("_")[-1].split(".")[0])))
        last_cnf = cnfs[-1]
        gromos_system.cnf = cnf.Cnf(last_cnf)
    elif os.path.exists(out_analysis_cnf):
        gromos_system.cnf = cnf.Cnf(out_analysis_cnf)
    else:
        gromos_system.cnf = cnf.Cnf(in_value=None)
        gromos_system.cnf._future_file = True
        gromos_system.cnf.path = out_analysis_dir + "/data/" + gromos_system.name + ".cnf"

    # Return trajectories if available
    gromos_system._traj_files_path = {}
    if hasattr(gromos_system.imd,
               "WRITETRAJ") and gromos_system.imd.WRITETRAJ.NTWX > 0:
        final_trc_file = out_analysis_dir + "/data/" + gromos_system.name + ".trc"
        if os.path.exists(final_trc_file + ".h5"):
            gromos_system.trc = final_trc_file + ".h5"
        elif os.path.exists(final_trc_file):
            gromos_system.trc = final_trc_file
        else:
            gromos_system.trc = Trc(traj_path=None, in_cnf=None)
            gromos_system.trc._future_file = True
            gromos_system.trc.path = final_trc_file

    if hasattr(gromos_system.imd,
               "WRITETRAJ") and gromos_system.imd.WRITETRAJ.NTWE > 0:
        final_tre_file = out_analysis_dir + "/data/" + gromos_system.name + ".tre"
        if os.path.exists(final_tre_file + ".h5"):
            gromos_system.tre = final_tre_file + ".h5"
        elif os.path.exists(final_tre_file):
            gromos_system.tre = final_tre_file
        else:
            gromos_system.tre = Tre(input_value=None)
            gromos_system.tre._future_file = True
            gromos_system.tre.path = final_tre_file

    if hasattr(gromos_system.imd,
               "WRITETRAJ") and gromos_system.imd.WRITETRAJ.NTWG > 0:
        final_trg_file = out_analysis_dir + "/data/" + gromos_system.name + ".trg"
        if os.path.exists(final_trg_file + ".h5"):
            gromos_system.trg = Trg(input_value=final_trg_file + ".h5")
        elif os.path.exists(final_trg_file):
            gromos_system.trg = Trg(input_value=final_trg_file)
        else:
            gromos_system.trg = Trg(input_value=None)
            gromos_system.trg._future_file = True
            gromos_system.trg.path = final_trg_file

    # Set work_folder to initial work folder path to avoid nested simulations
    # However, do not update all file names (work_folder_no_update) to avoid
    # compromising the file system structure
    gromos_system.work_folder_no_update(init_work_folder)
    gromos_system._last_jobID = last_jobID
    os.chdir(init_work_folder)

    return gromos_system
def do(
    in_simSystem: Gromos_System,
    out_dir_path: str,
    simulation_run_num: int,
    equilibration_run_num: int = 0,
    initialize_first_run=False,
    reinitialize_every_run=False,
    analysis_script_path: str = None,
    submission_system: _SubmissionSystem = LSF(),
    previous_job_ID: int = None,
    _no_double_submit_check: bool = False,
    _work_dir: str = None,
    verbose: bool = True,
    verbose_lvl: int = 1,
) -> int:
    """
        This function schedules simulations starting from the gromos system.

    Parameters
    ----------
    in_simSystem : Gromos_System
        system that should be submitted with the provided imd file
    out_dir_path : str
        out directory  path for the simulation
    simulation_run_num : int
        number of simulations
    equilibration_run_num : int, optional
        number of the equilibraitons, by default 0
    initialize_first_run : bool, optional
        should the velocities be initialized in the first run?, by default False
    reinitialize_every_run : bool, optional
        DEAPPRECIATED! should always the velocities be initialized, by default False
    analysis_script_path : str, optional
        path to the analysis script, that should be used for this simulaiton approach, by default None
    submission_system : _SubmissionSystem, optional
        system, to be used to submit the jobs, by default LSF()
    previous_job_ID : int, optional
        previous job ID, by default None
    _no_double_submit_check : bool, optional
        don't check if job was already submit to queue (increases performance!), by default False
    _work_dir : str, optional
        directory, to write out the tmp files, by default None
    verbose : bool, optional
        Baeh Baeh, by default True
    verbose_lvl : int, optional
        amount of Baehs, by default 1

    Returns
    -------
    int
        the last job id, that was submitted.

    Raises
    ------
    IOError
        If error happens in preperation of simulation or in the submission

    """

    submission_system.block_double_submission = _no_double_submit_check
    job_verb = True if (verbose and verbose_lvl > 2) else False

    # prepare
    try:
        if verbose:
            print("Script: ", __file__)

        if verbose and verbose_lvl > 2:
            print(spacer + "Simulation PREPERATION\n" + spacer + "\n")

        # Outdir
        bash.make_folder(out_dir_path)  # final output_folder

        # workdir:
        if not isinstance(_work_dir, type(None)) and _work_dir != "None":
            if verbose and verbose_lvl > 2:
                print("\t -> Generating given workdir: " + _work_dir)
            bash.make_folder(_work_dir, "-p")
            os.chdir(_work_dir)
        else:
            if verbose and verbose_lvl > 2:
                print("\t -> Using on node workdir")

        # sim vars logs
        out_prefix = in_simSystem.name
        worker_script = workerScript.__file__

        # CHECK PATH DEPENDENCIES - all Files present?
        # needed variables
        check_path_dependencies_paths = [
            worker_script,
            out_dir_path,
        ]  # Coord file is used by repex in_imd_path prepared_im
        # variable paths
        if _work_dir is not None and _work_dir != "out_dir":
            check_path_dependencies_paths.append(_work_dir)

        if not in_simSystem.top._future_file:
            check_path_dependencies_paths.append(in_simSystem.top.path)
        if not in_simSystem.cnf._future_file:
            check_path_dependencies_paths.append(in_simSystem.cnf.path)
        if not in_simSystem.imd._future_file:
            check_path_dependencies_paths.append(in_simSystem.imd.path)
        if in_simSystem.ptp is not None:
            check_path_dependencies_paths.append(in_simSystem.ptp.path)
        if in_simSystem.disres is not None:
            check_path_dependencies_paths.append(in_simSystem.disres.path)
        if in_simSystem.posres is not None:
            check_path_dependencies_paths.append(in_simSystem.posres.path)
        if in_simSystem.refpos is not None:
            check_path_dependencies_paths.append(in_simSystem.refpos.path)
        if in_simSystem.qmmm is not None:
            check_path_dependencies_paths.append(in_simSystem.qmmm.path)

        bash.check_path_dependencies(check_path_dependencies_paths,
                                     verbose=job_verb)

    except Exception as err:
        print(
            "#####################################################################################"
        )
        print("\t\tERROR in Preperations")
        print(
            "#####################################################################################"
        )

        traceback.print_exception(*sys.exc_info())

        raise IOError("ERROR in Preperations to submission!") from err

    # RUN Job
    try:

        if verbose:
            print("\n" + spacer + "Simulation Setup:\n" + spacer)
        if verbose:
            print("steps_per_run: ", in_simSystem.imd.STEP.NSTLIM)
        if verbose:
            print("equis: ", equilibration_run_num)
        if verbose:
            print("simulation runs: ", simulation_run_num)

        # Submission
        # EQ
        eq_job_id = None
        if equilibration_run_num > 0:  # EQUILIBRATION
            tmp_outprefix = "eq_" + out_prefix
            tmp_jobname = in_simSystem.name + "_eq"
            eq_job_id, tmp_jobname, in_simSystem = chain_submission(
                simSystem=in_simSystem,
                out_dir_path=out_dir_path,
                out_prefix=tmp_outprefix,
                jobname=tmp_jobname,
                chain_job_repetitions=equilibration_run_num,
                worker_script=workerScript.__file__,
                job_submission_system=submission_system,
                start_run_index=1,
                prefix_command="",
                previous_job_ID=previous_job_ID,
                work_dir=_work_dir,
                initialize_first_run=initialize_first_run,
                reinitialize_every_run=reinitialize_every_run,
                verbose=job_verb,
            )

        # MD
        tmp_outprefix = out_prefix
        tmp_jobname = in_simSystem.name
        previous_job_ID = previous_job_ID if (eq_job_id is None) else eq_job_id
        previous_job_ID, tmp_jobname, in_simSystem = chain_submission(
            simSystem=in_simSystem,
            out_dir_path=out_dir_path,
            out_prefix=tmp_outprefix,
            jobname=tmp_jobname,
            chain_job_repetitions=equilibration_run_num + simulation_run_num,
            start_run_index=equilibration_run_num + 1,
            worker_script=workerScript.__file__,
            job_submission_system=submission_system,
            prefix_command="",
            previous_job_ID=previous_job_ID,
            work_dir=_work_dir,
            initialize_first_run=initialize_first_run,
            reinitialize_every_run=reinitialize_every_run,
            verbose=job_verb,
        )

        ana_previous_job_ID = previous_job_ID
        if analysis_script_path is not None:
            tmp_jobname = in_simSystem.name + "_ana"

            ana_log = os.path.dirname(analysis_script_path) + "/ana_out.log"
            if verbose:
                print(spacer + "\n submit final analysis part \n")
            if verbose:
                print(ana_log)
            if verbose:
                print(analysis_script_path)

            sub_job = Submission_job(
                command="python3 " + analysis_script_path,
                jobName=tmp_jobname,
                outLog=ana_log,
                queue_after_jobID=previous_job_ID,
            )
            ana_previous_job_ID = submission_system.submit_to_queue(sub_job)

            if verbose:
                print("ANA jobID: " + str(previous_job_ID))

    except Exception as err:
        print(
            "#####################################################################################"
        )
        print("\t\tERROR in Submission")
        print(
            "#####################################################################################"
        )

        traceback.print_exception(*sys.exc_info())
        raise IOError("ERROR in SUBMISSION!") from err

    # in_simSystem._future_promise = False #reset future promising if necessary
    return ana_previous_job_ID