Skip to content

Commit

Permalink
603 use parameters from csv files create database on start (#604)
Browse files Browse the repository at this point in the history
* added tables

* chaged format

* some test neqsimdb

* added sql

* update database

* update tabel

* modified tables

* update from master (#606)

* Update release_with_jars.yml (#605)

* Update release_with_jars.yml

* Update release_with_jars.yml

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>

* updates

* updated tests

* fixed tests

* added more tables

* add more tables

* update

* deleted

* update

* fixed tables

* updates

* updates

* updates removed derby

* fixed reaction data table

* update java 8 pom

---------

Co-authored-by: Åsmund Våge Fannemel <34712686+asmfstatoil@users.noreply.github.com>
  • Loading branch information
EvenSol and asmfstatoil committed May 22, 2023
1 parent f4c7d3d commit 0b05ef1
Show file tree
Hide file tree
Showing 262 changed files with 3,017 additions and 156 deletions.
16 changes: 6 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
</repositories>

<dependencies>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down Expand Up @@ -55,16 +61,6 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-all</artifactId>
Expand Down
14 changes: 5 additions & 9 deletions pomJava8.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,12 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.2.0</version>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-all</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public void readReactions(SystemInterface system) {
r = Double.parseDouble(dataSet.getString("r"));
actH = Double.parseDouble(dataSet.getString("ACTENERGY"));

neqsim.util.database.NeqSimDataBase database2 = null;
java.sql.ResultSet dataSet2 = null;
try {
neqsim.util.database.NeqSimDataBase database2 =
new neqsim.util.database.NeqSimDataBase();
database2 = new neqsim.util.database.NeqSimDataBase();
dataSet2 = database2
.getResultSet("SELECT * FROM stoccoefdata where REACNAME='" + reacname + "'");
dataSet2.next();
Expand All @@ -107,6 +107,7 @@ public void readReactions(SystemInterface system) {
} finally {
try {
dataSet2.close();
database2.getConnection().close();
} catch (Exception ex) {
logger.error(ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void main(String[] args) {

NeqSimDataBase database = new NeqSimDataBase();
ResultSet dataSet = database.getResultSet(
"SELECT * FROM binarySolubilityData WHERE ComponentSolute='Hg' AND ComponentSolvent='n-decane'");
"SELECT * FROM binarySolubilityData WHERE ComponentSolute='Hg' AND ComponentSolvent='nC10'");

double parameterGuess[] = {188.385052774267, -0.84022345}; // , 2630.871733876947};

Expand Down
141 changes: 134 additions & 7 deletions src/main/java/neqsim/util/database/NeqSimDataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
* <p>
* NeqSimDataBase class.
Expand Down Expand Up @@ -48,11 +49,14 @@ public static void setCreateTemporaryTables(boolean createTemporaryTables) {
static Logger logger = LogManager.getLogger(NeqSimDataBase.class);
private static boolean createTemporaryTables = false;

private static String dataBaseType = "Derby";
private static String connectionString = "jdbc:derby:classpath:data/neqsimthermodatabase";
// private static String dataBaseType = "Derby";
// private static String connectionString = "jdbc:derby:classpath:data/neqsimthermodatabase";
private static String username = "remote";
private static String password = "remote";

private static String dataBaseType = "H2fromCSV";
private static String connectionString = "jdbc:h2:mem:neqsimthermodatabase";
private static boolean h2IsInit = false;
// static String dataBaseType = "MSAccessUCanAccess";
// public static String connectionString =
// "jdbc:ucanaccess://C:/Users/esol/OneDrive -
Expand All @@ -67,6 +71,10 @@ public static void setCreateTemporaryTables(boolean createTemporaryTables) {
* </p>
*/
public NeqSimDataBase() {
if (dataBaseType == "H2fromCSV" && !h2IsInit) {
h2IsInit = true;
initH2DatabaseFromCSVfiles();
}
setDataBaseType(dataBaseType);

try {
Expand Down Expand Up @@ -107,7 +115,8 @@ public Connection openConnection() throws SQLException, ClassNotFoundException {
}
return DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ="
+ dir + "\\data\\NeqSimDatabase");
} else if (dataBaseType.equals("H2") || dataBaseType.equals("H2RT")) {
} else if (dataBaseType.equals("H2fromCSV") || dataBaseType.equals("H2")
|| dataBaseType.equals("H2RT")) {
return DriverManager.getConnection(connectionString, "sa", "");
} else if (dataBaseType.equals("MSAccessUCanAccess")) {
return DriverManager.getConnection(getConnectionString());
Expand Down Expand Up @@ -185,6 +194,27 @@ public void execute(String sqlString) {
}
}

/**
* <p>
* execute.
* </p>
*
* @param sqlString a {@link java.lang.String} object
*/
public void executeQuery(String sqlString) {
try {
if (databaseConnection == null) {
databaseConnection = this.openConnection();
setStatement(databaseConnection.createStatement());
}
getStatement().executeQuery(sqlString);
} catch (Exception ex) {
logger.error("error in NeqSimDataBase " + ex.toString(), ex);
logger.error("The database must be rgistered on the local DBMS to work.");
throw new RuntimeException(ex);
}
}

/**
* <p>
* Getter for the field <code>dataBaseType</code>.
Expand Down Expand Up @@ -225,10 +255,9 @@ public static void setDataBaseType(String aDataBaseType, String connectionString
try {
if (dataBaseType.equals("MSAccess")) {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").getDeclaredConstructor().newInstance();
} else if (dataBaseType.equals("H2")) {
Class.forName("org.h2.Driver");
} else if (dataBaseType.equals("H2RT")) {
Class.forName("org.h2.Driver");
} else if (dataBaseType.equals("H2fromCSV") || dataBaseType.equals("H2")
|| dataBaseType.equals("H2RT")) {
// Class.forName("org.h2.Driver");
} else if (dataBaseType.equals("MSAccessUCanAccess")) {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
} else if (dataBaseType.equals("mySQL")) {
Expand Down Expand Up @@ -325,15 +354,21 @@ public static void setConnectionString(String aConnectionString) {
* @param args an array of {@link java.lang.String} objects
*/
public static void main(String[] args) {
// NeqSimDataBase.initH2DatabaseFromCSVfiles();
// NeqSimDataBase.initDatabaseFromCSVfiles();
NeqSimDataBase database = new NeqSimDataBase();

try (ResultSet dataSet = database.getResultSet("SELECT * FROM comp WHERE NAME='methane'")) {
dataSet.next();
System.out.println("dataset " + dataSet.getString("molarmass"));
logger.info("dataset " + dataSet.getString("molarmass"));
dataSet.close();
database.getConnection().close();
} catch (Exception ex) {
logger.error("failed " + ex.toString());
throw new RuntimeException(ex);
}

}

/**
Expand Down Expand Up @@ -394,4 +429,96 @@ public static boolean hasComponent(String compName) {
}
}
}

public static void initH2DatabaseFromCSVfiles() {
neqsim.util.database.NeqSimDataBase.connectionString =
"jdbc:h2:mem:neqsimthermodatabase;DB_CLOSE_DELAY=-1";
neqsim.util.database.NeqSimDataBase.createTemporaryTables = false;
neqsim.util.database.NeqSimDataBase.dataBaseType = "H2";
neqsim.util.database.NeqSimDataBase database = new neqsim.util.database.NeqSimDataBase();

// Connection con = database.getConnection();
// Statement stmn = con.createStatement();
// stmn.execute(defaultDatabaseRootRoot)

String createCOMP = "CREATE TABLE comp AS SELECT * FROM CSVREAD('classpath:/data/COMP.csv')";
String createINTER = "CREATE TABLE inter AS SELECT * FROM CSVREAD('classpath:/data/INTER.csv')";
String createElement =
"CREATE TABLE element AS SELECT * FROM CSVREAD('classpath:/data/element.csv')";
String create_iso6976 =
"CREATE TABLE iso6976constants AS SELECT * FROM CSVREAD('classpath:/data/ISO6976constants.csv')";
String create_iso6976_2016 =
"CREATE TABLE iso6976constants2016 AS SELECT * FROM CSVREAD('classpath:/data/ISO6976constants2016.csv')";
String create_STOCCOEFDATA =
"CREATE TABLE STOCCOEFDATA AS SELECT * FROM CSVREAD('classpath:/data/STOCCOEFDATA.csv')";
String create_REACTIONDATA =
"CREATE TABLE REACTIONDATA AS SELECT * FROM CSVREAD('classpath:/data/REACTIONDATA.csv')";
String ReactionKSPdata =
"CREATE TABLE ReactionKSPdata AS SELECT * FROM CSVREAD('classpath:/data/ReactionKSPdata.csv')";
String create_AdsorptionParameters =
"CREATE TABLE AdsorptionParameters AS SELECT * FROM CSVREAD('classpath:/data/AdsorptionParameters.csv')";
String create_UNIFACcomp =
"CREATE TABLE UNIFACcomp AS SELECT * FROM CSVREAD('classpath:/data/UNIFACcomp.csv')";
String create_UNIFACcompUMRPRU =
"CREATE TABLE UNIFACcompUMRPRU AS SELECT * FROM CSVREAD('classpath:/data/UNIFACcompUMRPRU.csv')";
String UNIFACGroupParam =
"CREATE TABLE UNIFACGroupParam AS SELECT * FROM CSVREAD('classpath:/data/UNIFACGroupParam.csv')";
String UNIFACInterParam =
"CREATE TABLE UNIFACInterParam AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParam.csv')";
String UNIFACInterParamA_UMR =
"CREATE TABLE UNIFACInterParamA_UMR AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamA_UMR.csv')";
String UNIFACInterParamA_UMRMC =
"CREATE TABLE UNIFACInterParamA_UMRMC AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamA_UMRMC.csv')";
String UNIFACInterParamB =
"CREATE TABLE UNIFACInterParamB AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamB.csv')";
String UNIFACInterParamB_UMR =
"CREATE TABLE UNIFACInterParamB_UMR AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamB_UMR.csv')";
String UNIFACInterParamB_UMRMC =
"CREATE TABLE UNIFACInterParamB_UMRMC AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamB_UMRMC.csv')";
String UNIFACInterParamC =
"CREATE TABLE UNIFACInterParamC AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamC.csv')";
String UNIFACInterParamC_UMR =
"CREATE TABLE UNIFACInterParamC_UMR AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamC_UMR.csv')";
String UNIFACInterParamC_UMRMC =
"CREATE TABLE UNIFACInterParamC_UMRMC AS SELECT * FROM CSVREAD('classpath:/data/UNIFACInterParamC_UMRMC.csv')";

try {
database.execute(createCOMP);
database.execute(createINTER);
database.execute(createElement);
database.execute(create_iso6976);
database.execute(create_iso6976_2016);
database.execute(create_STOCCOEFDATA);
database.execute(create_REACTIONDATA);
database.execute(ReactionKSPdata);
database.execute(create_AdsorptionParameters);
database.execute(create_UNIFACcomp);
database.execute(create_UNIFACcompUMRPRU);
database.execute(UNIFACGroupParam);
database.execute(UNIFACInterParam);
database.execute(UNIFACInterParamA_UMR);
database.execute(UNIFACInterParamA_UMRMC);
database.execute(UNIFACInterParamB);
database.execute(UNIFACInterParamB_UMR);
database.execute(UNIFACInterParamB_UMRMC);
database.execute(UNIFACInterParamC);
database.execute(UNIFACInterParamC_UMR);
database.execute(UNIFACInterParamC_UMRMC);
database.execute("CREATE TABLE comptemp AS SELECT * FROM comp");
database.execute("CREATE TABLE intertemp AS SELECT * FROM inter");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (database.getStatement() != null) {
database.getStatement().close();
}
if (database.getConnection() != null) {
database.getConnection().close();
}
} catch (Exception ex) {
logger.error("error closing database.....", ex);
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/resources/data/AdsorptionParameters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"ID","Name","Solid","eps","z0","beta"
1,"methane","AC Calgon F400",7.458000000,0.298000000,2.000000000
2,"CO2","AC Calgon F400",7.760000000,0.298000000,2.000000000
3,"nitrogen","AC Calgon F400",6.010000000,0.298000000,2.000000000
4,"methane","AC Norit R1",8.060000000,0.362000000,2.000000000
6,"argon","AC Norit R1",5.800000000,0.362000000,2.000000000
7,"methane","AC",7.180000000,0.367000000,2.000000000
8,"CO2","AC",7.510000000,0.367000000,2.000000000
9,"nitrogen","AC Norit R1",0.050000000,0.362000000,2.000000000
Loading

0 comments on commit 0b05ef1

Please sign in to comment.