Skip to content

Commit

Permalink
Merge branch 'advection_1d' into hackathon_fall_2023
Browse files Browse the repository at this point in the history
  • Loading branch information
danbryce committed Dec 1, 2023
2 parents 0cc9abc + 90bf6eb commit b2873f7
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
163 changes: 163 additions & 0 deletions resources/amr/advection_1d/advection_1d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"header": {
"name": "Advection 1d Model",
"schema": "https://raw.githubusercontent.com/DARPA-ASKEM/Model-Representations/petrinet_v0.1/petrinet/petrinet_schema.json",
"schema_name": "petrinet",
"description": "Advection 1d as Petrinet model created by Drisana",
"model_version": "0.1"
},
"model": {
"states": [
{
"id": "u_0",
"name": "u_0",
"description": "density"
},
{
"id": "u_1",
"name": "u_1",
"description": "density"
},
{
"id": "u_2",
"name": "u_2",
"description": "density"
},
{
"id": "u_3",
"name": "u_3",
"description": "density"
},
{
"id": "u_4",
"name": "u_4",
"description": "density"
}
],
"transitions": [
{
"id": "t_0",
"input": [
"u_0"
],
"output": [
"u_1"
],
"properties": {
"name": "t_0"
}
},
{
"id": "t_1",
"input": [
"u_1"
],
"output": [
"u_2"
],
"properties": {
"name": "t_1"
}
},
{
"id": "t_2",
"input": [
"u_2"
],
"output": [
"u_3"
],
"properties": {
"name": "t_2"
}
},
{
"id": "t_3",
"input": [
"u_3"
],
"output": [
"u_4"
],
"properties": {
"name": "t_3"
}
}
]
},
"semantics": {
"ode": {
"rates": [
{
"target": "t_0",
"expression": "(a/dx)*(u_1-u_0)"
},
{
"target": "t_1",
"expression": "(a/dx)*(u_2-u_1)"
},
{
"target": "t_2",
"expression": "(a/dx)*(u_3-u_2)"
},
{
"target": "t_3",
"expression": "(a/dx)*(u_4-u_3)"
}
],
"initials": [
{
"target": "u_0",
"expression": "0.1"
},
{
"target": "u_1",
"expression": "0.5"
},
{
"target": "u_2",
"expression": "1.0"
},
{
"target": "u_3",
"expression": "0.5"
},
{
"target": "u_4",
"expression": "0.1"
}
],
"parameters": [
{
"id": "a",
"value": 1.0,
"distribution": {
"type": "StandardUniform1",
"parameters": {
"minimum": 0.0,
"maximum": 1.0
}
}
},
{
"id": "dx",
"value": 1.0,
"distribution": {
"type": "StandardUniform1",
"parameters": {
"minimum": 0.0,
"maximum": 1.0
}
}
}
],
"time": {
"id": "t",
"units": {
"expression": "day",
"expression_mathml": "<ci>day</ci>"
}
}
}
}
}
36 changes: 36 additions & 0 deletions resources/amr/advection_1d/advection_1d_request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"structure_parameters": [
{
"name": "schedules",
"schedules": [
{
"timepoints": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
}
]
}
],
"config": {
"use_compartmental_constraints": false,
"normalization_constant": 1.0,
"tolerance": 1e-1,
"verbosity": 20,
"dreal_mcts": true,
"save_smtlib": "./out",
"substitute_subformulas": false,
"series_approximation_threshold": null,
"dreal_log_level": "none",
"profile": false
}
}
55 changes: 55 additions & 0 deletions scratch/sympy_pde_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from sympy import Derivative, E, Eq, Function, pde_separate, symbols
from sympy.parsing.latex import parse_latex

### Symbol initialization
x, y, z, t, i, n, dx, dt, a = symbols("x y z t i n dx dt a")
init_printing(use_unicode=True)

u = symbols("u", cls=Function)
u = Function("u")
dudx = u(x, t).diff(x)
dudt = u(x, t).diff(t)

### Solve for discrete derivatives - other arbitrary schemes for discretizing time and space can be brought in here. This is general, so these can be substituted into other PDEs.

backward_space_derivative = dudx.as_finite_difference(
[dx * (i - 1), dx * i]
).subs(t, dt * i)
forward_space_derivative = dudx.as_finite_difference(
[dx * (i), dx * (i + 1)]
).subs(t, dt * i)
forward_time_derivative = dudt.as_finite_difference(
[dt * (n), dt * (n + 1)]
).subs(x, dx * i)

### Parse advection equation. This one is from LaTeX but can use other sources too.

advection_parse = parse_latex(
r"\frac{\partial q}{\partial t} + a\frac{\partial q}{\partial x} = 0"
)


### Substitute into parsed equation - input definitions for u and discrete derivatives

advection_parse = advection_parse.subs(Symbol("q"), u(x, t))

advection_parse = advection_parse.subs(
Derivative(u(x, t), t), forward_time_derivative
)
advection_parse = advection_parse.subs(
Derivative(u(x, t), x), backward_space_derivative
)

print(
advection_parse
) ### Advection equation in terms of discrete time and space points

### Solve for u at next timepoint ("n+1") in terms of previous points

next_time_step_advection_parse = solve(
advection_parse, u(dx * i, dt * (n + 1))
)

print(
next_time_step_advection_parse
) ## matches FTBS on upwind scheme page of https://indico.ictp.it/event/a06220/session/18/contribution/10/material/0/2.pdf for a > 0

0 comments on commit b2873f7

Please sign in to comment.