-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new classes for model executable/description
Added classes: - ModelDescription - ModelExecutable - DefaultExperiment See examples/modelDescription.mjs for example usage.
- Loading branch information
1 parent
135c394
commit 78ba58c
Showing
15 changed files
with
1,435 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// This script examplifies how to get model description units and variables. | ||
// | ||
// Make sure to have installed dependencies and have the required environment variables | ||
// available, as described in the Quick start example: | ||
// | ||
// https://github.com/modelon-community/impact-client-js#quick-start | ||
// | ||
// Then run the example with: node modelDescription.mjs | ||
|
||
import { Analysis, Client, ExperimentDefinition, Model } from '../dist/index.js' | ||
import dotenv from 'dotenv' | ||
|
||
// Load the .env file variables, install with: npm install dotenv | ||
dotenv.config({ path: '../.env' }) | ||
|
||
const client = Client.fromImpactApiKey({ | ||
impactApiKey: process.env.MODELON_IMPACT_CLIENT_API_KEY, | ||
jupyterHubToken: process.env.JUPYTERHUB_API_TOKEN, | ||
serverAddress: process.env.MODELON_IMPACT_SERVER, | ||
}) | ||
|
||
const WorkspaceName = 'test' | ||
|
||
const workspace = await client.createWorkspace({ | ||
name: WorkspaceName, | ||
}) | ||
|
||
const experimentDefinition = ExperimentDefinition.from({ | ||
analysis: Analysis.from(Analysis.DefaultAnalysis), | ||
model: Model.from({ className: 'Modelica.Blocks.Examples.PID_Controller' }), | ||
}) | ||
|
||
const experiment = await workspace.executeExperimentUntilDone({ | ||
caseIds: ['case_1'], | ||
experimentDefinition, | ||
}) | ||
|
||
const cases = await experiment.getCases() | ||
const modelExecutable = await cases[0].getModelExecutable() | ||
const modelDescription = await modelExecutable.getModelDescription() | ||
|
||
const variables = modelDescription.getVariables() | ||
console.log(variables[0]) | ||
|
||
const units = modelDescription.getUnits() | ||
console.log(units[0]) | ||
|
||
await client.deleteWorkspace(WorkspaceName) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class DefaultExperiment { | ||
startTime | ||
stepSize | ||
stopTime | ||
tolerance | ||
|
||
constructor({ | ||
startTime, | ||
stepSize, | ||
stopTime, | ||
tolerance, | ||
}: { | ||
startTime?: number | ||
stepSize?: number | ||
stopTime?: number | ||
tolerance?: number | ||
}) { | ||
this.startTime = startTime | ||
this.stepSize = stepSize | ||
this.stopTime = stopTime | ||
this.tolerance = tolerance | ||
} | ||
} | ||
|
||
export default DefaultExperiment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import DefaultExperiment from './default-experiment' | ||
import { Unit, Variable } from './types' | ||
|
||
type ModelDescriptionData = { | ||
DefaultExperiment: Record<string, string> | ||
ModelVariables: { | ||
ScalarVariable: Variable[] | ||
} | ||
UnitDefinitions: { Unit: Unit[] } | ||
modelName: string | ||
} | ||
|
||
class ModelDescription { | ||
private data: ModelDescriptionData | ||
|
||
constructor(data: ModelDescriptionData) { | ||
this.data = data | ||
} | ||
|
||
getDefaultExperiment() { | ||
const defaultExperiment = this.data.DefaultExperiment | ||
|
||
const toFloatOrUndefined = (key: string, obj: Record<string, string>) => | ||
obj[key] ? parseFloat(obj[key]) : undefined | ||
|
||
const parameters: ConstructorParameters<typeof DefaultExperiment>[0] = | ||
{} | ||
|
||
const defaultExperimentKeys = [ | ||
'startTime', | ||
'stepSize', | ||
'stopTime', | ||
'tolerance', | ||
] as const | ||
|
||
type DefaultExperimentKey = typeof defaultExperimentKeys[number] | ||
|
||
defaultExperimentKeys.forEach((key) => { | ||
parameters[key as DefaultExperimentKey] = toFloatOrUndefined( | ||
key, | ||
defaultExperiment | ||
) | ||
}) | ||
|
||
return new DefaultExperiment(parameters) | ||
} | ||
|
||
getModelName() { | ||
return this.data.modelName | ||
} | ||
|
||
getUnits() { | ||
return this.data.UnitDefinitions.Unit | ||
} | ||
|
||
getVariables(): Variable[] { | ||
return this.data.ModelVariables.ScalarVariable | ||
} | ||
} | ||
|
||
export default ModelDescription |
Oops, something went wrong.