Skip to content

Commit

Permalink
WIP: native-lib ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed Feb 7, 2025
1 parent 1100abf commit 7ba479d
Show file tree
Hide file tree
Showing 11 changed files with 723 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<module>vcell-cli</module>
<module>vcell-rest</module>
<module>vcell-restclient</module>
<module>vcell-nativelib</module>
</modules>

<scm>
Expand Down
167 changes: 167 additions & 0 deletions vcell-nativelib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.vcell</groupId>
<artifactId>vcell-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>vcell-nativelib</artifactId>

<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<native.maven.plugin.version>0.10.4</native.maven.plugin.version>
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
<imageName>sbml_to_fvsolver</imageName>
<mainClass>org.vcell.pyvcell.SBMLSpatialTest</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>org.vcell</groupId>
<artifactId>vcell-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-api</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-1.2-api</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-api</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-core</artifactId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <groupId>com.lowagie</groupId>-->
<!-- <artifactId>itext</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>java</id>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>${mainClass}</mainClass>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.source}</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>



<profiles>
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>${native.maven.plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
<configuration>
<!-- ... -->
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
58 changes: 58 additions & 0 deletions vcell-nativelib/src/main/java/org/vcell/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.vcell;

import cbit.util.xml.VCLoggerException;
import cbit.vcell.biomodel.BioModel;
import cbit.vcell.mapping.MappingException;
import cbit.vcell.mapping.SimulationContext;
import cbit.vcell.parser.ExpressionException;
import cbit.vcell.solver.Simulation;
import cbit.vcell.solver.SolverException;
import cbit.vcell.solver.TimeBounds;
import cbit.vcell.solver.UniformOutputTimeSpec;
import org.apache.logging.log4j.Logger;
import org.vcell.sbml.SBMLFakeSpatialBioModel;
import org.vcell.sbml.vcell.SBMLExporter;
import org.vcell.sbml.vcell.SBMLImporter;

import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Main {

private static Logger logger = org.apache.logging.log4j.LogManager.getLogger(Main.class);

public static void sbmlToFiniteVolumeInput(File sbmlFile, File outputDir) throws FileNotFoundException, MappingException, VCLoggerException, PropertyVetoException, SolverException, ExpressionException {
SBMLExporter.MemoryVCLogger vcl = new SBMLExporter.MemoryVCLogger();
boolean bValidateSBML = true;
InputStream is = new FileInputStream(sbmlFile);
SBMLImporter importer = new SBMLImporter(is, vcl, bValidateSBML);
BioModel bioModel = importer.getBioModel();
bioModel.updateAll(false);

final double duration = 5.0; // endpoint arg
final double time_step = 0.1; // endpoint arg
//final ISize meshSize = new ISize(10, 10, 10); // future endpoint arg
SimulationContext simContext = bioModel.getSimulationContext(0);
Simulation sim = new Simulation(simContext.getMathDescription(), simContext);
sim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0.0, duration));
sim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(time_step));

SBMLFakeSpatialBioModel.writeInputFilesOnly(outputDir, sim);
}




public static void main(String[] args) {
try {
File sbml_file = new File("/Users/jimschaff/Documents/workspace/vcell/vcell-rest/src/test/resources/TinySpacialProject_Application0.xml");
sbmlToFiniteVolumeInput(sbml_file, new File("/Users/jimschaff/Documents/workspace/vcell/vcell-rest/src/test/resources/"));
System.out.println("Hello, World!");
} catch (Exception e) {
logger.error("Error processing spatial model", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.vcell.pyvcell;

public class FiniteVolumeUtils {
}
143 changes: 143 additions & 0 deletions vcell-nativelib/src/test/java/org/vcell/pyvcell/SBMLSpatialTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.vcell.pyvcell;


import cbit.vcell.biomodel.BioModel;
import cbit.vcell.field.FieldDataIdentifierSpec;
import cbit.vcell.mapping.SimulationContext;
import cbit.vcell.messaging.server.SimulationTask;
import cbit.vcell.parser.ExpressionException;
import cbit.vcell.resource.PropertyLoader;
import cbit.vcell.solver.*;
import cbit.vcell.solvers.FVSolverStandalone;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.vcell.sbml.vcell.SBMLExporter;
import org.vcell.sbml.vcell.SBMLImporter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

@Tag("SBML_IT")
public class SBMLSpatialTest {

private static File workingDir;

public static class MyFiniteVolumeSolver extends FVSolverStandalone {
public MyFiniteVolumeSolver(SimulationTask simulationTask, File dataDir) throws SolverException {
super(simulationTask, dataDir, false);
}
@Override
public void initialize() throws SolverException {
super.initialize();
}
}

public static class SBMLSpatialSolver {

private static class TempSimulationJob extends SimulationJob {
public TempSimulationJob(TempSimulation argSim, int jobIndex, FieldDataIdentifierSpec[] argFDIS) {
super(argSim, jobIndex, argFDIS);
}
@Override
public TempSimulation getSimulation() {
return (TempSimulation)super.getSimulation();
}
}

public static void simulate(File workingDir, Simulation simulation) throws SolverException, ExpressionException {

TempSimulation tempSimulation = new TempSimulation(simulation,false);
tempSimulation.setSimulationOwner(simulation.getSimulationOwner());
TempSimulationJob tempSimulationJob = new TempSimulationJob(tempSimulation, 0, null);

SimulationTask simTask = new SimulationTask(tempSimulationJob, 0);
MyFiniteVolumeSolver solver = new MyFiniteVolumeSolver(simTask, workingDir);
solver.runSolver();
}

public static void writeInputFilesOnly(File workingDir, Simulation simulation) throws SolverException, ExpressionException {

TempSimulation tempSimulation = new TempSimulation(simulation,false);
tempSimulation.setSimulationOwner(simulation.getSimulationOwner());
TempSimulationJob tempSimulationJob = new TempSimulationJob(tempSimulation, 0, null);

SimulationTask simTask = new SimulationTask(tempSimulationJob, 0);
MyFiniteVolumeSolver solver = new MyFiniteVolumeSolver(simTask, workingDir);
solver.initialize();
}
}

@BeforeAll
public static void before() throws IOException {
PropertyLoader.setProperty(PropertyLoader.installationRoot, "..");
Logger.getLogger(SBMLExporter.class).addAppender(new ConsoleAppender());
// create temporary working directory
workingDir = Files.createTempDirectory("sbml-test-suite-working-dir-").toFile();
}

@AfterAll
public static void after() {
// delete temporary working directory
try {
Files.delete(workingDir.toPath());
} catch (Exception e) {
e.printStackTrace();
}
}

private static InputStream getFileFromResourceAsStream(String path) {
InputStream inputStream = SBMLSpatialTest.class.getResourceAsStream(path);
if (inputStream == null) {
throw new RuntimeException("file not found! " + path);
} else {
return inputStream;
}
}

@Test
public void testSbmlTestSuiteImport() throws Exception{
TLogger vcl = new TLogger();
InputStream testFileInputStream = getFileFromResourceAsStream("/TinySpacialProject_Application0.xml"); // endpoint arg
boolean bValidateSBML = true;
SBMLImporter importer = new SBMLImporter(testFileInputStream, vcl, bValidateSBML);
try {
BioModel bioModel = importer.getBioModel();
bioModel.updateAll(false);

final double duration = 5.0; // endpoint arg
final double time_step = 0.1; // endpoint arg
//final ISize meshSize = new ISize(10, 10, 10); // future endpoint arg
SimulationContext simContext = bioModel.getSimulationContext(0);
Simulation sim = new Simulation(simContext.getMathDescription(), simContext);
sim.getSolverTaskDescription().setTimeBounds(new TimeBounds(0.0, duration));
sim.getSolverTaskDescription().setOutputTimeSpec(new UniformOutputTimeSpec(time_step));

System.out.println("INPUT FILES ONLY");
SBMLSpatialSolver.writeInputFilesOnly(workingDir, sim);
printWorkingDir();

System.out.println("ALL FILES INCLUDING OUTPUT");
SBMLSpatialSolver.simulate(workingDir, sim);
printWorkingDir();

}catch (Exception e){
e.printStackTrace();
}
}

private static void printWorkingDir() {
// print contents of workingDir
File[] files = workingDir.listFiles();
for (File file : files) {
System.out.println(file.getName());
}
}

}

Loading

0 comments on commit 7ba479d

Please sign in to comment.