Esempio n. 1
0
    def __init__(self, params=None, **kwargs):
        super(ChemostatSimple, self).__init__(**kwargs)
        if params == None:
            params = AttributeDict(kwargs)

        # Initialize update progress function
        self.updateProgress = params.updateProgress

        # Initialize parameters
        self.m = params.m
        self.K = params.K
        self.S_in = params.S_in
        self.X_in = params.X_in
        self.gamma = params.gamma
        self.D_vals = params.D_vals

        # Create state vector and derivative vector
        stateVarNames = ['S', 'X']
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize data storage
        self.resultStorage = ResultStorage(filePath=dataStorageFilePath,
                                           datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(varList=['t'] +
                                                 stateVarNames + ['D'],
                                                 chunkSize=1e4)

        # Register time event (changed of D)
        D_val = self.D_vals[0]
        self.D = D_val[1]

        tChangedD = D_val[0]
        for i in range(len(self.D_vals) - 1):
            self.D_val = self.D_vals[i + 1]
            self.timeEventRegistry.add(
                ChemostatSimpleTimeEvent(t=tChangedD,
                                         newValue_D=self.D_val[1]))
            tChangedD += self.D_val[0]

        # Set initial values of the states
        self.y.S = params.S0
        self.y.X = params.X0

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]
Esempio n. 2
0
	def __init__(self, params = None, **kwargs):
		super(ChemostatSimple, self).__init__(**kwargs)
		if params == None:
			params = AttributeDict(kwargs)
		
		# Initialize update progress function
		self.updateProgress = params.updateProgress
					
		# Initialize parameters
		self.m = params.m
		self.K = params.K
		self.S_in = params.S_in
		self.X_in = params.X_in
		self.gamma = params.gamma
		self.D_vals = params.D_vals

		# Create state vector and derivative vector
		stateVarNames = ['S', 'X'] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize data storage
		self.resultStorage = ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['t'] + stateVarNames + ['D'],
				chunkSize = 1e4)
		
		# Register time event (changed of D)
		D_val = self.D_vals[0]
		self.D = D_val[1]
		
		tChangedD = D_val[0]
		for i in range(len(self.D_vals)-1):
			self.D_val = self.D_vals[i+1]
			self.timeEventRegistry.add(ChemostatSimpleTimeEvent(t = tChangedD, newValue_D = self.D_val[1]))
			tChangedD += self.D_val[0]
		
		# Set initial values of the states
		self.y.S = params.S0
		self.y.X = params.X0
				
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
Esempio n. 3
0
class ChemostatSimple(Simulation):
	"""
	Class for implementation the model of a simple chemostat 
	"""
	name = 'Model of a simple chemostat.'
	
	def __init__(self, params = None, **kwargs):
		super(ChemostatSimple, self).__init__(**kwargs)
		if params == None:
			params = AttributeDict(kwargs)
		
		# Initialize update progress function
		self.updateProgress = params.updateProgress
					
		# Initialize parameters
		self.m = params.m
		self.K = params.K
		self.S_in = params.S_in
		self.X_in = params.X_in
		self.gamma = params.gamma
		self.D_vals = params.D_vals

		# Create state vector and derivative vector
		stateVarNames = ['S', 'X'] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize data storage
		self.resultStorage = ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['t'] + stateVarNames + ['D'],
				chunkSize = 1e4)
		
		# Register time event (changed of D)
		D_val = self.D_vals[0]
		self.D = D_val[1]
		
		tChangedD = D_val[0]
		for i in range(len(self.D_vals)-1):
			self.D_val = self.D_vals[i+1]
			self.timeEventRegistry.add(ChemostatSimpleTimeEvent(t = tChangedD, newValue_D = self.D_val[1]))
			tChangedD += self.D_val[0]
		
		# Set initial values of the states
		self.y.S = params.S0
		self.y.X = params.X0
				
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
		
	def mu(self, S):
		return self.m * S / (self.K + S)
		
	def rhs(self, t, y, sw):
		# Set state values
		self.y.set(y)
		
		try:
			# Get state values
			S = self.y.S
			X = self.y.X
			
			# Compute state derivatives
			S_dot = (self.S_in - S)*self.D - (1/self.gamma)*self.mu(S)*X
			X_dot = (self.X_in - X)*self.D + self.mu(S)*X
			
		except Exception, e:
			self.resultStorage.finalizeResult()
			# Log the error if it happens in the rhs() function
			print("Exception at time {}: {}".format(t, e))
			raise e
			
		self.yDot.S = S_dot
		self.yDot.X = X_dot
		return self.yDot.get()
Esempio n. 4
0
	def __init__(self, webModel, 
				paramsRH2, concentrsRH2, 
				paramsRCH4, concentrsRCH4,
				**kwargs):
		super(ADM1H2CH4Bioreactors, self).__init__(**kwargs)
		
		# Initialize update progress function
		self.updateProgress = webModel.updateProgress
					
		# Initialize parameters
		self.paramsRH2 = paramsRH2		
		self.paramsRCH4 = paramsRCH4	
				
		# Initialize concentrations
		self.concentrsRH2 = concentrsRH2
		self.concentrsRCH4 = concentrsRCH4
		
		# Create state vector and derivative vector
		stateVarNames = [
			'S_su_RH2', 'S_aa_RH2', 'S_fa_RH2', 'S_ac_RH2',
			'X_c_RH2', 'X_ch_RH2', 'X_pr_RH2', 'X_li_RH2', 'X_suaa_RH2', 'X_fa_RH2', 
			'intQ_h2_RH2',
			
			'S_ac_RCH4',
			'X_ac_RCH4', 
			'intQ_ch4_RCH4'
		] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize data storage
		self.resultStorage = ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['time'] + stateVarNames + ['Q_h2_RH2', 'Q_ch4_RCH4', 'D_RH2', 'D_RCH4'],
				chunkSize = 1e4
			)
		
		# Register time event (changed of D)
		tChangedD =  paramsRH2.D_liq_arr[0][0]
		self.D_RH2 =  paramsRH2.D_liq_arr[0][1]

		self.D_RCH4 = self.D_RH2 / paramsRCH4.V_liq_RCH4_del_V_liq_RH2
		
		for i in range(len(paramsRH2.D_liq_arr)-1):
			self.timeEventRegistry.add(
				ADM1TimeEvent(
					t = tChangedD,
					newValue_D = paramsRH2.D_liq_arr[i + 1][1]
				)
			)
			tChangedD += paramsRH2.D_liq_arr[i+1][0]
		
		# Set initial values of the states
		self.y.S_su_RH2 = concentrsRH2.S_su_0
		self.y.S_aa_RH2 = concentrsRH2.S_aa_0
		self.y.S_fa_RH2 = concentrsRH2.S_fa_0
		self.y.S_ac_RH2 = concentrsRH2.S_ac_0
		self.y.X_c_RH2 = concentrsRH2.X_c_0
		self.y.X_ch_RH2 = concentrsRH2.X_ch_0
		self.y.X_pr_RH2 = concentrsRH2.X_pr_0
		self.y.X_li_RH2 = concentrsRH2.X_li_0
		self.y.X_suaa_RH2 = concentrsRH2.X_suaa_0
		self.y.X_fa_RH2 = concentrsRH2.X_fa_0
		self.y.intQ_h2_RH2 = 0.0
		self.Q_h2_RH2 = 0.0
		
		self.y.S_ac_RCH4 = concentrsRCH4.S_ac_0
		self.y.X_ac_RCH4 = concentrsRCH4.X_ac_0
		self.y.intQ_ch4_RCH4 = 0.0
		self.Q_ch4_RCH4 = 0.0
				
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
Esempio n. 5
0
class ADM1H2CH4Bioreactors(Simulation):
	"""
	Class for implementation the model of a ADM1 H2 and CH4 Bioreactors
	"""
	name = 'Model of a bioreactor that produces hydrogen.'
	
	def __init__(self, webModel, 
				paramsRH2, concentrsRH2, 
				paramsRCH4, concentrsRCH4,
				**kwargs):
		super(ADM1H2CH4Bioreactors, self).__init__(**kwargs)
		
		# Initialize update progress function
		self.updateProgress = webModel.updateProgress
					
		# Initialize parameters
		self.paramsRH2 = paramsRH2		
		self.paramsRCH4 = paramsRCH4	
				
		# Initialize concentrations
		self.concentrsRH2 = concentrsRH2
		self.concentrsRCH4 = concentrsRCH4
		
		# Create state vector and derivative vector
		stateVarNames = [
			'S_su_RH2', 'S_aa_RH2', 'S_fa_RH2', 'S_ac_RH2',
			'X_c_RH2', 'X_ch_RH2', 'X_pr_RH2', 'X_li_RH2', 'X_suaa_RH2', 'X_fa_RH2', 
			'intQ_h2_RH2',
			
			'S_ac_RCH4',
			'X_ac_RCH4', 
			'intQ_ch4_RCH4'
		] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize data storage
		self.resultStorage = ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['time'] + stateVarNames + ['Q_h2_RH2', 'Q_ch4_RCH4', 'D_RH2', 'D_RCH4'],
				chunkSize = 1e4
			)
		
		# Register time event (changed of D)
		tChangedD =  paramsRH2.D_liq_arr[0][0]
		self.D_RH2 =  paramsRH2.D_liq_arr[0][1]

		self.D_RCH4 = self.D_RH2 / paramsRCH4.V_liq_RCH4_del_V_liq_RH2
		
		for i in range(len(paramsRH2.D_liq_arr)-1):
			self.timeEventRegistry.add(
				ADM1TimeEvent(
					t = tChangedD,
					newValue_D = paramsRH2.D_liq_arr[i + 1][1]
				)
			)
			tChangedD += paramsRH2.D_liq_arr[i+1][0]
		
		# Set initial values of the states
		self.y.S_su_RH2 = concentrsRH2.S_su_0
		self.y.S_aa_RH2 = concentrsRH2.S_aa_0
		self.y.S_fa_RH2 = concentrsRH2.S_fa_0
		self.y.S_ac_RH2 = concentrsRH2.S_ac_0
		self.y.X_c_RH2 = concentrsRH2.X_c_0
		self.y.X_ch_RH2 = concentrsRH2.X_ch_0
		self.y.X_pr_RH2 = concentrsRH2.X_pr_0
		self.y.X_li_RH2 = concentrsRH2.X_li_0
		self.y.X_suaa_RH2 = concentrsRH2.X_suaa_0
		self.y.X_fa_RH2 = concentrsRH2.X_fa_0
		self.y.intQ_h2_RH2 = 0.0
		self.Q_h2_RH2 = 0.0
		
		self.y.S_ac_RCH4 = concentrsRCH4.S_ac_0
		self.y.X_ac_RCH4 = concentrsRCH4.X_ac_0
		self.y.intQ_ch4_RCH4 = 0.0
		self.Q_ch4_RCH4 = 0.0
				
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
		
	def rhs(self, t, y, sw):
		paramsRH2 = self.paramsRH2
		concentrsRH2 = self.concentrsRH2
	
		paramsRCH4 = self.paramsRCH4
		concentrsRCH4 = self.concentrsRCH4
		
		# Set state values
		self.y.set(y)
		
		try:
		# Bioreactor - RH2	
			# Get state values
			S_su_RH2 = self.y.S_su_RH2
			S_aa_RH2 = self.y.S_aa_RH2
			S_fa_RH2 = self.y.S_fa_RH2
			S_ac_RH2 = self.y.S_ac_RH2
			X_c_RH2 = self.y.X_c_RH2
			X_ch_RH2 = self.y.X_ch_RH2
			X_pr_RH2 = self.y.X_pr_RH2
			X_li_RH2 = self.y.X_li_RH2
			X_suaa_RH2 = self.y.X_suaa_RH2
			X_fa_RH2 = self.y.X_fa_RH2
			
			# Compute biochemical process rates
			r1 = paramsRH2.k_dis * X_c_RH2
			r2 = paramsRH2.k_hyd_ch * X_ch_RH2
			r3 = paramsRH2.k_hyd_pr * X_pr_RH2
			r4 = paramsRH2.k_hyd_li * X_li_RH2
			r5 = paramsRH2.k_m_suaa * (S_su_RH2 / (paramsRH2.K_S_suaa + S_su_RH2)) * X_suaa_RH2 * (S_su_RH2/(S_su_RH2+S_aa_RH2))
			r6 = paramsRH2.k_m_suaa * (S_aa_RH2 / (paramsRH2.K_S_suaa + S_aa_RH2)) * X_suaa_RH2 * (S_aa_RH2/(S_su_RH2+S_aa_RH2))
			r7 = paramsRH2.k_m_fa * (S_fa_RH2 / (paramsRH2.K_S_fa + S_fa_RH2)) * X_fa_RH2
			
			# Compute state derivatives		
			S_su_RH2_dot = self.D_RH2*(concentrsRH2.S_su_in - S_su_RH2) \
				+ r2 + paramsRH2.f_su_li*r4 - r5 #1.1
			S_aa_RH2_dot = self.D_RH2*(concentrsRH2.S_aa_in - S_aa_RH2) \
				+ r3 - r6 #2.1
			S_fa_RH2_dot = self.D_RH2*(concentrsRH2.S_fa_in - S_fa_RH2) \
				+ paramsRH2.f_fa_li*r4 - r7 #3.1
			S_ac_RH2_dot = self.D_RH2*(concentrsRH2.S_ac_in - S_ac_RH2) \
				+ (1 - paramsRH2.Y_suaa)*paramsRH2.f_ac_su*r5 \
				+ (1 - paramsRH2.Y_suaa)*paramsRH2.f_ac_aa*r6 \
				+ (1 - paramsRH2.Y_fa)*0.7*r7 #7.1
			X_c_RH2_dot = self.D_RH2*(concentrsRH2.X_c_in - X_c_RH2) \
				- r1 #13.1
			X_ch_RH2_dot = self.D_RH2*(concentrsRH2.X_ch_in - X_ch_RH2) \
				+ paramsRH2.f_ch_xc*r1 - r2 #14.1
			X_pr_RH2_dot = self.D_RH2*(concentrsRH2.X_pr_in - X_pr_RH2) \
				+ paramsRH2.f_pr_xc*r1 - r3 #15.1
			X_li_RH2_dot = self.D_RH2*(concentrsRH2.X_li_in - X_li_RH2) \
				+ paramsRH2.f_li_xc*r1 - r4 #16.1
			X_suaa_RH2_dot = self.D_RH2*(concentrsRH2.X_suaa_in - X_suaa_RH2) \
				+ paramsRH2.Y_suaa*r5 \
				+ paramsRH2.Y_suaa*r6 #17.1
			X_fa_RH2_dot = self.D_RH2*(concentrsRH2.X_fa_in - X_fa_RH2) \
				+ paramsRH2.Y_fa*r7 #19.1
		
			self.Q_h2_RH2 = paramsRH2.Y_h2_su * r5 \
				+ paramsRH2.Y_h2_aa * r6 \
				+ paramsRH2.Y_h2_fa * r7
			intQ_h2_RH2_dot = self.Q_h2_RH2
			
			# Set state derivatives
			self.yDot.S_su_RH2 = S_su_RH2_dot
			self.yDot.S_aa_RH2 = S_aa_RH2_dot
			self.yDot.S_fa_RH2 = S_fa_RH2_dot
			self.yDot.S_ac_RH2 = S_ac_RH2_dot
			self.yDot.X_c_RH2 = X_c_RH2_dot
			self.yDot.X_ch_RH2 = X_ch_RH2_dot
			self.yDot.X_pr_RH2 = X_pr_RH2_dot
			self.yDot.X_li_RH2 = X_li_RH2_dot
			self.yDot.X_suaa_RH2 = X_suaa_RH2_dot
			self.yDot.X_fa_RH2 = X_fa_RH2_dot
			self.yDot.intQ_h2_RH2 = intQ_h2_RH2_dot
		
		# Bioreactor - RCH4
			# Get state values
			S_ac_RCH4 = self.y.S_ac_RCH4
			X_ac_RCH4 = self.y.X_ac_RCH4
			
			# Compute biochemical process rates
			r11 = paramsRCH4.k_m_ac * (S_ac_RCH4 / (paramsRCH4.K_S_ac + S_ac_RCH4)) * X_ac_RCH4
			
			# Compute state derivatives		
			S_ac_RCH4_dot = self.D_RCH4*(S_ac_RH2 - S_ac_RCH4) \
				-r11#7.2
			X_ac_RCH4_dot = self.D_RCH4*(concentrsRCH4.X_ac_in - X_ac_RCH4) \
				+ paramsRCH4.Y_ac * r11 #22.2
			self.Q_ch4_RCH4 = paramsRCH4.Y_ch4_ac * r11 
			intQ_ch4_RCH4_dot = self.Q_ch4_RCH4
			
			# Set state derivatives
			self.yDot.S_ac_RCH4 = S_ac_RCH4_dot
			self.yDot.X_ac_RCH4 = X_ac_RCH4_dot
			self.yDot.intQ_ch4_RCH4 = intQ_ch4_RCH4_dot
			
		except Exception, e:
			self.resultStorage.finalizeResult()
			# Log the error if it happens in the rhs() function
			print("Exception at time {}: {}".format(t, e))
			raise e
		
		return self.yDot.get()
Esempio n. 6
0
    def __init__(self, webModel, paramsRH2, concentrsRH2, paramsRCH4,
                 concentrsRCH4, **kwargs):
        super(ADM1H2CH4Bioreactors, self).__init__(**kwargs)

        # Initialize update progress function
        self.updateProgress = webModel.updateProgress

        # Initialize parameters
        self.paramsRH2 = paramsRH2
        self.paramsRCH4 = paramsRCH4

        # Initialize concentrations
        self.concentrsRH2 = concentrsRH2
        self.concentrsRCH4 = concentrsRCH4

        # Create state vector and derivative vector
        stateVarNames = [
            'S_su_RH2', 'S_aa_RH2', 'S_fa_RH2', 'S_ac_RH2', 'X_c_RH2',
            'X_ch_RH2', 'X_pr_RH2', 'X_li_RH2', 'X_suaa_RH2', 'X_fa_RH2',
            'intQ_h2_RH2', 'S_ac_RCH4', 'X_ac_RCH4', 'intQ_ch4_RCH4'
        ]
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize data storage
        self.resultStorage = ResultStorage(filePath=dataStorageFilePath,
                                           datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(
                varList=['time'] + stateVarNames +
                ['Q_h2_RH2', 'Q_ch4_RCH4', 'D_RH2', 'D_RCH4'],
                chunkSize=1e4)

        # Register time event (changed of D)
        tChangedD = paramsRH2.D_liq_arr[0][0]
        self.D_RH2 = paramsRH2.D_liq_arr[0][1]

        self.D_RCH4 = self.D_RH2 / paramsRCH4.V_liq_RCH4_del_V_liq_RH2

        for i in range(len(paramsRH2.D_liq_arr) - 1):
            self.timeEventRegistry.add(
                ADM1TimeEvent(t=tChangedD,
                              newValue_D=paramsRH2.D_liq_arr[i + 1][1]))
            tChangedD += paramsRH2.D_liq_arr[i + 1][0]

        # Set initial values of the states
        self.y.S_su_RH2 = concentrsRH2.S_su_0
        self.y.S_aa_RH2 = concentrsRH2.S_aa_0
        self.y.S_fa_RH2 = concentrsRH2.S_fa_0
        self.y.S_ac_RH2 = concentrsRH2.S_ac_0
        self.y.X_c_RH2 = concentrsRH2.X_c_0
        self.y.X_ch_RH2 = concentrsRH2.X_ch_0
        self.y.X_pr_RH2 = concentrsRH2.X_pr_0
        self.y.X_li_RH2 = concentrsRH2.X_li_0
        self.y.X_suaa_RH2 = concentrsRH2.X_suaa_0
        self.y.X_fa_RH2 = concentrsRH2.X_fa_0
        self.y.intQ_h2_RH2 = 0.0
        self.Q_h2_RH2 = 0.0

        self.y.S_ac_RCH4 = concentrsRCH4.S_ac_0
        self.y.X_ac_RCH4 = concentrsRCH4.X_ac_0
        self.y.intQ_ch4_RCH4 = 0.0
        self.Q_ch4_RCH4 = 0.0

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]
Esempio n. 7
0
class ChemostatSimple(Simulation):
    """
	Class for implementation the model of a simple chemostat 
	"""
    name = 'Model of a simple chemostat.'

    def __init__(self, params=None, **kwargs):
        super(ChemostatSimple, self).__init__(**kwargs)
        if params == None:
            params = AttributeDict(kwargs)

        # Initialize update progress function
        self.updateProgress = params.updateProgress

        # Initialize parameters
        self.m = params.m
        self.K = params.K
        self.S_in = params.S_in
        self.X_in = params.X_in
        self.gamma = params.gamma
        self.D_vals = params.D_vals

        # Create state vector and derivative vector
        stateVarNames = ['S', 'X']
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize data storage
        self.resultStorage = ResultStorage(filePath=dataStorageFilePath,
                                           datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(varList=['t'] +
                                                 stateVarNames + ['D'],
                                                 chunkSize=1e4)

        # Register time event (changed of D)
        D_val = self.D_vals[0]
        self.D = D_val[1]

        tChangedD = D_val[0]
        for i in range(len(self.D_vals) - 1):
            self.D_val = self.D_vals[i + 1]
            self.timeEventRegistry.add(
                ChemostatSimpleTimeEvent(t=tChangedD,
                                         newValue_D=self.D_val[1]))
            tChangedD += self.D_val[0]

        # Set initial values of the states
        self.y.S = params.S0
        self.y.X = params.X0

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]

    def mu(self, S):
        return self.m * S / (self.K + S)

    def rhs(self, t, y, sw):
        # Set state values
        self.y.set(y)

        try:
            # Get state values
            S = self.y.S
            X = self.y.X

            # Compute state derivatives
            S_dot = (self.S_in - S) * self.D - (1 /
                                                self.gamma) * self.mu(S) * X
            X_dot = (self.X_in - X) * self.D + self.mu(S) * X

        except Exception, e:
            self.resultStorage.finalizeResult()
            # Log the error if it happens in the rhs() function
            print("Exception at time {}: {}".format(t, e))
            raise e

        self.yDot.S = S_dot
        self.yDot.X = X_dot
        return self.yDot.get()
Esempio n. 8
0
class ADM1H2CH4Bioreactors(Simulation):
    """
	Class for implementation the model of a ADM1 H2 and CH4 Bioreactors
	"""
    name = 'Model of a bioreactor that produces hydrogen.'

    def __init__(self, webModel, paramsRH2, concentrsRH2, paramsRCH4,
                 concentrsRCH4, **kwargs):
        super(ADM1H2CH4Bioreactors, self).__init__(**kwargs)

        # Initialize update progress function
        self.updateProgress = webModel.updateProgress

        # Initialize parameters
        self.paramsRH2 = paramsRH2
        self.paramsRCH4 = paramsRCH4

        # Initialize concentrations
        self.concentrsRH2 = concentrsRH2
        self.concentrsRCH4 = concentrsRCH4

        # Create state vector and derivative vector
        stateVarNames = [
            'S_su_RH2', 'S_aa_RH2', 'S_fa_RH2', 'S_ac_RH2', 'X_c_RH2',
            'X_ch_RH2', 'X_pr_RH2', 'X_li_RH2', 'X_suaa_RH2', 'X_fa_RH2',
            'intQ_h2_RH2', 'S_ac_RCH4', 'X_ac_RCH4', 'intQ_ch4_RCH4'
        ]
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize data storage
        self.resultStorage = ResultStorage(filePath=dataStorageFilePath,
                                           datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(
                varList=['time'] + stateVarNames +
                ['Q_h2_RH2', 'Q_ch4_RCH4', 'D_RH2', 'D_RCH4'],
                chunkSize=1e4)

        # Register time event (changed of D)
        tChangedD = paramsRH2.D_liq_arr[0][0]
        self.D_RH2 = paramsRH2.D_liq_arr[0][1]

        self.D_RCH4 = self.D_RH2 / paramsRCH4.V_liq_RCH4_del_V_liq_RH2

        for i in range(len(paramsRH2.D_liq_arr) - 1):
            self.timeEventRegistry.add(
                ADM1TimeEvent(t=tChangedD,
                              newValue_D=paramsRH2.D_liq_arr[i + 1][1]))
            tChangedD += paramsRH2.D_liq_arr[i + 1][0]

        # Set initial values of the states
        self.y.S_su_RH2 = concentrsRH2.S_su_0
        self.y.S_aa_RH2 = concentrsRH2.S_aa_0
        self.y.S_fa_RH2 = concentrsRH2.S_fa_0
        self.y.S_ac_RH2 = concentrsRH2.S_ac_0
        self.y.X_c_RH2 = concentrsRH2.X_c_0
        self.y.X_ch_RH2 = concentrsRH2.X_ch_0
        self.y.X_pr_RH2 = concentrsRH2.X_pr_0
        self.y.X_li_RH2 = concentrsRH2.X_li_0
        self.y.X_suaa_RH2 = concentrsRH2.X_suaa_0
        self.y.X_fa_RH2 = concentrsRH2.X_fa_0
        self.y.intQ_h2_RH2 = 0.0
        self.Q_h2_RH2 = 0.0

        self.y.S_ac_RCH4 = concentrsRCH4.S_ac_0
        self.y.X_ac_RCH4 = concentrsRCH4.X_ac_0
        self.y.intQ_ch4_RCH4 = 0.0
        self.Q_ch4_RCH4 = 0.0

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]

    def rhs(self, t, y, sw):
        paramsRH2 = self.paramsRH2
        concentrsRH2 = self.concentrsRH2

        paramsRCH4 = self.paramsRCH4
        concentrsRCH4 = self.concentrsRCH4

        # Set state values
        self.y.set(y)

        try:
            # Bioreactor - RH2
            # Get state values
            S_su_RH2 = self.y.S_su_RH2
            S_aa_RH2 = self.y.S_aa_RH2
            S_fa_RH2 = self.y.S_fa_RH2
            S_ac_RH2 = self.y.S_ac_RH2
            X_c_RH2 = self.y.X_c_RH2
            X_ch_RH2 = self.y.X_ch_RH2
            X_pr_RH2 = self.y.X_pr_RH2
            X_li_RH2 = self.y.X_li_RH2
            X_suaa_RH2 = self.y.X_suaa_RH2
            X_fa_RH2 = self.y.X_fa_RH2

            # Compute biochemical process rates
            r1 = paramsRH2.k_dis * X_c_RH2
            r2 = paramsRH2.k_hyd_ch * X_ch_RH2
            r3 = paramsRH2.k_hyd_pr * X_pr_RH2
            r4 = paramsRH2.k_hyd_li * X_li_RH2
            r5 = paramsRH2.k_m_suaa * (S_su_RH2 /
                                       (paramsRH2.K_S_suaa + S_su_RH2)
                                       ) * X_suaa_RH2 * (S_su_RH2 /
                                                         (S_su_RH2 + S_aa_RH2))
            r6 = paramsRH2.k_m_suaa * (S_aa_RH2 /
                                       (paramsRH2.K_S_suaa + S_aa_RH2)
                                       ) * X_suaa_RH2 * (S_aa_RH2 /
                                                         (S_su_RH2 + S_aa_RH2))
            r7 = paramsRH2.k_m_fa * (S_fa_RH2 /
                                     (paramsRH2.K_S_fa + S_fa_RH2)) * X_fa_RH2

            # Compute state derivatives
            S_su_RH2_dot = self.D_RH2*(concentrsRH2.S_su_in - S_su_RH2) \
             + r2 + paramsRH2.f_su_li*r4 - r5 #1.1
            S_aa_RH2_dot = self.D_RH2*(concentrsRH2.S_aa_in - S_aa_RH2) \
             + r3 - r6 #2.1
            S_fa_RH2_dot = self.D_RH2*(concentrsRH2.S_fa_in - S_fa_RH2) \
             + paramsRH2.f_fa_li*r4 - r7 #3.1
            S_ac_RH2_dot = self.D_RH2*(concentrsRH2.S_ac_in - S_ac_RH2) \
             + (1 - paramsRH2.Y_suaa)*paramsRH2.f_ac_su*r5 \
             + (1 - paramsRH2.Y_suaa)*paramsRH2.f_ac_aa*r6 \
             + (1 - paramsRH2.Y_fa)*0.7*r7 #7.1
            X_c_RH2_dot = self.D_RH2*(concentrsRH2.X_c_in - X_c_RH2) \
             - r1 #13.1
            X_ch_RH2_dot = self.D_RH2*(concentrsRH2.X_ch_in - X_ch_RH2) \
             + paramsRH2.f_ch_xc*r1 - r2 #14.1
            X_pr_RH2_dot = self.D_RH2*(concentrsRH2.X_pr_in - X_pr_RH2) \
             + paramsRH2.f_pr_xc*r1 - r3 #15.1
            X_li_RH2_dot = self.D_RH2*(concentrsRH2.X_li_in - X_li_RH2) \
             + paramsRH2.f_li_xc*r1 - r4 #16.1
            X_suaa_RH2_dot = self.D_RH2*(concentrsRH2.X_suaa_in - X_suaa_RH2) \
             + paramsRH2.Y_suaa*r5 \
             + paramsRH2.Y_suaa*r6 #17.1
            X_fa_RH2_dot = self.D_RH2*(concentrsRH2.X_fa_in - X_fa_RH2) \
             + paramsRH2.Y_fa*r7 #19.1

            self.Q_h2_RH2 = paramsRH2.Y_h2_su * r5 \
             + paramsRH2.Y_h2_aa * r6 \
             + paramsRH2.Y_h2_fa * r7
            intQ_h2_RH2_dot = self.Q_h2_RH2

            # Set state derivatives
            self.yDot.S_su_RH2 = S_su_RH2_dot
            self.yDot.S_aa_RH2 = S_aa_RH2_dot
            self.yDot.S_fa_RH2 = S_fa_RH2_dot
            self.yDot.S_ac_RH2 = S_ac_RH2_dot
            self.yDot.X_c_RH2 = X_c_RH2_dot
            self.yDot.X_ch_RH2 = X_ch_RH2_dot
            self.yDot.X_pr_RH2 = X_pr_RH2_dot
            self.yDot.X_li_RH2 = X_li_RH2_dot
            self.yDot.X_suaa_RH2 = X_suaa_RH2_dot
            self.yDot.X_fa_RH2 = X_fa_RH2_dot
            self.yDot.intQ_h2_RH2 = intQ_h2_RH2_dot

            # Bioreactor - RCH4
            # Get state values
            S_ac_RCH4 = self.y.S_ac_RCH4
            X_ac_RCH4 = self.y.X_ac_RCH4

            # Compute biochemical process rates
            r11 = paramsRCH4.k_m_ac * (
                S_ac_RCH4 / (paramsRCH4.K_S_ac + S_ac_RCH4)) * X_ac_RCH4

            # Compute state derivatives
            S_ac_RCH4_dot = self.D_RCH4*(S_ac_RH2 - S_ac_RCH4) \
             -r11#7.2
            X_ac_RCH4_dot = self.D_RCH4*(concentrsRCH4.X_ac_in - X_ac_RCH4) \
             + paramsRCH4.Y_ac * r11 #22.2
            self.Q_ch4_RCH4 = paramsRCH4.Y_ch4_ac * r11
            intQ_ch4_RCH4_dot = self.Q_ch4_RCH4

            # Set state derivatives
            self.yDot.S_ac_RCH4 = S_ac_RCH4_dot
            self.yDot.X_ac_RCH4 = X_ac_RCH4_dot
            self.yDot.intQ_ch4_RCH4 = intQ_ch4_RCH4_dot

        except Exception, e:
            self.resultStorage.finalizeResult()
            # Log the error if it happens in the rhs() function
            print("Exception at time {}: {}".format(t, e))
            raise e

        return self.yDot.get()
Esempio n. 9
0
    def __init__(self, params=None, **kwargs):
        super(TankModel, self).__init__(**kwargs)
        if params == None:
            params = AttributeDict(kwargs)

        # Initialize update progress function
        self.updateProgress = params.updateProgress

        # Create state vector and derivative vector
        stateVarNames = [
            'WRealCompressor', 'TTank', 'rhoTank', 'TLiner_1', 'TLiner_2',
            'TComp_1', 'TComp_2', 'TComp_3', 'TComp_4'
        ]
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize storage
        self.resultStorage = DMC.ResultStorage(
            filePath=dataStorageFilePath, datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(
                varList=['t'] + stateVarNames + [
                    'pTank', 'mTank', 'TCompressorOut', 'TCoolerOut',
                    'QDotCooler', 'QDotComp', 'mDotFueling'
                ],
                chunkSize=10000)

        # Fueling source
        self.fuelingSource = DM.FluidStateSource(params.fuelingSource)
        self.fuelingSource.initState(params.fuelingSource)

        # Compressor
        self.compressor = DM.Compressor(params.compressor)
        # Connect the compressor to the fluid source
        self.compressor.portIn.connect(self.fuelingSource.port1)

        # Cooler
        self.cooler = DM.FluidHeater(params.cooler)
        self.cooler.setEffectivnessModel(epsilon=params.cooler.epsilon)
        # Connect the cooler to the compressor
        self.cooler.portIn.connect(self.compressor.portOut)
        # Connect the cooler to the heater (i.e. temperature source)
        self.coolerTemperatureSource = DMS.ThermalPort(
            'C', DMS.ThermalState(T=params.coolerTemperatureSource.T))
        self.cooler.thermalPort.connect(self.coolerTemperatureSource)

        # Tank chamber
        self.tank = DM.FluidChamber(params.tank)
        self.tank.initialize(params.tank.TInit, params.tank.pInit)
        # Connect the tank to the cooler
        self.tank.fluidPort.connect(self.cooler.portOut)

        # Extraction sink
        self.extractionSink = DM.FlowSource(params.extractionSink)
        # Connect the extraction sink with the tank
        self.extractionSink.port1.connect(self.tank.fluidPort)

        # Tank convection component
        self.tankInternalConvection = DM.ConvectionHeatTransfer(
            params.tankInternalConvection)
        # Connect to the fluid in the tank
        self.tankInternalConvection.fluidPort.connect(self.tank.fluidPort)

        # Tank (Liner)
        self.liner = DM.SolidConductiveBody(params.liner)
        # Connect to the tank convection component
        self.tankInternalConvection.wallPort.connect(self.liner.port1)

        # Tank (composite)
        self.composite = DM.SolidConductiveBody(params.composite)
        # Connect the liner to the composite
        self.composite.port1.connect(self.liner.port2)

        # Ambient fluid component
        self.ambientSource = DM.FluidStateSource(params.ambientSource)
        self.ambientSource.initState(params.ambientSource)

        # Composite convection component
        self.tankExternalConvection = DM.ConvectionHeatTransfer(
            params.tankExternalConvection)
        # Connect the composite convection to the ambient fluid
        self.tankExternalConvection.fluidPort.connect(self.ambientSource.port1)
        # Connect the composite convection to the composite
        self.tankExternalConvection.wallPort.connect(self.composite.port2)

        # Controller
        self.controller = params.controller

        # Initialize the state variables
        self.y.WRealCompressor = 0.  # [W]
        self.y.TLiner_1 = self.liner.T[0]
        self.y.TLiner_2 = self.liner.T[1]
        self.y.TComp_1 = self.composite.T[0]
        self.y.TComp_2 = self.composite.T[1]
        self.y.TComp_3 = self.composite.T[2]
        self.y.TComp_4 = self.composite.T[3]
        self.y.TTank = self.tank.fState.T
        self.y.rhoTank = self.tank.fState.rho

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]
Esempio n. 10
0
class TankModel(DMC.Simulation):
    name = 'Model of a tank fueling and extraction'

    def __init__(self, params=None, **kwargs):
        super(TankModel, self).__init__(**kwargs)
        if params == None:
            params = AttributeDict(kwargs)

        # Initialize update progress function
        self.updateProgress = params.updateProgress

        # Create state vector and derivative vector
        stateVarNames = [
            'WRealCompressor', 'TTank', 'rhoTank', 'TLiner_1', 'TLiner_2',
            'TComp_1', 'TComp_2', 'TComp_3', 'TComp_4'
        ]
        self.y = NamedStateVector(stateVarNames)
        self.yRes = NamedStateVector(stateVarNames)
        self.yDot = NamedStateVector(stateVarNames)

        # Initialize storage
        self.resultStorage = DMC.ResultStorage(
            filePath=dataStorageFilePath, datasetPath=dataStorageDatasetPath)
        if (kwargs.get('initDataStorage', True)):
            self.resultStorage.initializeWriting(
                varList=['t'] + stateVarNames + [
                    'pTank', 'mTank', 'TCompressorOut', 'TCoolerOut',
                    'QDotCooler', 'QDotComp', 'mDotFueling'
                ],
                chunkSize=10000)

        # Fueling source
        self.fuelingSource = DM.FluidStateSource(params.fuelingSource)
        self.fuelingSource.initState(params.fuelingSource)

        # Compressor
        self.compressor = DM.Compressor(params.compressor)
        # Connect the compressor to the fluid source
        self.compressor.portIn.connect(self.fuelingSource.port1)

        # Cooler
        self.cooler = DM.FluidHeater(params.cooler)
        self.cooler.setEffectivnessModel(epsilon=params.cooler.epsilon)
        # Connect the cooler to the compressor
        self.cooler.portIn.connect(self.compressor.portOut)
        # Connect the cooler to the heater (i.e. temperature source)
        self.coolerTemperatureSource = DMS.ThermalPort(
            'C', DMS.ThermalState(T=params.coolerTemperatureSource.T))
        self.cooler.thermalPort.connect(self.coolerTemperatureSource)

        # Tank chamber
        self.tank = DM.FluidChamber(params.tank)
        self.tank.initialize(params.tank.TInit, params.tank.pInit)
        # Connect the tank to the cooler
        self.tank.fluidPort.connect(self.cooler.portOut)

        # Extraction sink
        self.extractionSink = DM.FlowSource(params.extractionSink)
        # Connect the extraction sink with the tank
        self.extractionSink.port1.connect(self.tank.fluidPort)

        # Tank convection component
        self.tankInternalConvection = DM.ConvectionHeatTransfer(
            params.tankInternalConvection)
        # Connect to the fluid in the tank
        self.tankInternalConvection.fluidPort.connect(self.tank.fluidPort)

        # Tank (Liner)
        self.liner = DM.SolidConductiveBody(params.liner)
        # Connect to the tank convection component
        self.tankInternalConvection.wallPort.connect(self.liner.port1)

        # Tank (composite)
        self.composite = DM.SolidConductiveBody(params.composite)
        # Connect the liner to the composite
        self.composite.port1.connect(self.liner.port2)

        # Ambient fluid component
        self.ambientSource = DM.FluidStateSource(params.ambientSource)
        self.ambientSource.initState(params.ambientSource)

        # Composite convection component
        self.tankExternalConvection = DM.ConvectionHeatTransfer(
            params.tankExternalConvection)
        # Connect the composite convection to the ambient fluid
        self.tankExternalConvection.fluidPort.connect(self.ambientSource.port1)
        # Connect the composite convection to the composite
        self.tankExternalConvection.wallPort.connect(self.composite.port2)

        # Controller
        self.controller = params.controller

        # Initialize the state variables
        self.y.WRealCompressor = 0.  # [W]
        self.y.TLiner_1 = self.liner.T[0]
        self.y.TLiner_2 = self.liner.T[1]
        self.y.TComp_1 = self.composite.T[0]
        self.y.TComp_2 = self.composite.T[1]
        self.y.TComp_3 = self.composite.T[2]
        self.y.TComp_4 = self.composite.T[3]
        self.y.TTank = self.tank.fState.T
        self.y.rhoTank = self.tank.fState.rho

        # Set all the initial state values
        self.y0 = self.y.get(copy=True)

        # Set the initial flags
        self.sw0 = [True]

    def rhs(self, t, y, sw):
        # Compute the derivatives of the state variables
        self.compute(t, y, sw)

        # Return the derivatives
        self.yDot.WRealCompressor = self.compressor.WRealDot
        self.yDot.TTank = self.tank.TDot
        self.yDot.rhoTank = self.tank.rhoDot
        self.yDot.TLiner_1 = self.liner.TDot[0]
        self.yDot.TLiner_2 = self.liner.TDot[1]
        self.yDot.TComp_1 = self.composite.TDot[0]
        self.yDot.TComp_2 = self.composite.TDot[1]
        self.yDot.TComp_3 = self.composite.TDot[2]
        self.yDot.TComp_4 = self.composite.TDot[3]
        return self.yDot.get()

    def compute(self, t, y, sw=None):
        try:
            # Set the values (from the Solver) of the state variables
            self.y.set(y)

            # Set states of the components
            self.tank.setState(self.y.TTank, self.y.rhoTank)
            self.liner.setState([self.y.TLiner_1, self.y.TLiner_2])
            self.composite.setState([
                self.y.TComp_1, self.y.TComp_2, self.y.TComp_3, self.y.TComp_4
            ])
            self.cooler.setState()

            # Compute derivatives of the state variables
            # Compressor
            self.compressor.n = self.controller.outputs.nCompressor
            self.compressor.compute()
            # Cooler
            self.cooler.compute()
            # Extraction sink
            self.extractionSink.mDot = self.controller.outputs.mDotExtr
            self.extractionSink.compute()
            # Convection components
            self.tankInternalConvection.hConv = self.controller.outputs.hConvTank
            self.tankInternalConvection.compute()
            self.tankExternalConvection.compute()
            # Liner and composite
            self.composite.compute()
            self.liner.compute()
            # Tank
            self.tank.compute()

        except Exception, e:
            # Log the error if it happens in the compute() function
            print('Exception at time {}: {}'.format(t, e))
            raise e
Esempio n. 11
0
	def __init__(self, params = None, **kwargs):
		super(TankModel, self).__init__(**kwargs)
		if params == None:
			params = AttributeDict(kwargs)
			
		# Initialize update progress function
		self.updateProgress = params.updateProgress

		# Create state vector and derivative vector
		stateVarNames = ['WRealCompressor', 'TTank', 'rhoTank', 'TLiner_1', 'TLiner_2', 'TComp_1', 'TComp_2', 'TComp_3', 'TComp_4'] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize storage
		self.resultStorage = DMC.ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['t'] + stateVarNames + ['pTank', 'mTank', 'TCompressorOut', 'TCoolerOut', 'QDotCooler', 'QDotComp', 'mDotFueling' ],
				chunkSize = 10000)

		# Fueling source
		self.fuelingSource = DM.FluidStateSource(params.fuelingSource)
		self.fuelingSource.initState(params.fuelingSource)
		
		# Compressor	
		self.compressor = DM.Compressor(params.compressor)
		# Connect the compressor to the fluid source
		self.compressor.portIn.connect(self.fuelingSource.port1)
		
		# Cooler
		self.cooler = DM.FluidHeater(params.cooler)
		self.cooler.setEffectivnessModel(epsilon = params.cooler.epsilon)
		# Connect the cooler to the compressor
		self.cooler.portIn.connect(self.compressor.portOut)
		# Connect the cooler to the heater (i.e. temperature source)
		self.coolerTemperatureSource = DMS.ThermalPort('C', DMS.ThermalState(T = params.coolerTemperatureSource.T))
		self.cooler.thermalPort.connect(self.coolerTemperatureSource)
		
		# Tank chamber
		self.tank = DM.FluidChamber(params.tank)
		self.tank.initialize(params.tank.TInit, params.tank.pInit)
		# Connect the tank to the cooler
		self.tank.fluidPort.connect(self.cooler.portOut)
		
		# Extraction sink
		self.extractionSink = DM.FlowSource(params.extractionSink)
		# Connect the extraction sink with the tank
		self.extractionSink.port1.connect(self.tank.fluidPort)
		
		# Tank convection component
		self.tankInternalConvection = DM.ConvectionHeatTransfer(params.tankInternalConvection)
		# Connect to the fluid in the tank
		self.tankInternalConvection.fluidPort.connect(self.tank.fluidPort)

		# Tank (Liner)
		self.liner = DM.SolidConductiveBody(params.liner)
		# Connect to the tank convection component
		self.tankInternalConvection.wallPort.connect(self.liner.port1)
		
		# Tank (composite)
		self.composite = DM.SolidConductiveBody(params.composite)
		# Connect the liner to the composite
		self.composite.port1.connect(self.liner.port2)
				
		# Ambient fluid component
		self.ambientSource = DM.FluidStateSource(params.ambientSource)
		self.ambientSource.initState(params.ambientSource)
		
		# Composite convection component
		self.tankExternalConvection = DM.ConvectionHeatTransfer(params.tankExternalConvection)
		# Connect the composite convection to the ambient fluid
		self.tankExternalConvection.fluidPort.connect(self.ambientSource.port1)
		# Connect the composite convection to the composite  
		self.tankExternalConvection.wallPort.connect(self.composite.port2)
		
		# Controller
		self.controller = params.controller
		
		# Initialize the state variables
		self.y.WRealCompressor = 0. # [W]
		self.y.TLiner_1 = self.liner.T[0]
		self.y.TLiner_2 = self.liner.T[1]
		self.y.TComp_1 = self.composite.T[0]
		self.y.TComp_2 = self.composite.T[1]
		self.y.TComp_3 = self.composite.T[2]
		self.y.TComp_4 = self.composite.T[3]
		self.y.TTank = self.tank.fState.T
		self.y.rhoTank = self.tank.fState.rho 
		
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
Esempio n. 12
0
class TankModel(DMC.Simulation):
	name = 'Model of a tank fueling and extraction'
	
	def __init__(self, params = None, **kwargs):
		super(TankModel, self).__init__(**kwargs)
		if params == None:
			params = AttributeDict(kwargs)
			
		# Initialize update progress function
		self.updateProgress = params.updateProgress

		# Create state vector and derivative vector
		stateVarNames = ['WRealCompressor', 'TTank', 'rhoTank', 'TLiner_1', 'TLiner_2', 'TComp_1', 'TComp_2', 'TComp_3', 'TComp_4'] 
		self.y = NamedStateVector(stateVarNames)
		self.yRes = NamedStateVector(stateVarNames)
		self.yDot = NamedStateVector(stateVarNames)

		# Initialize storage
		self.resultStorage = DMC.ResultStorage(
			filePath = dataStorageFilePath,
			datasetPath = dataStorageDatasetPath)
		if (kwargs.get('initDataStorage', True)):
			self.resultStorage.initializeWriting(
				varList = ['t'] + stateVarNames + ['pTank', 'mTank', 'TCompressorOut', 'TCoolerOut', 'QDotCooler', 'QDotComp', 'mDotFueling' ],
				chunkSize = 10000)

		# Fueling source
		self.fuelingSource = DM.FluidStateSource(params.fuelingSource)
		self.fuelingSource.initState(params.fuelingSource)
		
		# Compressor	
		self.compressor = DM.Compressor(params.compressor)
		# Connect the compressor to the fluid source
		self.compressor.portIn.connect(self.fuelingSource.port1)
		
		# Cooler
		self.cooler = DM.FluidHeater(params.cooler)
		self.cooler.setEffectivnessModel(epsilon = params.cooler.epsilon)
		# Connect the cooler to the compressor
		self.cooler.portIn.connect(self.compressor.portOut)
		# Connect the cooler to the heater (i.e. temperature source)
		self.coolerTemperatureSource = DMS.ThermalPort('C', DMS.ThermalState(T = params.coolerTemperatureSource.T))
		self.cooler.thermalPort.connect(self.coolerTemperatureSource)
		
		# Tank chamber
		self.tank = DM.FluidChamber(params.tank)
		self.tank.initialize(params.tank.TInit, params.tank.pInit)
		# Connect the tank to the cooler
		self.tank.fluidPort.connect(self.cooler.portOut)
		
		# Extraction sink
		self.extractionSink = DM.FlowSource(params.extractionSink)
		# Connect the extraction sink with the tank
		self.extractionSink.port1.connect(self.tank.fluidPort)
		
		# Tank convection component
		self.tankInternalConvection = DM.ConvectionHeatTransfer(params.tankInternalConvection)
		# Connect to the fluid in the tank
		self.tankInternalConvection.fluidPort.connect(self.tank.fluidPort)

		# Tank (Liner)
		self.liner = DM.SolidConductiveBody(params.liner)
		# Connect to the tank convection component
		self.tankInternalConvection.wallPort.connect(self.liner.port1)
		
		# Tank (composite)
		self.composite = DM.SolidConductiveBody(params.composite)
		# Connect the liner to the composite
		self.composite.port1.connect(self.liner.port2)
				
		# Ambient fluid component
		self.ambientSource = DM.FluidStateSource(params.ambientSource)
		self.ambientSource.initState(params.ambientSource)
		
		# Composite convection component
		self.tankExternalConvection = DM.ConvectionHeatTransfer(params.tankExternalConvection)
		# Connect the composite convection to the ambient fluid
		self.tankExternalConvection.fluidPort.connect(self.ambientSource.port1)
		# Connect the composite convection to the composite  
		self.tankExternalConvection.wallPort.connect(self.composite.port2)
		
		# Controller
		self.controller = params.controller
		
		# Initialize the state variables
		self.y.WRealCompressor = 0. # [W]
		self.y.TLiner_1 = self.liner.T[0]
		self.y.TLiner_2 = self.liner.T[1]
		self.y.TComp_1 = self.composite.T[0]
		self.y.TComp_2 = self.composite.T[1]
		self.y.TComp_3 = self.composite.T[2]
		self.y.TComp_4 = self.composite.T[3]
		self.y.TTank = self.tank.fState.T
		self.y.rhoTank = self.tank.fState.rho 
		
		# Set all the initial state values
		self.y0 = self.y.get(copy = True)
		
		# Set the initial flags
		self.sw0 = [True]
		
	def rhs(self, t, y, sw):
		# Compute the derivatives of the state variables
		self.compute(t, y, sw)
		
		# Return the derivatives
		self.yDot.WRealCompressor = self.compressor.WRealDot
		self.yDot.TTank = self.tank.TDot
		self.yDot.rhoTank = self.tank.rhoDot
		self.yDot.TLiner_1 = self.liner.TDot[0]
		self.yDot.TLiner_2 = self.liner.TDot[1]
		self.yDot.TComp_1 = self.composite.TDot[0]
		self.yDot.TComp_2 = self.composite.TDot[1]
		self.yDot.TComp_3 = self.composite.TDot[2]
		self.yDot.TComp_4 = self.composite.TDot[3]
		return self.yDot.get()
	
	def compute(self, t, y, sw = None):
		try:
			# Set the values (from the Solver) of the state variables
			self.y.set(y)
			
			# Set states of the components		
			self.tank.setState(self.y.TTank, self.y.rhoTank)
			self.liner.setState([self.y.TLiner_1, self.y.TLiner_2])
			self.composite.setState([self.y.TComp_1, self.y.TComp_2, self.y.TComp_3, self.y.TComp_4])
			self.cooler.setState()
			

			# Compute derivatives of the state variables 
			# Compressor
			self.compressor.n = self.controller.outputs.nCompressor
			self.compressor.compute()
			# Cooler
			self.cooler.compute()
			# Extraction sink
			self.extractionSink.mDot = self.controller.outputs.mDotExtr
			self.extractionSink.compute()
			# Convection components
			self.tankInternalConvection.hConv = self.controller.outputs.hConvTank
			self.tankInternalConvection.compute()
			self.tankExternalConvection.compute()
			# Liner and composite
			self.composite.compute()
			self.liner.compute()
			# Tank
			self.tank.compute()
			
		except Exception, e:
			# Log the error if it happens in the compute() function
			print('Exception at time {}: {}'.format(t, e))
			raise e