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 22, 2025
1 parent f351149 commit 147d301
Show file tree
Hide file tree
Showing 32 changed files with 1,511 additions and 496 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.10.1" }
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"
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,20 @@ Type the following to test that rumoca is in your path.

```bash
$ rumoca --help

Rumoca Modelica Translator

Usage: rumoca [OPTIONS] --template-file <TEMPLATE_FILE> --model-file <MODEL_FILE>
Usage: rumoca [OPTIONS] <MODELICA_FILE>

Arguments:
<MODELICA_FILE> The modelica *.mo file to compile

Options:
-t, --template-file <TEMPLATE_FILE> The template
-m, --model-file <MODEL_FILE> The model file to compile
-v, --verbose Verbose output
-h, --help Print help
-V, --version Print version
-t, --template <TEMPLATE> Renders a template using dae_ast [default: ]
-a, --ast-template <AST_TEMPLATE> Renders a template using ast [default: ]
-v, --verbose Verbose output
-h, --help Print help
-V, --version Print version
```

## Building, Testing, and Running
Expand All @@ -116,7 +120,7 @@ This package uses the standard cargo conventions for rust.
cargo build
cargo run
cargo test
cargo run -- -t test/templates/casadi_sx.jinja -m test/models/integrator.mo
cargo run -- test/models/integrator.mo -t test/templates/casadi_sx.jinja
```

This package uses the standard cargo installation conventions.
Expand All @@ -142,7 +146,7 @@ end Integrator;

### Generated CasADi output file.
```bash
$ rumoca -t test/templates/casadi_sx.jinja -m test/models/integrator.mo
$ rumoca test/models/integrator.mo -t test/templates/casadi_sx.jinja
```
```python
import casadi as ca
Expand Down
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/");
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod s2_analyzer;
pub mod s3_optimizer;
pub mod s4_generator;

#[macro_use]
extern crate macro_rules_attribute;
57 changes: 40 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
mod s2_analyzer;
mod s3_optimizer;
mod s4_generator;

use clap::Parser;

use rumoca::s2_analyzer::visitors::Printer;
use rumoca::{s2_analyzer, s4_generator};
use rumoca_parser::Visitable;

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

/// Renders a template using ast
#[arg(short, long, default_value = "")]
ast_template: String,

/// The model file to compile
#[arg(short, long)]
model_file: String,
/// The modelica *.mo file to compile
#[arg(name = "MODELICA_FILE")]
modelica_file: String,

/// Flatten the model
#[arg(short, long, default_value_t = false)]
flatten: bool,

/// Verbose output
#[arg(short, long, default_value_t = false)]
Expand All @@ -22,18 +30,33 @@ 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 mut printer = Printer::default();
let def = rumoca_parser::parse_file(&args.modelica_file);
let bar = "=".repeat(40);

if args.verbose {
println!("def:\n{:#?}", def);
println!("\n\n{}", bar);
println!("PARSE");
println!("{}", bar);
def.accept(&mut printer);
}

let mut flat_def = s2_analyzer::flatten(&def).expect("failed to flatten");
let mut flat_def = def.clone();

if args.verbose {
println!("flat_def:\n{:#?}", flat_def);
if args.flatten {
flat_def = s2_analyzer::flatten(&def, args.verbose).expect("failed to flatten");
}

if !args.ast_template.is_empty() {
let s = s4_generator::generate_ast(&flat_def, &args.ast_template, args.verbose)?;
println!("{s:}");
}

if !args.template.is_empty() {
let mut dae_def =
s2_analyzer::dae_creator::create_dae(&def, args.verbose).expect("failed to create dae");
let s = s4_generator::generate_dae_ast(&mut dae_def, &args.template, 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.

Loading

0 comments on commit 147d301

Please sign in to comment.