Skip to content

Commit

Permalink
Merge pull request #96 from NTNU-IHB/95-remove-me-wrapper
Browse files Browse the repository at this point in the history
Remove ME wrapper
  • Loading branch information
markaren authored Oct 21, 2019
2 parents 1938c2f + 235757a commit e69c663
Show file tree
Hide file tree
Showing 19 changed files with 39 additions and 1,127 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
FMI4j is a software package for dealing with Functional Mock-up Units (FMUs) on the Java Virtual Machine (JVM), written in [Kotlin](https://kotlinlang.org/).

FMI4j supports import of both [FMI](http://fmi-standard.org/) 1.0 and 2.0 for **Co-simulation**. For **Model Exchange** version 2.0 is supported. <br/>
For Model Exchange, solvers from [Apache Commons Math](http://commons.apache.org/proper/commons-math/userguide/ode.html) can be used.

Export of FMI 2.0 for **Co-simulation** is also supported.

Compared to other FMI libraries targeting the JVM, FMI4j is **considerably faster** due to the fact that we use JNI instead of JNA.
Considering FMI-import, a significant speedup (2-5x) compared to other open-source FMI implementations for the JVM should be expected. For FMI-export FMI4j is multiple orders of magnitude faster than any existing open source alternative.
Considering FMI-import, a significant speedup (2-5x) compared to other open-source FMI implementations for the JVM should be expected.
For FMI-export FMI4j is multiple orders of magnitude faster than any existing open source alternative.


### <a name="api"></a> FMI import
Expand Down
1 change: 0 additions & 1 deletion fmi-import/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"

compile project(':fmi-md')
api group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'

testImplementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
testImplementation group: 'org.siani.javafmi', name: 'fmu-wrapper', version: '2.25.1'
Expand Down
10 changes: 8 additions & 2 deletions fmi-import/src/main/kotlin/no/ntnu/ihb/fmi4j/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@

package no.ntnu.ihb.fmi4j

import no.ntnu.ihb.fmi4j.modeldescription.CoSimulationModelDescription
import no.ntnu.ihb.fmi4j.modeldescription.CommonModelDescription
import no.ntnu.ihb.fmi4j.modeldescription.ModelDescription
import no.ntnu.ihb.fmi4j.modeldescription.ModelExchangeModelDescription
import java.io.Closeable

interface Model : Closeable {
typealias CosimulationModel = Model<CoSimulationModelDescription>
typealias ModelExchangeModel = Model<ModelExchangeModelDescription>

interface Model<E: CommonModelDescription> : Closeable {

val modelDescription: ModelDescription

fun newInstance(): SlaveInstance
fun newInstance(): ModelInstance<E>

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

package no.ntnu.ihb.fmi4j.importer

import no.ntnu.ihb.fmi4j.CosimulationModel
import no.ntnu.ihb.fmi4j.Model
import no.ntnu.ihb.fmi4j.ModelExchangeModel
import no.ntnu.ihb.fmi4j.modeldescription.ModelDescriptionParser
import no.ntnu.ihb.fmi4j.modeldescription.ModelDescriptionProvider
import no.ntnu.ihb.fmi4j.util.extractContentTo
Expand Down Expand Up @@ -57,7 +59,6 @@ abstract class AbstractFmu internal constructor(
val modelDescriptionXml: String
get() = modelDescriptionFile.readText()


/**
* Does the FMU support Co-simulation?
*/
Expand Down Expand Up @@ -88,9 +89,9 @@ abstract class AbstractFmu internal constructor(
}
}

abstract fun asCoSimulationFmu(): Model
abstract fun asCoSimulationFmu(): CosimulationModel

abstract fun asModelExchangeFmu(): Model
abstract fun asModelExchangeFmu(): ModelExchangeModel


protected abstract fun terminateInstances()
Expand Down Expand Up @@ -173,9 +174,7 @@ abstract class AbstractFmu internal constructor(
fun from(file: File): AbstractFmu {

val extension = file.extension.toLowerCase()
if (extension != FMU_EXTENSION) {
throw IllegalArgumentException("File '${file.absolutePath}' is not an FMU! Invalid extension found: .$extension")
}
require(extension == FMU_EXTENSION) { "File '${file.absolutePath}' is not an FMU! Invalid extension found: .$extension" }

if (!file.exists()) {
throw FileNotFoundException("No such file: '$file'!")
Expand All @@ -200,9 +199,7 @@ abstract class AbstractFmu internal constructor(
fun from(url: URL): AbstractFmu {

val extension = File(url.file).extension
if (extension != FMU_EXTENSION) {
throw IllegalArgumentException("URL '$url' does not point to an FMU! Invalid extension found: .$extension")
}
require(extension == FMU_EXTENSION) { "URL '$url' does not point to an FMU! Invalid extension found: .$extension" }

val fmuName = File(url.file).nameWithoutExtension
return createTempDir(fmuName).let { temp ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package no.ntnu.ihb.fmi4j.importer.fmi1

import no.ntnu.ihb.fmi4j.CosimulationModel
import no.ntnu.ihb.fmi4j.Model
import no.ntnu.ihb.fmi4j.SlaveInstance
import no.ntnu.ihb.fmi4j.importer.fmi1.jni.CoSimulationLibraryWrapper
Expand All @@ -35,7 +36,7 @@ import java.io.Closeable

class CoSimulationFmu(
private val fmu: Fmu
) : Model, Closeable by fmu {
) : CosimulationModel, Closeable by fmu {

override val modelDescription: CoSimulationModelDescription by lazy {
fmu.modelDescription.asCoSimulationModelDescription()
Expand All @@ -54,7 +55,7 @@ class CoSimulationFmu(
modelDescription.guid, fmu.fmuPath, loggingOn)
}

override fun newInstance(): SlaveInstance {
override fun newInstance(): CoSimulationSlave {
return newInstance(loggingOn = false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package no.ntnu.ihb.fmi4j.importer.fmi1

import no.ntnu.ihb.fmi4j.Model
import no.ntnu.ihb.fmi4j.ModelExchangeModel
import no.ntnu.ihb.fmi4j.ModelInstance
import no.ntnu.ihb.fmi4j.SlaveInstance
import no.ntnu.ihb.fmi4j.importer.fmi1.jni.Fmi1ModelExchangeLibrary
import no.ntnu.ihb.fmi4j.importer.fmi1.jni.FmiComponent
Expand All @@ -38,7 +40,7 @@ import java.io.Closeable
*/
class ModelExchangeFmu(
private val fmu: Fmu
) : Model, Closeable by fmu {
) : ModelExchangeModel, Closeable by fmu {

override val modelDescription: ModelExchangeModelDescription by lazy {
fmu.modelDescription.asModelExchangeModelDescription()
Expand All @@ -56,7 +58,10 @@ class ModelExchangeFmu(
return lib.instantiateModel(modelDescription.attributes.modelIdentifier, modelDescription.guid, loggingOn)
}

@JvmOverloads
override fun newInstance(): ModelExchangeInstance {
return newInstance(false)
}

fun newInstance(loggingOn: Boolean = false): ModelExchangeInstance {
val c = instantiate(loggingOn)
val wrapper = ModelExchangeLibraryWrapper(c, lib)
Expand All @@ -65,18 +70,4 @@ class ModelExchangeFmu(
}
}

override fun newInstance(): SlaveInstance {
throw IllegalStateException("Not supported (yet)")
}

// fun newInstance(solver: Solver): ModelExchangeSlave {
// return newInstance(solver, visible = false, loggingOn = false)
// }
//
// fun newInstance(solver: Solver, visible: Boolean = false, loggingOn: Boolean = false): ModelExchangeSlave {
// return newInstance(visible, loggingOn).let {
// ModelExchangeSlave(it, solver)
// }
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package no.ntnu.ihb.fmi4j.importer.fmi2

import no.ntnu.ihb.fmi4j.CosimulationModel
import no.ntnu.ihb.fmi4j.Model
import no.ntnu.ihb.fmi4j.SlaveInstance
import no.ntnu.ihb.fmi4j.importer.fmi2.jni.CoSimulationLibraryWrapper
Expand All @@ -34,7 +35,7 @@ import java.io.Closeable

class CoSimulationFmu(
private val fmu: Fmu
) : Model, Closeable by fmu {
) : CosimulationModel, Closeable by fmu {

override val modelDescription: CoSimulationModelDescription by lazy {
fmu.modelDescription.asCoSimulationModelDescription()
Expand All @@ -49,7 +50,7 @@ class CoSimulationFmu(
}

override fun newInstance(): SlaveInstance {
return newInstance(false, false)
return newInstance(visible = false, loggingOn = false)
}

fun newInstance(visible: Boolean = false, loggingOn: Boolean = false): CoSimulationSlave {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,12 @@ class Fmu internal constructor(


override fun asCoSimulationFmu(): CoSimulationFmu {
if (!supportsCoSimulation) {
throw IllegalStateException("FMU does not support Co-simulation!")
}
check(supportsCoSimulation) { "FMU does not support Co-simulation!" }
return coSimulationFmu
}

override fun asModelExchangeFmu(): ModelExchangeFmu {
if (!supportsModelExchange) {
throw IllegalStateException("FMU does not support Model Exchange!")
}
check(supportsModelExchange) { "FMU does not support Model Exchange!" }
return modelExchangeFmu
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
package no.ntnu.ihb.fmi4j.importer.fmi2

import no.ntnu.ihb.fmi4j.Model
import no.ntnu.ihb.fmi4j.ModelExchangeModel
import no.ntnu.ihb.fmi4j.ModelInstance
import no.ntnu.ihb.fmi4j.importer.fmi2.jni.Fmi2ModelExchangeLibrary
import no.ntnu.ihb.fmi4j.importer.fmi2.jni.ModelExchangeLibraryWrapper
import no.ntnu.ihb.fmi4j.modeldescription.ModelExchangeModelDescription
import no.ntnu.ihb.fmi4j.solvers.Solver
import java.io.Closeable

/**
*
* @author Lars Ivar Hatledal
*/
class ModelExchangeFmu @JvmOverloads constructor(
private val fmu: Fmu,
private val solver: Solver? = null
) : Model, Closeable by fmu {
class ModelExchangeFmu(
private val fmu: Fmu
) : ModelExchangeModel, Closeable by fmu {

override val modelDescription: ModelExchangeModelDescription by lazy {
fmu.modelDescription.asModelExchangeModelDescription()
Expand All @@ -52,8 +52,10 @@ class ModelExchangeFmu @JvmOverloads constructor(
}
}

override fun newInstance(): ModelExchangeInstance {
return newInstance(visible = false, loggingOn = false)
}

@JvmOverloads
fun newInstance(visible: Boolean = false, loggingOn: Boolean = false): ModelExchangeInstance {
val c = fmu.instantiate(modelDescription, lib, 0, visible, loggingOn)
val wrapper = ModelExchangeLibraryWrapper(c, lib)
Expand All @@ -62,19 +64,4 @@ class ModelExchangeFmu @JvmOverloads constructor(
}
}

override fun newInstance(): ModelExchangeSlave {
val solver = solver ?: throw IllegalStateException("Class instantiated with no solver!")
return newInstance(solver, visible = false, loggingOn = false)
}

fun newInstance(solver: Solver): ModelExchangeSlave {
return newInstance(solver, visible = false, loggingOn = false)
}

fun newInstance(solver: Solver, visible: Boolean = false, loggingOn: Boolean = false): ModelExchangeSlave {
return newInstance(visible, loggingOn).let {
ModelExchangeSlave(it, solver)
}
}

}
Loading

0 comments on commit e69c663

Please sign in to comment.