Skip to content

Commit

Permalink
Initial work on flattener.
Browse files Browse the repository at this point in the history
Signed-off-by: James Goppert <james.goppert@gmail.com>
  • Loading branch information
jgoppert committed Jan 18, 2025
1 parent f351149 commit 9a0427f
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 418 deletions.
32 changes: 25 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
name = "rumoca"
authors = ["James Goppert", "Benjamin Perseghetti"]
description = "A Modelica translator with focus on Casadi, Sympy, JAX, and Collimator generation"
version = "0.4.5"
version = "0.5.0"
edition = "2021"
license = "Apache-2.0"

[dependencies]
rumoca_parser = { version = "0.4.5" }
#rumoca_parser = { path = "../rumoca_parser" }
rumoca_parser = { version = "0.8.0" }
clap = { version = "4.5.20", features = ["derive"] }
codespan-reporting = "0.11.1"
lalrpop-util = "0.22.0"
logos = "0.15.0"
macro_rules_attribute = "0.2.0"
md5 = "0.7.0"
minijinja = "2.5.0"
ndarray = { version = "0.16.1", features = ["serde"] }
ordermap = { version = "0.5.4", features = ["serde"] }
serde = { version = "1.0.214", features = ["derive", "serde_derive"] }
serde_json = "1.0.132"
unindent = "0.2.3"
petgraph = "0.7.1"
25 changes: 21 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
use std::process::Command;

fn main() {
// create git hash
// Attempt to retrieve the current Git version
let output = Command::new("git")
.args(["describe", "--dirty", "--tags"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
.expect("Failed to execute git command");

if output.status.success() {
// Convert the hash to a String and trim it
let git_ver = String::from_utf8_lossy(&output.stdout).trim().to_string();

// Pass the Git hash to your Rust code via an environment variable
println!("cargo:rustc-env=GIT_VER={}", git_ver);

// Optionally, display a warning during the build with the hash
println!("cargo:warning=Using Git version: {}", git_ver);
} else {
eprintln!(
"Failed to retrieve Git version: {}",
String::from_utf8_lossy(&output.stderr)
);
}

// Rerun this build script if `.git/HEAD` or its references change
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rerun-if-changed=.git/refs/");
}
27 changes: 18 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use clap::Parser;

mod s2_analyzer;
mod s3_optimizer;
mod s4_generator;

use clap::Parser;
use rumoca_parser::{PrintVisitor, Visitable};

#[macro_use]
extern crate macro_rules_attribute;

#[derive(Parser, Debug)]
#[command(version, about = "Rumoca Modelica Translator", long_about = None)]
struct Args {
/// The template
#[arg(short, long)]
#[arg(short, long, default_value = "")]
template_file: String,

/// The model file to compile
Expand All @@ -22,18 +27,22 @@ struct Args {

fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let def = rumoca_parser::s1_parser::parse_file(&args.model_file);

let def = rumoca_parser::parse_file(&args.model_file);

if args.verbose {
println!("def:\n{:#?}", def);
println!("\n\n{}", "=".repeat(80));
println!("PARSE");
println!("{}", "=".repeat(80));
def.accept(&mut PrintVisitor::default());
}

let mut flat_def = s2_analyzer::flatten(&def).expect("failed to flatten");
let _flat_def = s2_analyzer::flatten(&def, args.verbose).expect("failed to flatten");
let mut dae_def = s2_analyzer::dae_creator::create_dae(&def, args.verbose).expect("failed to create dae");

if args.verbose {
println!("flat_def:\n{:#?}", flat_def);
if !args.template_file.is_empty() {
let s = s4_generator::generate(&mut dae_def, &args.template_file, args.verbose)?;
println!("{s:}");
}
let s = s4_generator::generate(&mut flat_def, &args.template_file)?;
println!("{s:}");
Ok(())
}
56 changes: 0 additions & 56 deletions src/s2_analyzer/ast.rs

This file was deleted.

76 changes: 76 additions & 0 deletions src/s2_analyzer/dae_ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use ordermap::{OrderMap, OrderSet};
use rumoca_parser::s1_parser::ast::{
ClassType, Equation, Expression, Modification, Statement, Subscript,
};
use serde::{Deserialize, Serialize};

derive_alias! {
#[derive(CommonTraits!)] = #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)];
}

//=============================================================================
/// A file level definition holding multiple flat models
#[derive(CommonTraits!, Default)]
pub struct Def {
pub classes: OrderMap<String, Class>,
pub model_md5: String,
pub rumoca_parser_version: String,
pub rumoca_parser_git: String,
pub rumoca_version: String,
pub rumoca_git: String,
pub template_md5: String,
}

//=============================================================================
/// A flat definition of a DAE
#[derive(CommonTraits!, Default)]
pub struct Class {
pub name: String,
pub class_type: ClassType,
pub description: Vec<String>,
/// dictinoary of components
pub components: OrderMap<String, Component>,
/// constants
pub c: OrderSet<String>,
/// continuous states
pub x: OrderSet<String>,
/// discrete states
pub z: OrderSet<String>,
/// continuous internal variables
pub w: OrderSet<String>,
/// input
pub u: OrderSet<String>,
/// continuous output variables
pub y: OrderSet<String>,
/// parameters
pub p: OrderSet<String>,
/// ordinary diff equation
pub ode: OrderMap<String, Expression>,
/// algebraic eq
pub algebraic: Vec<Equation>,
/// algorithm stms
pub algorithm: Vec<Statement>,
}

#[derive(CommonTraits!, Default)]
pub struct Component {
pub name: String,
pub start: Option<Modification>,
// pub start_value: ArrayBase<OwnedRepr<f64>, IxDyn>,
pub array_subscripts: Vec<Subscript>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ast() {
// class ball
#[allow(unused_variables)]
let class_ball = Class {
name: String::from("Ball"),
..Default::default()
};
}
}
Loading

0 comments on commit 9a0427f

Please sign in to comment.