Exemplo n.º 1
0
    def Calc(self, f=None):
        # var
        # i,nbins,numtomake,m:integer
        # fin_conv, tau, beta, Cb, Cs, monmass, Me:double

        # get parameters
        tau = self.parameters['tau'].value
        beta = self.parameters['beta'].value
        Cb = self.parameters['Cb'].value
        Cs = self.parameters['Cs'].value
        fin_conv = self.parameters['fin_conv'].value
        numtomake = np.round(self.parameters['num_to_make'].value)
        monmass = self.parameters['mon_mass'].value
        Me = self.parameters['Me'].value
        nbins = int(np.round(self.parameters['nbin'].value))
        rch.set_do_prio_senio(ct.c_bool(self.do_priority_seniority))
        rch.set_flag_stop_all(ct.c_bool(False))

        c_ndist = ct.c_int()

        #resize theory datatable
        ft = f.data_table
        ft = f.data_table
        tt = self.tables[f.file_name_short]
        tt.num_columns = ft.num_columns
        tt.num_rows = 1
        tt.data = np.zeros((tt.num_rows, tt.num_columns))

        if not self.dist_exists:
            success = rch.request_dist(ct.byref(c_ndist))
            self.ndist = c_ndist.value
            if not success:  # no dist available
                #launch dialog asking for more dist
                self.signal_request_dist.emit(
                    self)  #use signal to open QDialog in the main GUI window
                return
            else:
                self.dist_exists = True
        ndist = self.ndist
        # rch.react_dist[ndist].name = self.reactname #TODO: set the dist name in the C library
        rch.react_dist[ndist].contents.M_e = Me
        rch.react_dist[ndist].contents.monmass = monmass
        rch.react_dist[ndist].contents.nummwdbins = nbins
        rch.react_dist[ndist].contents.polysaved = False
        rch.react_dist[ndist].contents.nsaved_arch = 0
        rch.react_dist[ndist].contents.arch_minwt = self.xmin
        rch.react_dist[ndist].contents.arch_maxwt = self.xmax
        rch.init_bin_prio_vs_senio(ndist)

        if self.simexists:
            rch.return_dist_polys(ct.c_int(ndist))
        # initialise tobita batch
        rch.tobbatchstart(ct.c_double(fin_conv), ct.c_double(tau),
                          ct.c_double(beta), ct.c_double(Cs), ct.c_double(Cb),
                          ct.c_int(ndist))
        rch.react_dist[ndist].contents.npoly = 0

        c_m = ct.c_int()

        # make numtomake polymers
        i = 0
        rate_print = np.trunc(numtomake / 20)
        self.Qprint('Making polymers:')
        self.Qprint('0% ', end='')
        while i < numtomake:
            if self.stop_theory_flag:
                self.Qprint(
                    '<br><big><font color=red><b>Polymer creation stopped by user</b></font></big>'
                )
                break
            # get a polymer
            success = rch.request_poly(ct.byref(c_m))
            m = c_m.value
            if success:  # check availability of polymers
                # put it in list

                if rch.react_dist[
                        ndist].contents.npoly == 0:  # case of first polymer made
                    rch.react_dist[ndist].contents.first_poly = m
                    rch.set_br_poly_nextpoly(
                        ct.c_int(m),
                        ct.c_int(0))  # br_poly[m].contents.nextpoly = 0
                else:  # next polymer, put to top of list
                    rch.set_br_poly_nextpoly(
                        ct.c_int(m),
                        ct.c_int(rch.react_dist[ndist].contents.first_poly)
                    )  # br_poly[m].contents.nextpoly = rch.react_dist[ndist].contents.first_poly
                    rch.react_dist[ndist].contents.first_poly = m

                # make a polymer
                if rch.tobbatch(ct.c_int(m), ct.c_int(
                        ndist)):  # routine returns false if arms ran out
                    rch.react_dist[ndist].contents.npoly += 1
                    i += 1
                    # check for error
                    if rch.tb_global.tobitabatcherrorflag:
                        self.Qprint(
                            '<br><big><font color=red><b>Polymers too large: gelation occurs for these parameters</b></font></big>'
                        )
                        i = numtomake
                else:  # error message if we ran out of arms
                    self.success_increase_memory = None
                    self.signal_request_arm.emit(self)
                    while self.success_increase_memory is None:  # wait for the end of QDialog
                        time.sleep(
                            0.5
                        )  # TODO: find a better way to wait for the dialog thread to finish
                    if self.success_increase_memory:
                        continue  # back to the start of while loop
                    else:
                        i = numtomake
                        rch.tb_global.tobitabatcherrorflag = True

                # update on number made
                if i % rate_print == 0:
                    self.Qprint('-', end='')
                    # needed to use Qprint if in single-thread
                    QApplication.processEvents()

            else:  # polymer wasn't available
                self.success_increase_memory = None
                self.signal_request_polymer.emit(self)
                while self.success_increase_memory is None:
                    time.sleep(
                        0.5
                    )  # TODO: find a better way to wait for the dialog thread to finish
                if self.success_increase_memory:
                    continue
                else:
                    i = numtomake
        # end make polymers loop
        if not rch.tb_global.tobitabatcherrorflag:
            self.Qprint('&nbsp;100%')

        calc = 0
        # do analysis of polymers made
        if (rch.react_dist[ndist].contents.npoly >=
                100) and (not rch.tb_global.tobitabatcherrorflag):
            rch.molbin(ndist)
            #resize theory datatable
            ft = f.data_table
            ft = f.data_table
            tt = self.tables[f.file_name_short]
            tt.num_columns = ft.num_columns + 2
            tt.num_rows = rch.react_dist[ndist].contents.nummwdbins
            tt.data = np.zeros((tt.num_rows, tt.num_columns))

            for i in range(1, rch.react_dist[ndist].contents.nummwdbins + 1):
                tt.data[i - 1,
                        0] = np.power(10,
                                      rch.react_dist[ndist].contents.lgmid[i])
                tt.data[i - 1, 1] = rch.react_dist[ndist].contents.wt[i]
                tt.data[i - 1, 2] = rch.react_dist[ndist].contents.avg[i]
                tt.data[i - 1, 3] = rch.react_dist[ndist].contents.avbr[i]

            rch.end_print(self, ndist, self.do_priority_seniority)
            rch.prio_and_senio(self, f, ndist, self.do_priority_seniority)

            calc = rch.react_dist[ndist].contents.nummwdbins - 1
            rch.react_dist[ndist].contents.polysaved = True

        self.simexists = True
        # self.Qprint(
        # '%d arm records left in memory' % rch.pb_global.arms_left)
        return calc
Exemplo n.º 2
0
    def Calc(self, f=None):
        """Template function that returns the square of y
        
        [description]
        
        Keyword Arguments:
            - f {[type]} -- [description] (default: {None})
        
        Returns:
            - [type] -- [description]
        """

        # get parameters
        col_time = self.parameters['col_time'].value
        tau = self.parameters['tau'].value
        kpM = self.parameters['kpM'].value
        kDLCB = self.parameters['kDLCB'].value
        kpLCB = self.parameters['kpLCB'].value
        kpD = self.parameters['kpD'].value
        keq = self.parameters['k='].value
        ks = self.parameters['ks'].value
        D0 = self.parameters['D0'].value
        C0 = self.parameters['C0'].value
        numtomake = np.round(self.parameters['num_to_make'].value)
        monmass = self.parameters['mon_mass'].value
        Me = self.parameters['Me'].value
        nbins = int(np.round(self.parameters['nbin'].value))
        rch.set_do_prio_senio(ct.c_bool(self.do_priority_seniority))
        rch.set_flag_stop_all(ct.c_bool(False))

        c_ndist = ct.c_int()

        #resize theory datatable
        ft = f.data_table
        ft = f.data_table
        tt = self.tables[f.file_name_short]
        tt.num_columns = ft.num_columns
        tt.num_rows = 1
        tt.data = np.zeros((tt.num_rows, tt.num_columns))

        if not self.dist_exists:
            success = rch.request_dist(ct.byref(c_ndist))
            self.ndist = c_ndist.value
            if not success:
                #launch dialog asking for more dist
                self.signal_request_dist.emit(
                    self)  #use signal to open QDialog in the main GUI window
                return
            else:
                self.dist_exists = True
        ndist = self.ndist
        # rch.react_dist[ndist].contents.name = self.reactname #TODO: set the dist name in the C library
        rch.react_dist[ndist].contents.M_e = Me
        rch.react_dist[ndist].contents.monmass = monmass
        rch.react_dist[ndist].contents.nummwdbins = nbins
        rch.react_dist[ndist].contents.polysaved = False
        rch.react_dist[ndist].contents.nsaved_arch = 0
        rch.react_dist[ndist].contents.arch_minwt = self.xmin
        rch.react_dist[ndist].contents.arch_maxwt = self.xmax
        rch.init_bin_prio_vs_senio(ndist)

        if self.simexists:
            rch.return_dist_polys(ct.c_int(ndist))

        # initialise diene batch
        ldiene = self.M_diene / monmass
        rch.dieneCSTRstart(ct.c_double(tau), ct.c_double(kpM), ct.c_double(kDLCB), ct.c_double(kpLCB), ct.c_double(kpD), ct.c_double(keq), ct.c_double(ks), ct.c_double(D0), ct.c_double(C0), ct.c_double(ldiene), ct.c_double(col_time), ct.c_int(ndist))
        rch.react_dist[ndist].contents.npoly = 0

        c_m = ct.c_int()

        # make numtomake polymers
        i = 0
        n_gel = 0
        rate_print = np.trunc(numtomake / 20)
        self.Qprint('Making polymers:')
        self.Qprint('0% ', end='')
        while i < numtomake:
            if self.stop_theory_flag:
                self.Qprint('<br><big><font color=red><b>Polymer creation stopped by user</b></font></big>')
                break
            # get a polymer
            success = rch.request_poly(ct.byref(c_m))
            m = c_m.value
            if success:  # check availability of polymers
                # put it in list
                # case of first polymer made
                if rch.react_dist[ndist].contents.npoly == 0:
                    rch.react_dist[ndist].contents.first_poly = m
                    #br_poly[m].contents.nextpoly = 0
                    rch.set_br_poly_nextpoly(ct.c_int(m), ct.c_int(0))
                else:  # next polymer, put to top of list
                    #br_poly[m].contents.nextpoly = rch.react_dist[ndist].contents.first_poly
                    rch.set_br_poly_nextpoly(
                        ct.c_int(m),
                        ct.c_int(rch.react_dist[ndist].contents.first_poly))
                    rch.react_dist[ndist].contents.first_poly = m

                # make a polymer
                # routine returns false if arms ran out
                if rch.dieneCSTR(ct.c_int(m), ct.c_int(ndist)):
                    rch.react_dist[ndist].contents.npoly += 1
                    i += 1
                    # check for error
                    if rch.dCSTR_global.dieneCSTRerrorflag:
                        n_gel += 1
                        rch.dCSTR_global.dieneCSTRerrorflag = False
                        # self.Qprint(
                        #     '<br><big><font color=red><b>Polymers too large: gelation occurs for these parameters</b></font></big>'
                        # )
                        # i = numtomake
                else:  # error message if we ran out of arms
                    self.success_increase_memory = None
                    self.signal_request_arm.emit(self)
                    # wait for the end of QDialog
                    while self.success_increase_memory is None:
                        # TODO: find a better way to wait for the dialog thread to finish
                        time.sleep(0.5)
                    if self.success_increase_memory:
                        continue  # back to the start of while loop
                    else:
                        i = numtomake
                        rch.dCSTR_global.dieneCSTRerrorflag = True

                # update on number made
                if i % rate_print == 0:
                    self.Qprint('-', end='')
                    # needed to use Qprint if in single-thread
                    QApplication.processEvents()  

            else:  # polymer wasn't available
                self.success_increase_memory = None
                self.signal_request_polymer.emit(self)
                while self.success_increase_memory is None:
                    # TODO: find a better way to wait for the dialog thread to finish
                    time.sleep(0.5)
                if self.success_increase_memory:
                    continue
                else:
                    i = numtomake
        # end make polymers loop
        if not rch.dCSTR_global.dieneCSTRerrorflag:
            self.Qprint('&nbsp;100%')

        calc = 0
        # do analysis of polymers made
        if (rch.react_dist[ndist].contents.npoly):
            rch.molbin(ndist)
            ft = f.data_table

            #resize theory data table
            ft = f.data_table
            tt = self.tables[f.file_name_short]
            tt.num_columns = ft.num_columns + 2
            tt.num_rows = rch.react_dist[ndist].contents.nummwdbins
            tt.data = np.zeros((tt.num_rows, tt.num_columns))

            for i in range(1, rch.react_dist[ndist].contents.nummwdbins + 1):
                tt.data[i - 1, 0] = np.power(
                    10, rch.react_dist[ndist].contents.lgmid[i])
                tt.data[i - 1, 1] = rch.react_dist[ndist].contents.wt[i]
                tt.data[i - 1, 2] = rch.react_dist[ndist].contents.avg[i]
                tt.data[i - 1, 3] = rch.react_dist[ndist].contents.avbr[i]
            rch.end_print(self, ndist, self.do_priority_seniority)
            rch.prio_and_senio(self, f, ndist, self.do_priority_seniority)

            calc = rch.react_dist[ndist].contents.nummwdbins - 1
            rch.react_dist[ndist].contents.polysaved = True
        self.simexists = True
        if n_gel != 0:
            self.Qprint('<br><big><font color=red><b>Gelation might occurs for these parameters.<br>%.3g%% of the molecules exceeded the maximum recursion level</b></font></big>' % (n_gel/numtomake*100.0))    
        return calc
Exemplo n.º 3
0
 def request_stop_computations(self):
     """Called when user wants to terminate the current computation"""
     rch.set_flag_stop_all(ct.c_bool(True))
     super().request_stop_computations()
Exemplo n.º 4
0
    def Calc(self, f=None):
        """MultiMetCSTR function that returns the square of y
        
        [description]
        
        Keyword Arguments:
            - f {[type]} -- [description] (default: {None})
        
        Returns:
            - [type] -- [description]
        """

        # get parameters
        numtomake = np.round(self.parameters['num_to_make'].value)
        monmass = self.parameters['mon_mass'].value
        Me = self.parameters['Me'].value
        nbins = int(np.round(self.parameters['nbin'].value))
        rch.set_do_prio_senio(ct.c_bool(self.do_priority_seniority))
        rch.set_flag_stop_all(ct.c_bool(False))

        c_ndist = ct.c_int()

        #resize theory datatable
        ft = f.data_table
        ft = f.data_table
        tt = self.tables[f.file_name_short]
        tt.num_columns = ft.num_columns
        tt.num_rows = 1
        tt.data = np.zeros((tt.num_rows, tt.num_columns))

        #request a dist
        if not self.dist_exists:
            success = rch.request_dist(ct.byref(c_ndist))
            self.ndist = c_ndist.value
            if not success:
                #launch dialog asking for more dist
                self.signal_request_dist.emit(
                    self)  #use signal to open QDialog in the main GUI window
                return
            else:
                self.dist_exists = True
        ndist = self.ndist
        # rch.react_dist[ndist].contents.name = self.reactname #TODO: set the dist name in the C library
        rch.react_dist[ndist].contents.npoly = 0
        rch.react_dist[ndist].contents.M_e = Me
        rch.react_dist[ndist].contents.monmass = monmass
        rch.react_dist[ndist].contents.nummwdbins = nbins
        rch.react_dist[ndist].contents.polysaved = False
        rch.react_dist[ndist].contents.nsaved_arch = 0
        rch.react_dist[ndist].contents.arch_minwt = self.xmin
        rch.react_dist[ndist].contents.arch_maxwt = self.xmax
        rch.init_bin_prio_vs_senio(ndist)

        if self.simexists:
            rch.return_dist_polys(ct.c_int(ndist))
        self.simexists = False

        #launch form
        self.success_dialog = None
        self.signal_mulmet_dialog.emit(self)
        while self.success_dialog is None:  # wait for the end of QDialog
            time.sleep(
                0.5
            )  # TODO: find a better way to wait for the dialog thread to finish
        if not self.success_dialog:
            return

        conc = (ct.c_double * self.numcat)()
        kp = (ct.c_double * self.numcat)()
        kdb = (ct.c_double * self.numcat)()
        ks = (ct.c_double * self.numcat)()
        kplcb = (ct.c_double * self.numcat)()
        for i in range(self.numcat):
            conc[i] = ct.c_double(float(self.pvalues[i][0]))
            kp[i] = ct.c_double(float(self.pvalues[i][1]))
            kdb[i] = ct.c_double(float(self.pvalues[i][2]))
            ks[i] = ct.c_double(float(self.pvalues[i][3]))
            kplcb[i] = ct.c_double(float(self.pvalues[i][4]))

        #initialise metallocene CSTR
        rch.mulmetCSTRstart(kp, kdb, ks, kplcb, conc,
                            ct.c_double(self.time_const),
                            ct.c_double(self.monomer_conc),
                            ct.c_int(self.numcat), ct.c_int(ndist),
                            ct.c_int(self.NUMCAT_MAX))

        c_m = ct.c_int()

        # make numtomake polymers
        i = 0
        rate_print = np.trunc(numtomake / 20)
        self.Qprint('Making polymers:')
        self.Qprint('0% ', end='')
        while i < numtomake:
            if self.stop_theory_flag:
                self.Qprint(
                    '<br><big><font color=red><b>Polymer creation stopped by user</b></font></big>'
                )
                break
            # get a polymer
            success = rch.request_poly(ct.byref(c_m))
            m = c_m.value
            if success:  # check availability of polymers
                # put it in list
                if rch.react_dist[
                        ndist].contents.npoly == 0:  # case of first polymer made
                    rch.react_dist[ndist].contents.first_poly = m
                    rch.set_br_poly_nextpoly(
                        ct.c_int(m),
                        ct.c_int(0))  #br_poly[m].contents.nextpoly = 0
                else:  # next polymer, put to top of list
                    rch.set_br_poly_nextpoly(
                        ct.c_int(m),
                        ct.c_int(rch.react_dist[ndist].contents.first_poly)
                    )  #br_poly[m].contents.nextpoly = rch.react_dist[ndist].contents.first_poly
                    rch.react_dist[ndist].contents.first_poly = m

                # make a polymer
                if rch.mulmetCSTR(ct.c_int(m), ct.c_int(
                        ndist)):  # routine returns false if arms ran out
                    rch.react_dist[ndist].contents.npoly += 1
                    i += 1
                    # check for error
                    if rch.MMCSTR_global.mulmetCSTRerrorflag:
                        self.Qprint(
                            '<br><big><font color=red><b>Polymers too large: gelation occurs for these parameters</b></font></big>'
                        )
                        i = numtomake
                else:  # error message if we ran out of arms
                    self.success_increase_memory = None
                    self.signal_request_arm.emit(self)
                    while self.success_increase_memory is None:  # wait for the end of QDialog
                        time.sleep(
                            0.5
                        )  # TODO: find a better way to wait for the dialog thread to finish
                    if self.success_increase_memory:
                        continue  # back to the start of while loop
                    else:
                        i = numtomake
                        rch.MMCSTR_global.mulmetCSTRerrorflag = True

                # update on number made
                if i % rate_print == 0:
                    self.Qprint('-', end='')
                    # needed to use Qprint if in single-thread
                    QApplication.processEvents()

            else:  # polymer wasn't available
                self.success_increase_memory = None
                self.signal_request_polymer.emit(self)
                while self.success_increase_memory is None:
                    time.sleep(
                        0.5
                    )  # TODO: find a better way to wait for the dialog thread to finish
                if self.success_increase_memory:
                    continue
                else:
                    i = numtomake
        # end make polymers loop
        if not rch.MMCSTR_global.mulmetCSTRerrorflag:
            self.Qprint('&nbsp;100%')

        calc = 0
        # do analysis of polymers made
        if (rch.react_dist[ndist].contents.npoly >=
                100) and (not rch.MMCSTR_global.mulmetCSTRerrorflag):
            rch.molbin(ndist)
            ft = f.data_table

            #resize theory data table
            ft = f.data_table
            tt = self.tables[f.file_name_short]
            tt.num_columns = ft.num_columns + 2
            tt.num_rows = rch.react_dist[ndist].contents.nummwdbins
            tt.data = np.zeros((tt.num_rows, tt.num_columns))

            for i in range(1, rch.react_dist[ndist].contents.nummwdbins + 1):
                tt.data[i - 1,
                        0] = np.power(10,
                                      rch.react_dist[ndist].contents.lgmid[i])
                tt.data[i - 1, 1] = rch.react_dist[ndist].contents.wt[i]
                tt.data[i - 1, 2] = rch.react_dist[ndist].contents.avg[i]
                tt.data[i - 1, 3] = rch.react_dist[ndist].contents.avbr[i]

            rch.end_print(self, ndist, self.do_priority_seniority)
            rch.prio_and_senio(self, f, ndist, self.do_priority_seniority)

            calc = rch.react_dist[ndist].contents.nummwdbins - 1
            rch.react_dist[ndist].contents.polysaved = True

        self.simexists = True
        # self.Qprint('%d arm records left in memory' % rch.pb_global.arms_left)
        return calc