Skip to content

Commit

Permalink
major database redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusbfs committed Apr 23, 2019
1 parent e844ea7 commit f281b33
Show file tree
Hide file tree
Showing 20 changed files with 740 additions and 486 deletions.
507 changes: 277 additions & 230 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Sindri/Controllers/PureSubstanceController.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def genDiagrams(self):
self.model.getPref(),
self.model.getTref(),
int(self.points),
# isotherms=self.isotherms_range,
isotherms=self.isotherms_range,
)
s2 = time()
QtWidgets.QMessageBox.information(
Expand Down
4 changes: 3 additions & 1 deletion Sindri/DatabaseInterface/DatabaseTableWidgetView.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def load_db(self):

def show_full_db(self):
try:
query = "SELECT * FROM database"
# join = "substance s LEFT JOIN cp_correlations c ON s.substance_id = c.substance_id LEFT JOIN antoine_correlations a ON a.substance_id = s.substance_id"
table_name = "v_all_properties_including_correlations"
query = "SELECT * FROM " + table_name
db.cursor.execute(query)
results = db.cursor.fetchall()
self.update_table_db(results)
Expand Down
18 changes: 11 additions & 7 deletions Sindri/DatabaseInterface/databaseSearchFunctions.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
def getQueryBySearchNameFormulaOrCas(s: str) -> str:
# join = "substance s LEFT JOIN cp_correlations c ON s.substance_id = c.substance_id LEFT JOIN antoine_correlations a ON a.substance_id = s.substance_id"
table_name = "v_all_properties_including_correlations"
query = (
"SELECT * FROM database WHERE Name LIKE '%"
"SELECT * FROM "
+ table_name
+ " WHERE name LIKE '%"
+ s
+ "%'"
+ " OR Formula LIKE '%"
+ " OR formula LIKE '%"
+ s
+ "%'"
+ " OR `CAS #` LIKE '%"
+ " OR cas LIKE '%"
+ s
+ "%' ORDER BY (Name = '"
+ "%' ORDER BY (name = '"
+ s
+ "' OR Formula = '"
+ "' OR formula = '"
+ s
+ "' OR `CAS #` = '"
+ "' OR cas = '"
+ s
+ "') DESC, length(Name)"
+ "') DESC, length(name)"
)
return query
14 changes: 14 additions & 0 deletions Sindri/EOSMixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ def getZfromPT(self, P: float, T: float, y):
real_values = roots[roots >= 0]
return real_values

def getPfromTV(self, T: float, V: float, y) -> float:
b = self.mixRuleBehavior.bm(y, T, self.biBehavior, self.substances)
theta = self.mixRuleBehavior.thetam(
y, T, self.thetaiBehavior, self.substances, self.k
)
delta = self.deltaMixBehavior.deltam(
y, T, self.biBehavior, self.mixRuleBehavior, self.substances
)
epsilon = self.epsilonMixBehavior.epsilonm(
y, T, self.biBehavior, self.mixRuleBehavior, self.substances
)
p = R_IG * T / (V - b) - theta / (V * (V + delta) + epsilon)
return p

def getPhi_i(self, i: int, y, P: float, T: float, Z: float):

RT = R_IG * T
Expand Down
3 changes: 3 additions & 0 deletions Sindri/EOSPureSubstanceInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def getPvp(
def getZfromPT(self, _P: float, _T: float):
return self.eosmix.getZfromPT(_P, _T, self.y)

def getPfromTV(self, _T: float, _V: float):
return self.eosmix.getPfromTV(_T, _V, self.y)

def getAllProps(
self, Tref: float, T: float, Pref: float, P: float
) -> (Props, Props):
Expand Down
7 changes: 2 additions & 5 deletions Sindri/Models/FitExpDataToBinaryParameterModel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from Models.MixtureModel import MixtureModel
import numpy as np
from scipy.optimize import least_squares

Expand Down Expand Up @@ -88,12 +87,10 @@ def _objectiveFunction_isothermal(self, k: float):
k0_calc = phi_liq_0 / phi_vap_0
k1_calc = phi_liq_1 / phi_vap_1

lnk0_exp = np.log(k0_exp)
lnk0_calc = np.log(k0_calc)
lnk1_exp = np.log(k1_exp)
lnk1_calc = np.log(k1_calc)
s += ((np.log(k0_exp) - np.log(k0_calc)) / np.log(k0_exp)) ** 2
s += ((np.log(k1_exp) - np.log(k1_calc)) / np.log(k1_exp)) ** 2
# s += ((np.log(k0_exp) - np.log(k0_calc)) ) ** 2
# s += ((np.log(k1_exp) - np.log(k1_calc)) ) ** 2

return s

Expand Down
5 changes: 4 additions & 1 deletion Sindri/compounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def __init__(self, name: str, formula: str):

db.init()

table_name = "v_all_properties_including_correlations"
query = (
"SELECT * FROM database WHERE Formula LIKE '%"
"SELECT * FROM "
+ table_name
+ " WHERE Formula LIKE '%"
+ formula
+ "%'"
+ " AND Name LIKE '%"
Expand Down
43 changes: 34 additions & 9 deletions Sindri/databaseWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def load_db(self):

def show_full_db(self):
try:
query = "SELECT * FROM database"
# join = "substance s LEFT JOIN cp_correlations c ON s.substance_id = c.substance_id LEFT JOIN antoine_correlations a ON a.substance_id = s.substance_id"
# query = "SELECT * FROM " + join
query = "SELECT * from v_all_properties_including_correlations"
db.cursor.execute(query)
results = db.cursor.fetchall()
self.update_table_db(results)
Expand Down Expand Up @@ -202,20 +204,43 @@ def del_substance(self):
if choice == QtWidgets.QMessageBox.Yes:
try:
self.database_changed = True
# query = (
# "DELETE FROM database WHERE Formula LIKE '"
# + row_values[0]
# + "%'"
# + " AND Name LIKE '"
# + row_values[1]
# + "%'"
# + " AND `CAS #` LIKE '"
# + row_values[2]
# + "%'"
# )
query = (
"DELETE FROM database WHERE Formula LIKE '"
"SELECT substance_id FROM substance WHERE name='"
+ str(row_values[1])
+ "' AND formula='"
+ row_values[0]
+ "%'"
+ " AND Name LIKE '"
+ row_values[1]
+ "%'"
+ " AND `CAS #` LIKE '"
+ "' AND cas='"
+ row_values[2]
+ "%'"
+ "'"
)
db.cursor.execute(query)
substance_id = "'{:d}'".format(db.cursor.fetchone()[0])
query = "DELETE from cp_correlations where substance_id={0}".format(
substance_id
)
db.cursor.execute(query)
query = "DELETE FROM antoine_correlations where substance_id={0}".format(
substance_id
)
db.cursor.execute(query)
query = "DELETE FROM substance where substance_id={0}".format(
substance_id
)
db.cursor.execute(query)
self.search_substance()
except:
except Exception as e:
print(str(e))
pass

def save_db(self):
Expand Down
Binary file modified Sindri/db/database.db
Binary file not shown.
Binary file modified Sindri/db/database.db.orig
Binary file not shown.
186 changes: 148 additions & 38 deletions Sindri/db_addSubstanceProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import db
from ui.db_substanceProperties_ui import Ui_Form_db_substanceProperties
from validators import getDoubleValidatorRegex


class Form_AddSubstanceProperties(QtWidgets.QWidget, Ui_Form_db_substanceProperties):
Expand Down Expand Up @@ -54,52 +55,146 @@ def __init__(self, parent=None):
self.le_formula.textChanged.connect(self.disableConfirmButton)
self.le_CAS.textChanged.connect(self.disableConfirmButton)

doublevalidator = getDoubleValidatorRegex(self)
# general
self.le_MM.setValidator(doublevalidator)
self.le_Tb.setValidator(doublevalidator)
self.le_Tfp.setValidator(doublevalidator)
self.le_Tc.setValidator(doublevalidator)
self.le_Pc.setValidator(doublevalidator)
self.le_Vc.setValidator(doublevalidator)
self.le_Zc.setValidator(doublevalidator)
self.le_omega.setValidator(doublevalidator)
# cp
self.le_a0.setValidator(doublevalidator)
self.le_a1.setValidator(doublevalidator)
self.le_a2.setValidator(doublevalidator)
self.le_a3.setValidator(doublevalidator)
self.le_a4.setValidator(doublevalidator)
self.le_CpTmin.setValidator(doublevalidator)
self.le_CpTmax.setValidator(doublevalidator)
# antoine
self.le_AntoineA.setValidator(doublevalidator)
self.le_AntoineB.setValidator(doublevalidator)
self.le_AntoineC.setValidator(doublevalidator)
self.le_AntoineTmin.setValidator(doublevalidator)
self.le_AntoineTmax.setValidator(doublevalidator)

def confirm_clicked(self):

if self.isFloat(self.le_CpTmin.text()) and self.isFloat(self.le_CpTmax.text()):
Trange = self.le_CpTmin.text() + "-" + self.le_CpTmax.text()
else:
Trange = ""

query = "insert into database values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
items = (
self.le_formula.text(),
self.le_name.text(),
self.le_CAS.text(),
self.le_MM.text(),
self.le_Tfp.text(),
self.le_Tb.text(),
self.le_Tc.text(),
self.le_Pc.text(),
self.le_Vc.text(),
self.le_Zc.text(),
self.le_omega.text(),
Trange,
self.le_a0.text(),
self.le_a1.text(),
self.le_a2.text(),
self.le_a3.text(),
self.le_a4.text(),
"",
"",
self.le_AntoineA.text(),
self.le_AntoineC.text(),
self.le_AntoineB.text(),
"",
self.le_AntoineTmin.text(),
"",
self.le_AntoineTmax.text(),
)
try:

# insert general properties substances
name = self.returnNULLifEmptyString(self.le_name)
formula = self.returnNULLifEmptyString(self.le_formula)
cas = self.returnNULLifEmptyString(self.le_CAS)
tfp_k = self.returnNULLifEmptyFloat(self.le_Tfp)
tb_k = self.returnNULLifEmptyFloat(self.le_Tb)
tc_k = self.returnNULLifEmptyFloat(self.le_Tc)
pc_bar = self.returnNULLifEmptyFloat(self.le_Pc)
vc_cm3_per_mol = self.returnNULLifEmptyFloat(self.le_Vc)
zc = self.returnNULLifEmptyFloat(self.le_Zc)
omega = self.returnNULLifEmptyFloat(self.le_omega)
molar_weigth = self.returnNULLifEmptyFloat(self.le_MM)

query = (
"INSERT INTO substance (name,formula,cas,tfp_k,tb_k,tc_k,pc_bar,vc_cm3_per_mol,zc,omega,molar_weigth)"
+ "VALUES(?,?,?,?,?,?,?,?,?,?,?)"
)
db.cursor.execute(
query,
(
name,
formula,
cas,
tfp_k,
tb_k,
tc_k,
pc_bar,
vc_cm3_per_mol,
zc,
omega,
molar_weigth,
),
)

query = (
"SELECT substance_id FROM substance WHERE name='"
+ name
+ "' AND formula='"
+ formula
+ "' AND cas='"
+ cas
+ "'"
)
db.cursor.execute(query)
substance_id = int(db.cursor.fetchone()[0])

cp_eq_type = 1
cp_tmin = self.returnNULLifEmptyFloat(self.le_CpTmin)
cp_tmax = self.returnNULLifEmptyFloat(self.le_CpTmax)
cp_a0 = self.returnNULLifEmptyFloat(self.le_a0)
cp_a1 = self.returnNULLifEmptyFloat(self.le_a1)
cp_a2 = self.returnNULLifEmptyFloat(self.le_a2)
cp_a3 = self.returnNULLifEmptyFloat(self.le_a3)
cp_a4 = self.returnNULLifEmptyFloat(self.le_a4)

query = (
"insert into cp_correlations (substance_id,eq_type,cp_tmin,cp_tmax,cp_a0,cp_a1,cp_a2,cp_a3,cp_a4)"
+ "values(?,?,?,?,?,?,?,?,?)"
)
db.cursor.execute(
query,
(
substance_id,
cp_eq_type,
cp_tmin,
cp_tmax,
cp_a0,
cp_a1,
cp_a2,
cp_a3,
cp_a4,
),
)

antoine_eq_type = 1
antoine_a = self.returnNULLifEmptyFloat(self.le_AntoineA)
antoine_b = self.returnNULLifEmptyFloat(self.le_AntoineB)
antoine_c = self.returnNULLifEmptyFloat(self.le_AntoineC)
tmin_k = self.returnNULLifEmptyFloat(self.le_AntoineTmin)
tmax_k = self.returnNULLifEmptyFloat(self.le_AntoineTmax)

query = (
"insert into antoine_correlations (substance_id,eq_type,antoine_a,antoine_b,antoine_c,tmin_k,tmax_k)"
+ "values(?,?,?,?,?,?,?)"
)
db.cursor.execute(
query,
(
substance_id,
antoine_eq_type,
antoine_a,
antoine_b,
antoine_c,
tmin_k,
tmax_k,
),
)

self.substance_added = True

except Exception as e:
QtWidgets.QMessageBox.about(self, "Error adding substance", str(e))
return -1

db.cursor.execute(query, items)
self.substance_added = True
self.close()

def isFloat(self, s):
try:
float(s)
float(s) * 1.0 + 1.0
return True
except ValueError:
except:
return False

def cancel_clicked(self):
Expand All @@ -120,3 +215,18 @@ def disableConfirmButton(self):
self.btn_edit_confirm.setDisabled(False)
else:
self.btn_edit_confirm.setDisabled(True)

def returnNULLifEmptyString(self, le):
if le.text() == "":
return None
return le.text()

def returnNULLifEmptyFloat(self, le):
if le.text() == "":
return None
return float(le.text())

def returnNULLifEmptyInteger(self, le):
if le.text() == "":
return None
return int(le.text())
Loading

0 comments on commit f281b33

Please sign in to comment.