Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build FANS as a library to be coupled to a macro-scale simulation via preCICE and the Micro Manager #23

Merged
merged 136 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
136 commits
Select commit Hold shift + click to select a range
315e12a
add new features to CMakeLists.txt and adapt modern best practices
siggmo Jun 26, 2024
87a77d4
Add Language C to project to allow HDF5 to compile its C parts
siggmo Jun 26, 2024
cbc6ccb
Add autoformat file for CMake, Presets and a Doxyfile
siggmo Jun 26, 2024
dd3c840
remove unnecessary includes and links, replace linker flag by cmake c…
siggmo Jun 27, 2024
12264a3
replace -fopenmp by modern CMake way
siggmo Jun 27, 2024
a30a1e5
move compiler options to target based locations
siggmo Jun 27, 2024
3cab37a
Use FFTW3 find script and find_package(MPI)
siggmo Jun 28, 2024
60f4763
add FindFFTW3.cmake (was hidden by .git
siggmo Jun 28, 2024
e55a376
change output name of shared lib from libFANS_FANS.so to libFANS.so
siggmo Jun 28, 2024
eaadfb7
ignore .cmake folder generated by CLion
siggmo Jun 28, 2024
c228d5e
Dockerfile for containerized build and publishable standalone selfcon…
siggmo Jun 28, 2024
6919555
add another container stage for dev-env, add docker.yaml github actio…
siggmo Jul 3, 2024
7362bab
fix typo
siggmo Jul 3, 2024
8f4799a
Merge branch 'main' into rework_build_system
siggmo Jul 3, 2024
ceade72
Merge branch 'main' into rework_build_system
siggmo Jul 8, 2024
d508e1b
fix missing CMD start command for container dev-env
siggmo Jul 8, 2024
adf98d4
Make use of auto-determined dependencies in runtime docker image (red…
siggmo Jul 8, 2024
28801fa
Set up cross-compilation for arm64 using Docker buildx
siggmo Jul 9, 2024
0a7fdab
fix COMPONENT_PACKAGE_DEPENDS (needs to be uppercase), and add openmp…
siggmo Jul 9, 2024
1bdfdd6
rename packages and edit descriptions
siggmo Jul 9, 2024
d6418b0
Add CI workflow to build and package FANS
siggmo Jul 9, 2024
6ba1430
update version of publish artifacts action
siggmo Jul 10, 2024
bae0687
Merge branch 'main' into rework_build_system
siggmo Jul 10, 2024
3c4daf3
Add remaining dependencies to FANSConfig.cmake and include FindFFTW3.…
siggmo Jul 10, 2024
6687675
run autoformat
siggmo Jul 10, 2024
6404b14
Reoarganize CMake files, fix install calls, add found message
siggmo Jul 10, 2024
07d5ff1
remove COMPONENT statement in install(TARGETS ... INCLUDES) since tha…
siggmo Jul 10, 2024
b01390b
cmake overhaul
siggmo Jul 11, 2024
229ba7d
Add Docker images for building FANS and instructions in the README.md
siggmo Jul 11, 2024
726fbb0
clean up comments
siggmo Jul 11, 2024
80ed714
Add Github Actions workflow to build and publish docker images fans-c…
siggmo Jul 11, 2024
5275eb2
upgrade to latest LTS ubuntu
siggmo Jul 11, 2024
a86388a
Merge branch 'cmake-overhaul' into docker
siggmo Jul 11, 2024
bb403ab
rename docker github action yaml + fix dockerfile
siggmo Jul 11, 2024
32fa2f3
add workflow to build FANS
siggmo Jul 11, 2024
12e508e
Rename build workflow to CI and add a job for packaging FANS
siggmo Jul 11, 2024
054b3f6
instead of putting the FANS executable in the test/ dir, just symlink it
siggmo Jul 11, 2024
d8f99c8
Merge branch 'cmake-overhaul' into docker
siggmo Jul 11, 2024
dd6f8d6
temporarily disable arm64 support since it takes super long to run
siggmo Jul 11, 2024
90cc1fa
add debug output
siggmo Jul 11, 2024
892af4b
use just one job to avoid complicated data exchange between jobs
siggmo Jul 11, 2024
ca80cde
add fans docker image + github actions workflow
siggmo Jul 11, 2024
2d17090
make build variable dir in actions + streamline dockerfile of fans image
siggmo Jul 11, 2024
1cc7c6f
add support for older ubuntu versions
siggmo Jul 11, 2024
3b8d457
escape & sign in Dockerfile RUN command
siggmo Jul 11, 2024
f17822b
fix typo
siggmo Jul 11, 2024
a592bbf
add support for different ubuntu LTS versions
siggmo Jul 11, 2024
b14ca1a
use matrix also for arch in docker helpers workflow
siggmo Jul 11, 2024
3ebae97
put variables in brackets
siggmo Jul 11, 2024
b22da2e
also use matrix.arch in fans docker image
siggmo Jul 11, 2024
09c3872
fix weird escape error that occurs when switching ubuntu versions
siggmo Jul 11, 2024
5569e23
fix comment
siggmo Jul 11, 2024
cdd0900
remove focal as it causes some CPack error
siggmo Jul 11, 2024
2c4a169
use correct variable substitution syntax
siggmo Jul 11, 2024
ad0289b
variable substitution
siggmo Jul 11, 2024
f064113
add ubuntu version to name of artifact
siggmo Jul 11, 2024
df908aa
make detection if project is top level compatible with older CMake ve…
siggmo Jul 11, 2024
13d99ba
reactivate focal again after fix
siggmo Jul 11, 2024
4a4dc2e
use explicit path instead of dot
siggmo Jul 11, 2024
73ac60e
Merge branch 'main' into cmake-overhaul
siggmo Jul 12, 2024
5ef6083
Merge branch 'cmake-overhaul' into docker
siggmo Jul 12, 2024
d56e344
Make IPO handling compatible with CMake 3.0
siggmo Jul 12, 2024
2503b5d
Add max version to cmake_mininum_required() to indicate that we don't…
siggmo Jul 12, 2024
1403e85
make detection if project is top level compatible with older CMake ve…
siggmo Jul 11, 2024
7f870ed
Merge branch 'cmake-overhaul' into docker
siggmo Jul 12, 2024
e3991ab
fix RPATH settings
siggmo Jul 12, 2024
0d3e22e
update README.md
siggmo Jul 12, 2024
c8abedd
Merge branch 'cmake-overhaul' into docker
siggmo Jul 12, 2024
231a8ea
fix link
siggmo Jul 13, 2024
99db4c2
remove commas in package lists
siggmo Jul 13, 2024
9278dd1
restructure Installation section and add link to prebuilt packages
siggmo Jul 13, 2024
69b93ca
Merge branch 'main' into cmake-overhaul
siggmo Jul 13, 2024
d87f749
Merge branch 'cmake-overhaul' into docker
siggmo Jul 13, 2024
8857d55
move docker section of README.md into docker/ dir
siggmo Jul 13, 2024
a0f4b49
add section part to link
siggmo Jul 13, 2024
03bec4d
replace multiple docker run commands by docker create + docker exec
siggmo Jul 13, 2024
a672ad9
fix docker exec call
siggmo Jul 13, 2024
a38f22d
updated h5 to xdmf viz pipeline
sanathkeshav1 Jul 11, 2024
6388e08
Fix path to filename in 3d0_MFL
tjwsch Jul 12, 2024
7eb0c09
small changes
sanathkeshav1 Jul 12, 2024
14b7bf7
can provide keyword for temporal dataset group names
sanathkeshav1 Jul 12, 2024
3d9d4a3
Adapt FANS for MM initial commit
tjwsch Jul 18, 2024
e8834ae
Adapt FANS further
tjwsch Jul 19, 2024
0b484bb
fix quotation mark
siggmo Jul 19, 2024
9cb1106
fix quotation mark
siggmo Jul 19, 2024
880d37b
provide users with a cache option for enabling packaging
siggmo Jul 19, 2024
5c607a5
Merge branch 'cmake-overhaul' into docker
siggmo Jul 19, 2024
8bbc5e2
Micro manager python bindings
siggmo Jul 19, 2024
799d6fe
add symlink to test directory for convenience
siggmo Jul 19, 2024
7a8bf39
Merge with other MM branch
tjwsch Jul 20, 2024
bdce496
Move Micro Manager input files
tjwsch Jul 20, 2024
0d2f217
Add Micro Manager input files
tjwsch Jul 20, 2024
801f81d
Little tweaks on the MM version
tjwsch Jul 24, 2024
bc35f36
Running Micro Manager version of FANS
tjwsch Jul 26, 2024
80e7e80
Change Class descriptions
tjwsch Jul 29, 2024
2ccc75d
Clean up
tjwsch Aug 1, 2024
b581bc8
Merge main
tjwsch Aug 1, 2024
ef29e45
Fix building of bindings
tjwsch Aug 1, 2024
224cb14
Merge develop to this branch
IshaanDesai Sep 21, 2024
da4bd81
CMake edit temporarily
IshaanDesai Sep 23, 2024
f2bcd60
Formatting
IshaanDesai Oct 9, 2024
a56d3bb
restructure micromanager part of CMakeLists.txt file
siggmo Oct 9, 2024
33c6191
Merge branch 'develop' into lib_micro_sim
IshaanDesai Oct 9, 2024
fdeb468
Merge branch 'develop' into lib_micro_sim2
siggmo Oct 9, 2024
418f46f
Align with current state on develop branch
siggmo Oct 9, 2024
e793ffe
Merge branch 'lib_micro_sim2' into lib_micro_sim
siggmo Oct 9, 2024
aea7930
Merge branch 'develop' into lib_micro_sim
siggmo Oct 9, 2024
a707673
Remove unnecessary code and get homogenized stress only by access
IshaanDesai Oct 11, 2024
cdfc48e
Remove thermal micro simulation code as it is just a copy of the mech…
IshaanDesai Oct 11, 2024
5fc5691
Remove duplicate micro.hpp from include/
IshaanDesai Oct 11, 2024
62e927d
Remove more redundant code and fix types
IshaanDesai Oct 11, 2024
f515e5e
Streamline code in micro.hpp and micro.cpp
IshaanDesai Oct 11, 2024
f5aecb3
A first implementation of numerically calculating the tangent stiffne…
IshaanDesai Oct 14, 2024
3bdae1b
First working version with logically correct stiffness matrix C
IshaanDesai Oct 16, 2024
f3260c2
Bump clang-format version in pre-commit config
IshaanDesai Oct 17, 2024
57d22bb
Merge branch 'develop' into lib_micro_sim
IshaanDesai Oct 17, 2024
cbdcccd
Formatting
IshaanDesai Oct 17, 2024
6bb7fde
Rename folder with content for micro library to pyfans
IshaanDesai Oct 17, 2024
96dc4a8
Remove files related to thermal test of micro simulation library
IshaanDesai Oct 17, 2024
194da8a
Replace Eigen Matrix row.begin and row.end with simpler functionality
IshaanDesai Oct 18, 2024
0f74610
Remove MPI initialization
IshaanDesai Oct 30, 2024
8352bdd
Merge branch 'develop' into lib_micro_sim
IshaanDesai Oct 30, 2024
37e802f
Initial restructuring of pyfans test, and remove the numerical comput…
IshaanDesai Oct 30, 2024
d0d4820
Remove h5 file and revert inadvertant changes to input file
IshaanDesai Oct 30, 2024
6f9e600
Merge branch 'develop' into lib_micro_sim
IshaanDesai Oct 30, 2024
251c665
Add documentation and test CI for pyFANS
IshaanDesai Oct 31, 2024
add3b4c
Working GitHub Action to test pyFANS
IshaanDesai Oct 31, 2024
5d8d405
Add CHANGELOG enty and a small comment in the pyFANS test script
IshaanDesai Oct 31, 2024
cc245ca
Remove stale input.json from test/
IshaanDesai Oct 31, 2024
7b5783f
Explicitly mention what type of FANS library is being compiled
IshaanDesai Nov 2, 2024
5d06b1a
Incorporate review comments
IshaanDesai Nov 4, 2024
7e16075
Remove unnecessary parts from pyfans/CmakeLists.txt
IshaanDesai Nov 4, 2024
a55ab27
Hardcode mechanical model and solver because delaying variable defini…
IshaanDesai Nov 4, 2024
a04944e
Incorporate feedback regarding the test
IshaanDesai Nov 4, 2024
2ea2815
Remove unnecessary code
IshaanDesai Nov 4, 2024
431baa8
One empty entry in macroscale_loading should be sufficient
sanathkeshav Nov 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/test_pyfans.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Test PyFans

on: [push, pull_request]

jobs:
pyfans-run-test:
runs-on: ubuntu-latest
container: unistuttgartdae/fans-ci:noble
defaults:
run:
shell: "bash --login -eo pipefail {0}"
env:
FANS_BUILD_DIR: build
FANS_MPI_USER: fans
steps:

- name: Checkout repository
uses: actions/checkout@v2

- name: Generate build directory
run: mkdir -p ${{ env.FANS_BUILD_DIR }}

- name: Install dependencies
run: |
apt update
apt install -y cmake make g++ python3 python3-numpy python3.12-dev

- name: Configure
working-directory: ${{ env.FANS_BUILD_DIR }}
run: |
cmake .. -DFANS_LIBRARY_FOR_MICRO_MANAGER=ON
make
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved

- name: Run FANS as a library via a Python script
run: |
cd test/test_pyfans
python3 run_fans_as_library.py
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# FANS Changelog

## latest

- Build FANS as a library to be coupled to a macro-scale simulation via preCICE and the Micro Manager https://github.com/DataAnalyticsEngineering/FANS/pull/23

## v0.3.0

- Added Linear thermal and mechanical triclinic material models https://github.com/DataAnalyticsEngineering/FANS/pull/32
Expand Down
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ find_package(MPI REQUIRED)

find_package(FFTW3 REQUIRED COMPONENTS DOUBLE MPI)

option(FANS_LIBRARY_FOR_MICRO_MANAGER "Building FANS as a library to be used by the Micro Manager." OFF)

if (FANS_LIBRARY_FOR_MICRO_MANAGER)
include(FetchContent)
FetchContent_Declare(
pybind11 GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.12.0
)
FetchContent_MakeAvailable(pybind11)
endif()

# ##############################################################################
# TARGETS
# ##############################################################################
Expand Down Expand Up @@ -115,6 +126,10 @@ add_custom_command(
COMMENT "Create a symlink for FANS executable to ${CMAKE_CURRENT_SOURCE_DIR}/test/"
)

if (FANS_LIBRARY_FOR_MICRO_MANAGER)
add_subdirectory(pyfans)
endif ()

# ##############################################################################
# HEADERS
# ##############################################################################
Expand Down
11 changes: 11 additions & 0 deletions pyfans/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pybind11_add_module(PyFANS micro.hpp micro.cpp)
target_link_libraries(PyFANS PRIVATE FANS::FANS)

add_custom_command(
TARGET PyFANS
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
$<TARGET_FILE:PyFANS>
${CMAKE_CURRENT_SOURCE_DIR}/../test/test_pyfans/$<TARGET_FILE_NAME:PyFANS>
COMMENT "Create a symlink for FANS python bindings to ${CMAKE_CURRENT_SOURCE_DIR}/../test/"
)
15 changes: 15 additions & 0 deletions pyfans/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# pyFANS

pyFANS is a Python-wrapped library to control FANS via the [Micro Manager](https://precice.org/tooling-micro-manager-overview.html). The main idea is to create a large number of FANS simulations, and couple them to one macro-scale simulation typically in Abaqus, CalculiX, etc. The library follows the [API of the Micro Manager](https://precice.org/tooling-micro-manager-prepare-micro-simulation.html).

## Dependencies

- [pybind11](https://pybind11.readthedocs.io/en/stable/index.html)

## Building

To build FANS as a Micro Manager compatible Python library, set the CMake variable `FANS_LIB` to `ON`. The CMake command to compile FANS would then be `cmake .. -DFANS_LIBRARY_FOR_MICRO_MANAGER=ON`.

## Usage

pyFANS is intended to be used with the Micro Manager and preCICE for two-scale coupled simulations. However, standalone use of the library is not restricted per se. Look at the [test_pyfans](../test/test_pyfans/) example to see how the library is used in a Python script.
100 changes: 100 additions & 0 deletions pyfans/micro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Micro simulation for mechanical problems
// In this file we solve a micro problem with FANS which is controlled by the Micro Manager
// This file is compiled with pybind11 to be available as a python module
//
// To check if python is able to import it, run:
// python3 -c "import micro; micro.MicroSimulation(1)"
// from the same directory

#include "micro.hpp"
#include "setup.h"
#include "matmodel.h"

py::array_t<double> merge_arrays(py::array_t<double> array1, py::array_t<double> array2)
{
// Ensure arrays are contiguous for efficient merging
array1 = array1.attr("copy")();
array2 = array2.attr("copy")();

// Get numpy concatenate function
py::object np = py::module::import("numpy");
py::object concatenate = np.attr("concatenate");

// Concatenate the two arrays
py::tuple arrays = py::make_tuple(array1, array2);
py::array_t<double> result = concatenate(arrays, py::int_(0)).cast<py::array_t<double>>();

return result;
}

MicroSimulation::MicroSimulation(int sim_id, char *input_file)
{
MPI_Init(NULL, NULL);

// initialize fftw mpi
fftw_mpi_init();

// Input file name is hardcoded. TODO: Make it configurable
reader.ReadInputFile(input_file);

reader.ReadMS(3);
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved
matmodel = createMatmodel<3>(reader);
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved
solver = createSolver<3>(reader, matmodel);
}

py::dict MicroSimulation::solve(py::dict macro_data, double dt)
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved
{
// Time step value dt is not used currently, but is available for future use

// Create a pybind style Numpy array from macro_write_data["micro_vector_data"], which is a Numpy array
py::array_t<double> strain1 = macro_data["strains1to3"].cast<py::array_t<double>>();
py::array_t<double> strain2 = macro_data["strains4to6"].cast<py::array_t<double>>();

py::array_t<double> strain = merge_arrays(strain1, strain2);
std::vector<double> g0 = std::vector<double>(strain.data(), strain.data() + strain.size()); // convert numpy array to std::vector.

VectorXd homogenized_stress;

matmodel->setGradient(g0);

solver->solve();

homogenized_stress = solver->get_homogenized_stress();

auto C = solver->get_homogenized_tangent(pert_param);

// Convert data to a py::dict again to send it back to the Micro Manager
py::dict micro_write_data;

// Add stress and stiffness matrix data to Python dict to be returned
std::vector<double> stress13 = {homogenized_stress[0], homogenized_stress[1], homogenized_stress[2]};
micro_write_data["stresses1to3"] = stress13;
std::vector<double> stress46 = {homogenized_stress[3], homogenized_stress[4], homogenized_stress[5]};
micro_write_data["stresses4to6"] = stress46;
std::vector<double> C_1 = {C(0, 0), C(0, 1), C(0, 2)};
micro_write_data["cmat1"] = C_1;
std::vector<double> C_2 = {C(0, 3), C(0, 4), C(0, 5)};
micro_write_data["cmat2"] = C_2;
std::vector<double> C_3 = {C(1, 1), C(1, 2), C(1, 3)};
micro_write_data["cmat3"] = C_3;
std::vector<double> C_4 = {C(1, 4), C(1, 5), C(2, 2)};
micro_write_data["cmat4"] = C_4;
std::vector<double> C_5 = {C(2, 3), C(2, 4), C(2, 5)};
micro_write_data["cmat5"] = C_5;
std::vector<double> C_6 = {C(3, 3), C(3, 4), C(3, 5)};
micro_write_data["cmat6"] = C_6;
std::vector<double> C_7 = {C(4, 4), C(4, 5), C(5, 5)};
micro_write_data["cmat7"] = C_7;
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved

return micro_write_data;
}

PYBIND11_MODULE(PyFANS, m)
{
// optional docstring
m.doc() = "FANS for Micro Manager";

py::class_<MicroSimulation>(m, "MicroSimulation")
.def(py::init<int>())
.def("solve", &MicroSimulation::solve);
}
31 changes: 31 additions & 0 deletions pyfans/micro.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This is the header file for the micro simulation class.
// It is included in the micro_cpp_dummy.cpp file and the micro_cpp_dummy.cpp file is compiled with pybind11 to create a python module.
// The python module is then imported in the Micro Manager.

#pragma once
#include <iostream>
#include <vector>

#include "pybind11/pybind11.h"
#include "pybind11/numpy.h" // numpy arrays
#include "pybind11/stl.h" // std::vector conversion

#include "general.h"
#include "matmodel.h"
#include "solver.h"

namespace py = pybind11;

class MicroSimulation {
public:
MicroSimulation(int sim_id, char *input_file = "input.json");
py::dict solve(py::dict macro_write_data, double dt);

private:
int _sim_id;
Reader reader;
// Hardcoding mechanical models because these definitions need information from the input file.
Matmodel<3> *matmodel;
Solver<3> *solver;
double pert_param = 1e-6; // scalar strain perturbation parameter
};
17 changes: 17 additions & 0 deletions test/test_pyfans/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Test pyFANS

Test pyFANS as standalone library called from a Python script.

## Build pyFANS

Configure the FANS CMake build with the variable `FANS_LIB` set to `ON`.

## Run the test

Run

```bash
python3 run_fans_as_library.py
```

The script creates a pyFANS object and calls the `solve()` method. The script only checks if the pyFANS object is created and the solve function is callable. The result is not checked for correctness.
25 changes: 25 additions & 0 deletions test/test_pyfans/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"ms_filename": "../microstructures/sphere32.h5",
"ms_datasetname": "/sphere/32x32x32/ms",
"ms_L": [1.0, 1.0, 1.0],

"problem_type": "mechanical",
"matmodel": "LinearElasticIsotropic",
"material_properties":{
"bulk_modulus": [62.5000, 222.222],
"shear_modulus": [28.8462, 166.6667]
},

"method": "cg",
"error_parameters":{
"measure": "Linfinity",
"type": "absolute",
"tolerance": 1e-10
},
"n_it": 100,
"macroscale_loading": [
sanathkeshav marked this conversation as resolved.
Show resolved Hide resolved
[[]]
],

"results": []
}
24 changes: 24 additions & 0 deletions test/test_pyfans/run_fans_as_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Run FANS as a Python callable library.
"""
import PyFANS as fans
import numpy as np

# from mpi4py import MPI

# MPI.Init()

micro = fans.MicroSimulation(1)

macro_data = dict()

macro_data["strains1to3"] = np.random.rand(3)
macro_data["strains4to6"] = np.random.rand(3)

dt = 0.0001

output = micro.solve(macro_data, dt) # solve FANS for one load vector g0

print(output)

# MPI.Finalize()
Loading