Ejemplo n.º 1
0
    def load(self):
        """
        Load the contents of the input and output files into a MEASURE object.
        """
        from rmgpy.measure.main import MEASURE

        self.measure = MEASURE()

        if self.outputFileExists():
            self.measure.loadOutput(self.getOutputFilename())
        elif self.inputFileExists():
            self.measure.loadInput(self.getInputFilename())

        if self.measure.network is not None:
            self.title = self.measure.network.title
            self.save()

        return self.measure.network
Ejemplo n.º 2
0
 def load(self):
     """
     Load the contents of the input and output files into a MEASURE object.
     """
     from rmgpy.measure.main import MEASURE
     
     self.measure = MEASURE()
     
     if self.outputFileExists():
         self.measure.loadOutput(self.getOutputFilename())
     elif self.inputFileExists():
         self.measure.loadInput(self.getInputFilename())
     
     if self.measure.network is not None:
         self.title = self.measure.network.title
         self.save()
     
     return self.measure.network
Ejemplo n.º 3
0
class Network(models.Model):
    """
    A Django model of a pressure-dependent reaction network. 
    """
    def upload_input_to(instance, filename):
        # Always name the uploaded input file "input.py"
        return 'pdep/networks/{0}/input.py'.format(instance.pk)
    id = models.CharField(max_length=32, primary_key=True, default=_createId)
    title = models.CharField(max_length=50)
    inputFile = models.FileField(upload_to=upload_input_to, verbose_name='Input file')
    inputText = models.TextField(blank=True, verbose_name='')
    user = models.ForeignKey(User)

    def __init__(self, *args, **kwargs):
        super(Network, self).__init__(*args, **kwargs)
        self.measure = None

    def getDirname(self):
        """
        Return the absolute path of the directory that the Network object uses
        to store files.
        """
        return os.path.join(settings.MEDIA_ROOT, 'pdep', 'networks', str(self.pk))
    
    def getInputFilename(self):
        """
        Return the absolute path of the input file.
        """
        return os.path.join(self.getDirname(), 'input.py')
    
    def getOutputFilename(self):
        """
        Return the absolute path of the output file.
        """
        return os.path.join(self.getDirname(), 'output.py')
    
    def getLogFilename(self):
        """
        Return the absolute path of the log file.
        """
        return os.path.join(self.getDirname(), 'MEASURE.log')
    
    def getSurfaceFilenamePNG(self):
        """
        Return the absolute path of the PES image file in PNG format.
        """
        return os.path.join(self.getDirname(), 'PES.png')
    
    def getSurfaceFilenamePDF(self):
        """
        Return the absolute path of the PES image file in PDF format.
        """
        return os.path.join(self.getDirname(), 'PES.pdf')
    
    def getSurfaceFilenameSVG(self):
        """
        Return the absolute path of the PES image file in SVG format.
        """
        return os.path.join(self.getDirname(), 'PES.svg')
    
    def getLastModifiedDate(self):
        """
        Return the date on which the network was most recently modified.
        """
        if not self.inputFileExists(): return 'unknown'
        
        mtime = os.path.getmtime(self.getInputFilename())
        if self.outputFileExists():
            mtime0 = os.path.getmtime(self.getOutputFilename())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFilePDFExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenamePDF())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFilePNGExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenamePNG())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFileSVGExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenameSVG())
            if mtime < mtime0: mtime = mtime0
        
        gmtime = time.gmtime(mtime)
        return time.strftime("%d %b %Y", gmtime)
        
    def inputFileExists(self):
        """
        Return ``True`` if the input file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getInputFilename())
        
    def outputFileExists(self):
        """
        Return ``True`` if the output file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getOutputFilename())
        
    def logFileExists(self):
        """
        Return ``True`` if the log file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getLogFilename())
        
    def surfaceFilePNGExists(self):
        """
        Return ``True`` if a potential energy surface PNG image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenamePNG())
        
    def surfaceFilePDFExists(self):
        """
        Return ``True`` if a potential energy surface PDF image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenamePDF())
        
    def surfaceFileSVGExists(self):
        """
        Return ``True`` if a potential energy surface SVG image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenameSVG())
        
    def outputFileOutOfDate(self):
        """
        Return ``True`` if the output file is out of date or ``False`` if
        not.
        """
        return self.outputFileExists() and os.path.getmtime(self.getInputFilename()) > os.path.getmtime(self.getOutputFilename())
        
    def surfaceFilePNGOutOfDate(self):
        """
        Return ``True`` if a potential energy surface PNG image file is out of 
        date or ``False`` if not.
        """
        return self.surfaceFilePNGExists() and os.path.getmtime(self.getInputFilename()) > os.path.getmtime(self.getSurfaceFilenamePNG())
        
    def surfaceFilePDFOutOfDate(self):
        """
        Return ``True`` if a potential energy surface PDF image file is out of
        date or ``False`` if not.
        """
        return self.surfaceFilePDFExists() and os.path.getmtime(self.getInputFilename()) > os.path.getmtime(self.getSurfaceFilenamePDF())
        
    def surfaceFileSVGOutOfDate(self):
        """
        Return ``True`` if a potential energy surface SVG image file is out of
        date or ``False`` if not.
        """
        return self.surfaceFileSVGExists() and os.path.getmtime(self.getInputFilename()) > os.path.getmtime(self.getSurfaceFilenameSVG())
        
    def createDir(self):
        """
        Create the directory (and any other needed parent directories) that
        the Network uses for storing files.
        """
        try:
            os.makedirs(self.getDirname())
        except OSError:
            # Fail silently on any OS errors
            pass
        
    def deleteInputFile(self):
        """
        Delete the input file for this network from the server.
        """
        if self.inputFileExists():
            os.remove(self.getInputFilename())
        
    def deleteOutputFile(self):
        """
        Delete the output file for this network from the server.
        """
        if self.outputFileExists():
            os.remove(self.getOutputFilename())
        
    def deleteSurfaceFilePNG(self):
        """
        Delete the PES image file in PNF format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenamePNG()):
            os.remove(self.getSurfaceFilenamePNG())
        
    def deleteSurfaceFilePDF(self):
        """
        Delete the PES image file in PDF format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenamePDF()):
            os.remove(self.getSurfaceFilenamePDF())
        
    def deleteSurfaceFileSVG(self):
        """
        Delete the PES image file in SVG format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenameSVG()):
            os.remove(self.getSurfaceFilenameSVG())
        
    def loadInputText(self):
        """
        Load the input file text into the inputText field.
        """
        self.inputText = ''
        if self.inputFileExists():
            f = open(self.getInputFilename(),'r')
            for line in f:
                self.inputText += line
            f.close()
        
    def saveInputText(self):
        """
        Save the contents of the inputText field to the input file.
        """
        fpath = self.getInputFilename()
        self.createDir()
        f = open(fpath,'w')
        for line in self.inputText.splitlines():
            f.write(line + '\n')
        f.close()
    
    def load(self):
        """
        Load the contents of the input and output files into a MEASURE object.
        """
        from rmgpy.measure.main import MEASURE
        
        self.measure = MEASURE()
        
        if self.outputFileExists():
            self.measure.loadOutput(self.getOutputFilename())
        elif self.inputFileExists():
            self.measure.loadInput(self.getInputFilename())
        
        if self.measure.network is not None:
            self.title = self.measure.network.title
            self.save()
        
        return self.measure.network
Ejemplo n.º 4
0
    def saveForm(self, posted, form):
        """
        Save form data into input.py file specified by the path.
        """
        # Clean past history
        self.rmg = RMG()

        # Databases
        #self.rmg.databaseDirectory = settings['database.directory']
        self.rmg.thermoLibraries = []
        if posted.thermo_libraries.all():
            self.rmg.thermoLibraries = [
                item.thermolib.encode()
                for item in posted.thermo_libraries.all()
            ]
        self.rmg.reactionLibraries = []
        self.rmg.seedMechanisms = []
        if posted.reaction_libraries.all():
            for item in posted.reaction_libraries.all():
                if not item.seedmech and not item.edge:
                    self.rmg.reactionLibraries.append(
                        (item.reactionlib.encode(), False))
                elif not item.seedmech:
                    self.rmg.reactionLibraries.append(
                        (item.reactionlib.encode(), True))
                else:
                    self.rmg.seedMechanisms.append(item.reactionlib.encode())
        self.rmg.statmechLibraries = []
        self.rmg.kineticsDepositories = ['training']
        self.rmg.kineticsFamilies = ['!Intra_Disproportionation']
        self.rmg.kineticsEstimator = 'rate rules'

        # Species
        self.rmg.initialSpecies = []
        speciesDict = {}
        initialMoleFractions = {}
        self.rmg.reactionModel = CoreEdgeReactionModel()
        for item in posted.reactor_species.all():
            structure = Molecule().fromAdjacencyList(item.adjlist.encode())
            spec, isNew = self.rmg.reactionModel.makeNewSpecies(
                structure,
                label=item.name.encode(),
                reactive=False if item.inert else True)
            self.rmg.initialSpecies.append(spec)
            speciesDict[item.name.encode()] = spec
            initialMoleFractions[spec] = item.molefrac

        # Reactor systems
        self.rmg.reactionSystems = []
        for item in posted.reactor_systems.all():
            T = Quantity(item.temperature, item.temperature_units.encode())
            P = Quantity(item.pressure, item.pressure_units.encode())
            termination = []
            if item.conversion:
                termination.append(
                    TerminationConversion(speciesDict[item.species.encode()],
                                          item.conversion))
            termination.append(
                TerminationTime(
                    Quantity(item.terminationtime, item.time_units.encode())))
            system = SimpleReactor(T, P, initialMoleFractions, termination)
            self.rmg.reactionSystems.append(system)

        # Simulator tolerances
        self.rmg.absoluteTolerance = form.cleaned_data['simulator_atol']
        self.rmg.relativeTolerance = form.cleaned_data['simulator_rtol']
        self.rmg.fluxToleranceKeepInEdge = form.cleaned_data[
            'toleranceKeepInEdge']
        self.rmg.fluxToleranceMoveToCore = form.cleaned_data[
            'toleranceMoveToCore']
        self.rmg.fluxToleranceInterrupt = form.cleaned_data[
            'toleranceInterruptSimulation']
        self.rmg.maximumEdgeSpecies = form.cleaned_data['maximumEdgeSpecies']

        # Pressure Dependence
        pdep = form.cleaned_data['pdep'].encode()
        if pdep != 'off':
            self.rmg.pressureDependence = MEASURE()
            self.rmg.pressureDependence.method = pdep
            # Temperature and pressure range
            interpolation = (form.cleaned_data['interpolation'].encode(),
                             form.cleaned_data['temp_basis'],
                             form.cleaned_data['p_basis'])
            self.rmg.pressureDependence.Tmin = Quantity(
                form.cleaned_data['temp_low'],
                form.cleaned_data['temprange_units'].encode())
            self.rmg.pressureDependence.Tmax = Quantity(
                form.cleaned_data['temp_high'],
                form.cleaned_data['temprange_units'].encode())
            self.rmg.pressureDependence.Tcount = form.cleaned_data[
                'temp_interp']
            Tlist = getTemperaturesForModel(
                interpolation, self.rmg.pressureDependence.Tmin.value,
                self.rmg.pressureDependence.Tmax.value,
                self.rmg.pressureDependence.Tcount)
            self.rmg.pressureDependence.Tlist = Quantity(Tlist, "K")

            self.rmg.pressureDependence.Pmin = Quantity(
                form.cleaned_data['p_low'],
                form.cleaned_data['prange_units'].encode())
            self.rmg.pressureDependence.Pmax = Quantity(
                form.cleaned_data['p_high'],
                form.cleaned_data['prange_units'].encode())
            self.rmg.pressureDependence.Pcount = form.cleaned_data['p_interp']
            Plist = getPressuresForModel(
                interpolation, self.rmg.pressureDependence.Pmin.value,
                self.rmg.pressureDependence.Pmax.value,
                self.rmg.pressureDependence.Pcount)
            self.rmg.pressureDependence.Plist = Quantity(Plist, "Pa")

            # Process grain size and count
            self.rmg.pressureDependence.grainSize = Quantity(
                form.cleaned_data['maximumGrainSize'],
                form.cleaned_data['grainsize_units'].encode())
            self.rmg.pressureDependence.grainCount = form.cleaned_data[
                'minimumNumberOfGrains']

            # Process interpolation model
            self.rmg.pressureDependence.model = interpolation

        # Additional Options
        self.rmg.units = 'si'
        self.rmg.saveRestartPeriod = Quantity(
            form.cleaned_data['saveRestartPeriod'],
            form.cleaned_data['saveRestartPeriodUnits'].encode(
            )) if form.cleaned_data['saveRestartPeriod'] else None
        self.rmg.drawMolecules = form.cleaned_data['drawMolecules']
        self.rmg.generatePlots = form.cleaned_data['generatePlots']
        self.rmg.saveConcentrationProfiles = form.cleaned_data[
            'saveConcentrationProfiles']

        # Save the input.py file
        self.rmg.saveInput(self.savepath)
Ejemplo n.º 5
0
    if args.dictionary is not None:
        f = open(args.dictionary[0])
        adjlist = ''; label = ''
        for line in f:
            if len(line.strip()) == 0:
                if len(adjlist.strip()) > 0:
                    molecule = Molecule()
                    molecule.fromAdjacencyList(adjlist)
                    moleculeDict[label] = molecule
                adjlist = ''; label = ''
            else:
                if len(adjlist.strip()) == 0:
                    label = line.strip()
                adjlist += line
                    
        f.close()
    
    method = None

    for fstr in args.file:

        # Construct MEASURE job from FAME input
        measure = MEASURE()
        measure.loadFAMEInput(fstr, moleculeDict)

        # Save MEASURE input file based on the above
        dirname, basename = os.path.split(os.path.abspath(fstr))
        basename, ext = os.path.splitext(basename)
        path = os.path.join(dirname, basename + '.py')
        measure.saveInput(path)
Ejemplo n.º 6
0
class Network(models.Model):
    """
    A Django model of a pressure-dependent reaction network. 
    """
    def upload_input_to(instance, filename):
        # Always name the uploaded input file "input.py"
        return 'pdep/networks/{0}/input.py'.format(instance.pk)

    id = models.CharField(max_length=32, primary_key=True, default=_createId)
    title = models.CharField(max_length=50)
    inputFile = models.FileField(upload_to=upload_input_to,
                                 verbose_name='Input file')
    inputText = models.TextField(blank=True, verbose_name='')
    user = models.ForeignKey(User)

    def __init__(self, *args, **kwargs):
        super(Network, self).__init__(*args, **kwargs)
        self.measure = None

    def getDirname(self):
        """
        Return the absolute path of the directory that the Network object uses
        to store files.
        """
        return os.path.join(settings.MEDIA_ROOT, 'pdep', 'networks',
                            str(self.pk))

    def getInputFilename(self):
        """
        Return the absolute path of the input file.
        """
        return os.path.join(self.getDirname(), 'input.py')

    def getOutputFilename(self):
        """
        Return the absolute path of the output file.
        """
        return os.path.join(self.getDirname(), 'output.py')

    def getLogFilename(self):
        """
        Return the absolute path of the log file.
        """
        return os.path.join(self.getDirname(), 'MEASURE.log')

    def getSurfaceFilenamePNG(self):
        """
        Return the absolute path of the PES image file in PNG format.
        """
        return os.path.join(self.getDirname(), 'PES.png')

    def getSurfaceFilenamePDF(self):
        """
        Return the absolute path of the PES image file in PDF format.
        """
        return os.path.join(self.getDirname(), 'PES.pdf')

    def getSurfaceFilenameSVG(self):
        """
        Return the absolute path of the PES image file in SVG format.
        """
        return os.path.join(self.getDirname(), 'PES.svg')

    def getLastModifiedDate(self):
        """
        Return the date on which the network was most recently modified.
        """
        if not self.inputFileExists(): return 'unknown'

        mtime = os.path.getmtime(self.getInputFilename())
        if self.outputFileExists():
            mtime0 = os.path.getmtime(self.getOutputFilename())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFilePDFExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenamePDF())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFilePNGExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenamePNG())
            if mtime < mtime0: mtime = mtime0
        if self.surfaceFileSVGExists():
            mtime0 = os.path.getmtime(self.getSurfaceFilenameSVG())
            if mtime < mtime0: mtime = mtime0

        gmtime = time.gmtime(mtime)
        return time.strftime("%d %b %Y", gmtime)

    def inputFileExists(self):
        """
        Return ``True`` if the input file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getInputFilename())

    def outputFileExists(self):
        """
        Return ``True`` if the output file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getOutputFilename())

    def logFileExists(self):
        """
        Return ``True`` if the log file exists on the server or ``False`` if
        not.
        """
        return os.path.exists(self.getLogFilename())

    def surfaceFilePNGExists(self):
        """
        Return ``True`` if a potential energy surface PNG image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenamePNG())

    def surfaceFilePDFExists(self):
        """
        Return ``True`` if a potential energy surface PDF image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenamePDF())

    def surfaceFileSVGExists(self):
        """
        Return ``True`` if a potential energy surface SVG image file exists or
        ``False`` if not.
        """
        return os.path.exists(self.getSurfaceFilenameSVG())

    def outputFileOutOfDate(self):
        """
        Return ``True`` if the output file is out of date or ``False`` if
        not.
        """
        return self.outputFileExists() and os.path.getmtime(
            self.getInputFilename()) > os.path.getmtime(
                self.getOutputFilename())

    def surfaceFilePNGOutOfDate(self):
        """
        Return ``True`` if a potential energy surface PNG image file is out of 
        date or ``False`` if not.
        """
        return self.surfaceFilePNGExists() and os.path.getmtime(
            self.getInputFilename()) > os.path.getmtime(
                self.getSurfaceFilenamePNG())

    def surfaceFilePDFOutOfDate(self):
        """
        Return ``True`` if a potential energy surface PDF image file is out of
        date or ``False`` if not.
        """
        return self.surfaceFilePDFExists() and os.path.getmtime(
            self.getInputFilename()) > os.path.getmtime(
                self.getSurfaceFilenamePDF())

    def surfaceFileSVGOutOfDate(self):
        """
        Return ``True`` if a potential energy surface SVG image file is out of
        date or ``False`` if not.
        """
        return self.surfaceFileSVGExists() and os.path.getmtime(
            self.getInputFilename()) > os.path.getmtime(
                self.getSurfaceFilenameSVG())

    def createDir(self):
        """
        Create the directory (and any other needed parent directories) that
        the Network uses for storing files.
        """
        try:
            os.makedirs(self.getDirname())
        except OSError:
            # Fail silently on any OS errors
            pass

    def deleteInputFile(self):
        """
        Delete the input file for this network from the server.
        """
        if self.inputFileExists():
            os.remove(self.getInputFilename())

    def deleteOutputFile(self):
        """
        Delete the output file for this network from the server.
        """
        if self.outputFileExists():
            os.remove(self.getOutputFilename())

    def deleteSurfaceFilePNG(self):
        """
        Delete the PES image file in PNF format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenamePNG()):
            os.remove(self.getSurfaceFilenamePNG())

    def deleteSurfaceFilePDF(self):
        """
        Delete the PES image file in PDF format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenamePDF()):
            os.remove(self.getSurfaceFilenamePDF())

    def deleteSurfaceFileSVG(self):
        """
        Delete the PES image file in SVG format for this network from the 
        server.
        """
        if os.path.exists(self.getSurfaceFilenameSVG()):
            os.remove(self.getSurfaceFilenameSVG())

    def loadInputText(self):
        """
        Load the input file text into the inputText field.
        """
        self.inputText = ''
        if self.inputFileExists():
            f = open(self.getInputFilename(), 'r')
            for line in f:
                self.inputText += line
            f.close()

    def saveInputText(self):
        """
        Save the contents of the inputText field to the input file.
        """
        fpath = self.getInputFilename()
        self.createDir()
        f = open(fpath, 'w')
        for line in self.inputText.splitlines():
            f.write(line + '\n')
        f.close()

    def load(self):
        """
        Load the contents of the input and output files into a MEASURE object.
        """
        from rmgpy.measure.main import MEASURE

        self.measure = MEASURE()

        if self.outputFileExists():
            self.measure.loadOutput(self.getOutputFilename())
        elif self.inputFileExists():
            self.measure.loadInput(self.getInputFilename())

        if self.measure.network is not None:
            self.title = self.measure.network.title
            self.save()

        return self.measure.network