<commit_before>#! /usr/bin/env python

import os
import subprocess
import shlex
import shutil

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import numpy as np

from htsohm.runDB_declarative import Base, RunData, CreateSession

def CreateSession():
#
#    from sqlalchemy import create_engine
#    from sqlalchemy.orm import sessionmaker
#    from runDB_declarative import Base

    engine = create_engine( "sqlite:///HTSOHM-dev.db" )
    Base.metadata.bind = engine

    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    return session

def AddRows(run_ID, mat_IDs):
#
#    from runDB_declarative import RunData

    s = CreateSession()

    for i in mat_IDs:
        check_first = s.query(RunData).filter( RunData.run_id == run_ID,
                                               RunData.material_id == str(i)
                                              ).count()
        if not check_first:
            new_mat = RunData( run_id=run_ID, material_id=str(i) )
            s.add(new_mat)
    s.commit()


def UpdateTable(run_ID, mat_ID, data):
#
#   from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.material_id == str(mat_ID) )
    DBmat.update(data)
    s.commit()


def GetValue(run_ID, mat_ID, value):
#
#    from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.material_id == str(mat_ID) )

    for i in DBmat:
        DBval = getattr(i, value)

    return DBval

def id_to_mat(run_ID, ID):
#
#    from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.id == str(ID) )

    for i in DBmat:
        mat = getattr(i, "material_id")

    return mat


def VoidFraction(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()
    
    # Simulate VOID FRACTION:
    VF_input = open( pwd + '/VoidFraction.input', "w")
    VF_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t1000\n" +             # number of MonteCarlo cycles
         "PrintEvery\t\t\t100\n" +
                    "PrintPropertiesEvery\t\t100\n" +
                    "\n" +
                    "Forcefield\t\t\t%s-%s\n" % (run_ID, mat_ID) +
                    "CutOff\t\t\t\t12.8\n" +                       # LJ interaction cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "ExternalTemperature 298.0\n" +       # External temperature, K
                    "\n" +
                    "Component 0 MoleculeName\t\thelium\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            WidomProbability\t\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    VF_input.close()

    subprocess.call(shlex.split('simulate VoidFraction.input'))

def MethaneLoading(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()

    VF = GetValue(run_ID, mat_ID, "helium_void_fraction")

    # Simulate METHANE LOADING
    ML_input = open( pwd + '/MethaneLoading.input', "w")
    ML_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t1000\n" +             # number of MonteCarlo cycles
                    "NumberOfInitializationCycles\t500\n" +    # number of initialization cycles
                    "PrintEvery\t\t\t100\n" +
                    "RestartFile\t\t\tno\n" +
                    "\n" +
                    "Forcefield\t\t\t%s-%s\n" % (run_ID, mat_ID) +
                    "ChargeMethod\t\t\tEwald\n"
                    "CutOff\t\t\t\t12.0\n" +                   # electrostatic cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "HeliumVoidFraction %s\n" % (VF) +
                    "ExternalTemperature 298.0\n" +            # External temperature, K
                    "ExternalPressure 3500000\n" +             # External pressure, Pa
                    "\n" +
                    "Component 0 MoleculeName\t\tmethane\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            TranslationProbability\t1.0\n" +
                    "            ReinsertionProbability\t1.0\n" +
                    "            SwapProbability\t\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    ML_input.close()

    subprocess.call(shlex.split('simulate MethaneLoading.input'))


def SurfaceArea(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()

    # Simulate SURFACE AREA:
    SA_input = open( pwd + '/SurfaceArea.input', "w")
    SA_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t10\n" +             # number of MonteCarlo cycles
                    "PrintEvery\t\t\t1\n" +
                    "PrintPropertiesEvery\t\t1\n" +
                    "\n" +
                    "Forcefield %s-%s\n" % (run_ID, mat_ID) +
                    "CutOff 12.8\n" +                        # electrostatic cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "SurfaceAreaProbeDistance Minimum\n" +
                    "\n" +
                    "Component 0 MoleculeName\t\tN2\n" +
                    "            StartingBead\t\t0\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            SurfaceAreaProbability\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    SA_input.close()

    subprocess.call(shlex.split('simulate SurfaceArea.input'))


def GetML(run_ID, mat_ID):

    MethaneLoading(run_ID, mat_ID)

    ML_data = "Output/System_0/output_%s-%s_1.1.1_298.000000_3.5e+06.data" % (run_ID, mat_ID)
    with open(ML_data) as origin:
        for line in origin:
            if "absolute [mol/kg" in line:
                ML_a_mk = line.split()[5]
            elif "absolute [cm^3 (STP)/g" in line:
                ML_a_cg = line.split()[6]
            elif "absolute [cm^3 (STP)/c" in line:
                ML_a_cc = line.split()[6]
            elif "excess [mol/kg" in line:
                ML_e_mk = line.split()[5]
            elif "excess [cm^3 (STP)/g" in line:
                ML_e_cg = line.split()[6]
            elif "excess [cm^3 (STP)/c" in line:
                ML_e_cc = line.split()[6]

    data = {"absolute_volumetric_loading": ML_a_cc,
            "absolute_gravimetric_loading": ML_a_cg,
            "absolute_molar_loading": ML_a_mk,
            "excess_volumetric_loading": ML_e_cc,
            "excess_gravimetric_loading": ML_e_cg,
            "excess_molar_loading": ML_e_mk}
    UpdateTable(run_ID, mat_ID, data)

    print( "\nMETHANE LOADING\tabsolute\texcess\n" +
           "mol/kg\t\t%s\t%s\n" % (ML_a_mk, ML_e_mk) +<commit_msg>use .run and check=True to raise an exception when the underlying executable fails<commit_after>#! /usr/bin/env python

import os
import subprocess
import shlex
import shutil

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import numpy as np

from htsohm.runDB_declarative import Base, RunData, CreateSession

def CreateSession():
#
#    from sqlalchemy import create_engine
#    from sqlalchemy.orm import sessionmaker
#    from runDB_declarative import Base

    engine = create_engine( "sqlite:///HTSOHM-dev.db" )
    Base.metadata.bind = engine

    DBSession = sessionmaker(bind=engine)
    session = DBSession()

    return session

def AddRows(run_ID, mat_IDs):
#
#    from runDB_declarative import RunData

    s = CreateSession()

    for i in mat_IDs:
        check_first = s.query(RunData).filter( RunData.run_id == run_ID,
                                               RunData.material_id == str(i)
                                              ).count()
        if not check_first:
            new_mat = RunData( run_id=run_ID, material_id=str(i) )
            s.add(new_mat)
    s.commit()


def UpdateTable(run_ID, mat_ID, data):
#
#   from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.material_id == str(mat_ID) )
    DBmat.update(data)
    s.commit()


def GetValue(run_ID, mat_ID, value):
#
#    from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.material_id == str(mat_ID) )

    for i in DBmat:
        DBval = getattr(i, value)

    return DBval

def id_to_mat(run_ID, ID):
#
#    from runDB_declarative import RunData

    s = CreateSession()
    DBmat = s.query(RunData).filter( RunData.run_id == run_ID,
                                     RunData.id == str(ID) )

    for i in DBmat:
        mat = getattr(i, "material_id")

    return mat


def VoidFraction(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()
    
    # Simulate VOID FRACTION:
    VF_input = open( pwd + '/VoidFraction.input', "w")
    VF_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t1000\n" +             # number of MonteCarlo cycles
         "PrintEvery\t\t\t100\n" +
                    "PrintPropertiesEvery\t\t100\n" +
                    "\n" +
                    "Forcefield\t\t\t%s-%s\n" % (run_ID, mat_ID) +
                    "CutOff\t\t\t\t12.8\n" +                       # LJ interaction cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "ExternalTemperature 298.0\n" +       # External temperature, K
                    "\n" +
                    "Component 0 MoleculeName\t\thelium\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            WidomProbability\t\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    VF_input.close()

    subprocess.run(shlex.split('simulate VoidFraction.input'), check=True)


def MethaneLoading(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()

    VF = GetValue(run_ID, mat_ID, "helium_void_fraction")

    # Simulate METHANE LOADING
    ML_input = open( pwd + '/MethaneLoading.input', "w")
    ML_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t1000\n" +             # number of MonteCarlo cycles
                    "NumberOfInitializationCycles\t500\n" +    # number of initialization cycles
                    "PrintEvery\t\t\t100\n" +
                    "RestartFile\t\t\tno\n" +
                    "\n" +
                    "Forcefield\t\t\t%s-%s\n" % (run_ID, mat_ID) +
                    "ChargeMethod\t\t\tEwald\n"
                    "CutOff\t\t\t\t12.0\n" +                   # electrostatic cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "HeliumVoidFraction %s\n" % (VF) +
                    "ExternalTemperature 298.0\n" +            # External temperature, K
                    "ExternalPressure 3500000\n" +             # External pressure, Pa
                    "\n" +
                    "Component 0 MoleculeName\t\tmethane\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            TranslationProbability\t1.0\n" +
                    "            ReinsertionProbability\t1.0\n" +
                    "            SwapProbability\t\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    ML_input.close()

    subprocess.run(shlex.split('simulate MethaneLoading.input'), check=True)

def SurfaceArea(run_ID, mat_ID):
#
#    import os
#    import subprocess
#    import shlex

    pwd = os.getcwd()

    # Simulate SURFACE AREA:
    SA_input = open( pwd + '/SurfaceArea.input', "w")
    SA_input.write( "SimulationType\t\t\tMonteCarlo\n" +
                    "NumberOfCycles\t\t\t10\n" +             # number of MonteCarlo cycles
                    "PrintEvery\t\t\t1\n" +
                    "PrintPropertiesEvery\t\t1\n" +
                    "\n" +
                    "Forcefield %s-%s\n" % (run_ID, mat_ID) +
                    "CutOff 12.8\n" +                        # electrostatic cut-off, Angstroms
                    "\n" +
                    "Framework 0\n" +
                    "FrameworkName %s-%s\n" % (run_ID, mat_ID) +
                    "UnitCells 1 1 1\n" +
                    "SurfaceAreaProbeDistance Minimum\n" +
                    "\n" +
                    "Component 0 MoleculeName\t\tN2\n" +
                    "            StartingBead\t\t0\n" +
                    "            MoleculeDefinition\t\tTraPPE\n" +
                    "            SurfaceAreaProbability\t1.0\n" +
                    "            CreateNumberOfMolecules\t0\n" )
    SA_input.close()

    subprocess.run(shlex.split('simulate SurfaceArea.input'), check=True)

def GetML(run_ID, mat_ID):

    MethaneLoading(run_ID, mat_ID)

    ML_data = "Output/System_0/output_%s-%s_1.1.1_298.000000_3.5e+06.data" % (run_ID, mat_ID)
    with open(ML_data) as origin:
        for line in origin:
            if "absolute [mol/kg" in line:
                ML_a_mk = line.split()[5]
            elif "absolute [cm^3 (STP)/g" in line:
                ML_a_cg = line.split()[6]
            elif "absolute [cm^3 (STP)/c" in line:
                ML_a_cc = line.split()[6]
            elif "excess [mol/kg" in line:
                ML_e_mk = line.split()[5]
            elif "excess [cm^3 (STP)/g" in line:
                ML_e_cg = line.split()[6]
            elif "excess [cm^3 (STP)/c" in line:
                ML_e_cc = line.split()[6]

    data = {"absolute_volumetric_loading": ML_a_cc,
            "absolute_gravimetric_loading": ML_a_cg,
            "absolute_molar_loading": ML_a_mk,
            "excess_volumetric_loading": ML_e_cc,
            "excess_gravimetric_loading": ML_e_cg,
            "excess_molar_loading": ML_e_mk}
    UpdateTable(run_ID, mat_ID, data)

    print( "\nMETHANE LOADING\tabsolute\texcess\n" +
           "mol/kg\t\t%s\t%s\n" % (ML_a_mk, ML_e_mk) +
{'fraction_non_alphanumeric': 0.11269851817396505, 'fraction_numerical': 0.013954415575785766, 'mean_word_length': 1.5209380234505863, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5659662', 'n_tokens_mistral': 5240, 'n_tokens_neox': 4825, 'n_words': 899}