Skip to content

Commit

Permalink
Juliacall removed from streamlit app, converting of juliacall wraps t…
Browse files Browse the repository at this point in the history
…o python objects in flask_api, and docker volumes are added for host-container syncing
  • Loading branch information
LorenaH84 committed Feb 20, 2024
1 parent b7be548 commit 18bd273
Show file tree
Hide file tree
Showing 21 changed files with 465 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "BattMoGUIEnv",
"dockerComposeFile": "../docker-compose-dev.yml",
"service": "devcontainer",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
"name": "Flask Container",
"dockerComposeFile": "../../docker-compose-dev.yml",
"service": "flask_api",
"shutdownAction": "none",
"workspaceFolder": "/workspace/flask_api",
// "remoteEnv": {
// "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}/.."
// },
Expand Down
49 changes: 49 additions & 0 deletions .devcontainer/nginx-container/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "Nginx Container",
"dockerComposeFile": "../../docker-compose-dev.yml",
"service": "nginx",
"shutdownAction": "none",
"workspaceFolder": "/workspace/nginx"
// "remoteEnv": {
// "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}/.."
// },
// "mounts": [
// "source=${localWorkspaceFolder}/..,target=/workspace,type=bind,consistency=cached"
// ],
// "remoteEnv": {
// "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
// },

//"runArgs": [ "-v", "/var/run/docker.sock:/var/run/docker.sock"],


//"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

// Set *default* container specific settings.json values on container create.
// "customizations": {

// "vscode": {
// "settings": {
// "python.pythonPath": "/usr/local/bin/python",
// "python.languageServer": "Pylance",
// "python.linting.enabled": true,
// "python.linting.pylintEnabled": true


// },
// // "customVolumeMounts": [
// // "/../flask_api:/workspaces/flask_api",
// // "/../nginx:/workspaces/nginx",
// // "/../streamlit:/workspaces/streamlit"
// // ],

// "extensions": [
// "ms-python.python",
// "ms-python.vscode-pylance",
// "julialang.language-julia"

// ]
// }
// }

}
51 changes: 51 additions & 0 deletions .devcontainer/streamlit-container/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "Streamlit Container",
"dockerComposeFile": "../../docker-compose-dev.yml",
"service": "streamlit",
"shutdownAction": "none",
"workspaceFolder": "/workspace/streamlit",


// "remoteEnv": {
// "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}/.."
// },
// "mounts": [
// "source=${localWorkspaceFolder}/..,target=/workspace,type=bind,consistency=cached"
// ],
// "remoteEnv": {
// "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
// },

//"runArgs": [ "-v", "/var/run/docker.sock:/var/run/docker.sock"],


//"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

// Set *default* container specific settings.json values on container create.
"customizations": {

"vscode": {
"settings": {
"python.pythonPath": "/usr/local/bin/python",
"python.languageServer": "Pylance",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true


},
// "customVolumeMounts": [
// "/../flask_api:/workspaces/flask_api",
// "/../nginx:/workspaces/nginx",
// "/../streamlit:/workspaces/streamlit"
// ],

"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"julialang.language-julia"

]
}
}

}
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for more information:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
# https://containers.dev/guide/dependabot

version: 2
updates:
- package-ecosystem: "devcontainers"
directory: "/"
schedule:
interval: weekly
16 changes: 4 additions & 12 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
version: "3.8"

services:
devcontainer:
image: mcr.microsoft.com/devcontainers/base:jammy
volumes:
- "../..:/workspaces:cached"
network_mode: service:streamlit
command: sleep infinity


nginx:
build: ./nginx
container_name: nginx
restart: unless-stopped
ports:
- "8001:8001"
volumes:
- /workspaces/Repositories/nginx:/app
- ./nginx:/app
depends_on:
- flask_api

Expand All @@ -27,9 +19,9 @@ services:
ports:
- "8000:8000"
volumes:
- /workspaces/Repositories/flask_api:/app
- ./flask_api:/app
command: gunicorn -w 1 -b 0.0.0.0:8000 wsgi:server --timeout 200
#command: python wsgi.py


streamlit:
build: ./streamlit
Expand All @@ -38,5 +30,5 @@ services:
ports:
- "8501:8501"
volumes:
- /workspaces/Repositories/streamlit:/app
- ./streamlit:/app
command: streamlit run Introduction.py --global.disableWidgetStateDuplicationWarning true --server.port=8501
7 changes: 7 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ services:
restart: always
ports:
- "8000:8000"
volumes:
- ./flask_api:/app
command: gunicorn -w 1 -b 0.0.0.0:8000 wsgi:server --timeout 200
#command: python wsgi.py
nginx:
Expand All @@ -16,9 +18,12 @@ services:
restart: always
ports:
- "8001:8001"
volumes:
- ./nginx:/app

depends_on:
- flask_api



streamlit:
Expand All @@ -27,5 +32,7 @@ services:
restart: always
ports:
- "8501:8501"
volumes:
- ./streamlit:/app

command: streamlit run Introduction.py --global.disableWidgetStateDuplicationWarning true --server.port=8501
9 changes: 6 additions & 3 deletions flask_api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ RUN pip install jill --no-cache-dir

RUN jill install 1.9.2 --confirm

RUN rm -rf ~/.julia/compiled
#RUN rm -rf ~/.julia/compiled

# Helpful Development Packages

RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "PythonCall",version = "0.9.14"))'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "JSON",version = "0.21.4"))'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "BattMo",version = "0.1.6"))'
#RUN julia -e 'using Pkg; Pkg.add("BattMo")'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "Jutul",version = "0.2.14"))'
#RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;url="https://github.com/BattMoTeam/BattMo.jl.git", rev="refac"))'
#RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "Jutul",version = "0.2.14"))'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "LoggingExtras",version = "1.0.3"))'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "PythonCall",version = "0.9.14"))'
RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "JSON",version = "0.21.4"))'

RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "PrettyTables",version = "2.2.7"))'

COPY requirements.txt .
Expand Down
54 changes: 42 additions & 12 deletions flask_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pickle
from uuid import uuid4
import os
import numpy as np


##############################
Expand Down Expand Up @@ -34,28 +35,57 @@ def run_julia(q_in,q_out):

# Define the Julia code to execute
julia_code = f"runP2DBattery.runP2DBatt(\"{file_name}\")"
log_messages, number_of_states, cell_voltage, cell_current, time_values, negative_electrode_grid, electrolyte_grid, positive_electrode_grid, negative_electrode_concentration, electrolyte_concentration, positive_electrode_concentration, negative_electrode_potential, electrolyte_potential, positive_electrode_potential = jl.seval(julia_code)
log_messages, number_of_states, cell_voltage, cell_current, time_values, negative_electrode_grid, negative_electrode_grid_bc, electrolyte_grid, electrolyte_grid_bc, positive_electrode_grid, positive_electrode_grid_bc, negative_electrode_concentration, electrolyte_concentration, positive_electrode_concentration, negative_electrode_potential, electrolyte_potential, positive_electrode_potential = jl.seval(julia_code)

print("The simulation has completed {} time steps.".format(number_of_states))

# Converting data to python objects
negative_electrode_concentration = [jl.Py(subarray).to_numpy() for subarray in negative_electrode_concentration],
negative_electrode_concentration = np.vstack(negative_electrode_concentration)

electrolyte_concentration = [jl.Py(subarray).to_numpy() for subarray in electrolyte_concentration],
electrolyte_concentration = np.vstack(electrolyte_concentration)

positive_electrode_concentration = [jl.Py(subarray).to_numpy() for subarray in positive_electrode_concentration],
positive_electrode_concentration = np.vstack(positive_electrode_concentration)

negative_electrode_potential = [jl.Py(subarray).to_numpy() for subarray in negative_electrode_potential],
negative_electrode_potential = np.vstack(negative_electrode_potential)

electrolyte_potential = [jl.Py(subarray).to_numpy() for subarray in electrolyte_potential],
electrolyte_potential = np.vstack(electrolyte_potential)

positive_electrode_potential = [jl.Py(subarray).to_numpy() for subarray in positive_electrode_potential],
positive_electrode_potential = np.vstack(positive_electrode_potential)


print(number_of_states)

output = [log_messages,
number_of_states,
cell_voltage,
cell_current,
time_values,
negative_electrode_grid,
electrolyte_grid,
positive_electrode_grid,
output = [ np.array(log_messages),
int(number_of_states[0]),
jl.Py(cell_voltage).to_numpy(),
jl.Py(cell_current).to_numpy(),
jl.Py(time_values).to_numpy(),
np.squeeze(jl.Py(negative_electrode_grid).to_numpy()),
np.squeeze(jl.Py(negative_electrode_grid_bc).to_numpy()),
np.squeeze(jl.Py(electrolyte_grid).to_numpy()),
np.squeeze(jl.Py(electrolyte_grid_bc).to_numpy()),
np.squeeze(jl.Py(positive_electrode_grid).to_numpy()),
np.squeeze(jl.Py(positive_electrode_grid_bc).to_numpy()),
negative_electrode_concentration,
electrolyte_concentration,
positive_electrode_concentration,
negative_electrode_potential,
electrolyte_potential,
positive_electrode_potential
]

positive_electrode_potential]

print(output)

# print("1: ", negative_electrode_grid)
# print("2: ", output[5])
# print("3: ", type(output[5]))
# with open("test.txt", "w") as new_pickle_file:
# new_pickle_file.write(output)

with open(os.path.join("results",uuid_str), "wb") as new_pickle_file:
pickle.dump(output, new_pickle_file)
Expand Down
18 changes: 14 additions & 4 deletions flask_api/julia/runP2DBattery.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module runP2DBattery
negative_electrode_grid = 0
electrolyte_grid = 0
positive_electrode_grid = 0
negative_electrode_grid_bc = 0
electrolyte_grid_bc = 0
positive_electrode_grid_bc = 0
negative_electrode_concentration = 0
electrolyte_concentration = 0
positive_electrode_concentration = 0
Expand Down Expand Up @@ -63,9 +66,16 @@ module runP2DBattery

# Create grid arrays

negative_electrode_grid = [centroids_NAM, boundaries_NAM].*10^6
electrolyte_grid = [centroids_ELYTE, boundaries_ELYTE].*10^6
positive_electrode_grid = [centroids_PAM, boundaries_PAM].*10^6
# negative_electrode_grid = [centroids_NAM, boundaries_NAM].*10^6
# electrolyte_grid = [centroids_ELYTE, boundaries_ELYTE].*10^6
# positive_electrode_grid = [centroids_PAM, boundaries_PAM].*10^6

negative_electrode_grid = centroids_NAM.*10^6
negative_electrode_grid_bc = boundaries_NAM.*10^6
electrolyte_grid = centroids_ELYTE.*10^6
electrolyte_grid_bc = boundaries_ELYTE.*10^6
positive_electrode_grid = centroids_PAM.*10^6
positive_electrode_grid_bc = boundaries_PAM.*10^6
negative_electrode_concentration = negative_electrode_concentration[1]
positive_electrode_concentration = positive_electrode_concentration[1]

Expand All @@ -78,6 +88,6 @@ module runP2DBattery
close(log_buffer)
end

return log_messages, number_of_states, cell_voltage, cell_current, time_values, negative_electrode_grid, electrolyte_grid, positive_electrode_grid, negative_electrode_concentration, electrolyte_concentration, positive_electrode_concentration, negative_electrode_potential, electrolyte_potential, positive_electrode_potential
return log_messages, number_of_states, cell_voltage, cell_current, time_values, negative_electrode_grid, negative_electrode_grid_bc, electrolyte_grid, electrolyte_grid_bc, positive_electrode_grid, positive_electrode_grid_bc, negative_electrode_concentration, electrolyte_concentration, positive_electrode_concentration, negative_electrode_potential, electrolyte_potential, positive_electrode_potential
end
end
Empty file added flask_api/test.txt
Empty file.
2 changes: 1 addition & 1 deletion flask_api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

if __name__ == '__main__':
#multiprocessing.set_start_method("spawn")'
server.run(host='0.0.0.0', port=8000)
server.run(host='0.0.0.0', port=8000, debug=True)
4 changes: 2 additions & 2 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM nginx:1.15.8

RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/
RUN rm /etc/nginx/nginx.conf
COPY project.conf /etc/nginx/conf.d/
RUN rm /etc/nginx/conf.d/default.conf
COPY project.conf /etc/nginx/conf.d/
11 changes: 5 additions & 6 deletions streamlit/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ FROM python:3.11.4

WORKDIR /app

RUN pip install jill --no-cache-dir
COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

RUN jill install 1.9.2 --confirm

RUN julia -e 'using Pkg; Pkg.add(PackageSpec(;name = "PythonCall",version = "0.9.14"))'

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .
Loading

0 comments on commit 18bd273

Please sign in to comment.