Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2d1061a
Preliminary version of FEM solver.
jbrezmorf Nov 12, 2023
e7b552f
Merge remote-tracking branch 'origin/master' into JB_homo
jbrezmorf Mar 20, 2024
6b28638
Equivalent scalar and tensor properties.
jbrezmorf Mar 26, 2024
8c48cb1
Improved FEM test.
jbrezmorf Mar 26, 2024
7cb5cc1
Add bgem.core from Endorse
jbrezmorf Mar 26, 2024
8902654
Reference solution for two-scale homogenization test.
jbrezmorf Mar 26, 2024
25fd13b
Working anisotropic homogenization, test_rasterize
jbrezmorf Apr 3, 2024
489aa00
Minor improvements and fixes.
jbrezmorf Apr 3, 2024
f54c4ca
El and dof natural numbering consistent with ndarray.
jbrezmorf Apr 7, 2024
b20ad52
Fix numberings in test_two_scale
jbrezmorf Apr 7, 2024
e139429
Triangle and tetra refinement.
jbrezmorf Apr 11, 2024
58d01cd
Sparce FEM solver, refinement flow123d projection
jbrezmorf Apr 15, 2024
9084559
Improved creation of Population
jbrezmorf Apr 19, 2024
d621ac8
Merge branch 'JB_e' into JB_homo
jbrezmorf Apr 26, 2024
0c1ef6d
Fix attrs package usage.
jbrezmorf May 2, 2024
90ce12b
Add decovalex_transform to make reference voxelization work.
jbrezmorf May 2, 2024
b6eac0f
Sparse mesh missing dependencies.
jbrezmorf May 2, 2024
4827c9c
DFN refactoring (WIP)
jbrezmorf May 2, 2024
8ef0f34
Stub of new test files
jbrezmorf May 2, 2024
94c370b
stub changes to carfully review
jbrezmorf May 2, 2024
f319612
Merge modifications from stash.
jbrezmorf May 2, 2024
c065538
Merge branch 'master' into JB_homo
jbrezmorf May 2, 2024
b6c3562
Fix core.config for yamlinclude
jbrezmorf Jun 12, 2024
1128610
Refactoring of the stochastic fracture generation.
jbrezmorf Jun 12, 2024
790cb99
Upscaling and voxelization tools update.
jbrezmorf Jun 12, 2024
5cee57f
Split FEM into Grid and FEM
jbrezmorf Jun 20, 2024
d98adfd
Elementary PyVista plotting
jbrezmorf Jun 25, 2024
80dcb34
Fixes in DFN
jbrezmorf Jul 30, 2024
d8e957e
Vectorized transfom matrix for fractures.
jbrezmorf Jul 30, 2024
74fbdcc
Fixed vectorised transform_mat
jbrezmorf Aug 2, 2024
84d6457
FractureSet AABB, inv_transfomr
jbrezmorf Aug 14, 2024
95d6a39
More robust tests and fixes of transform_mat
jbrezmorf Aug 17, 2024
b02506c
Working intersection tests
jbrezmorf Aug 18, 2024
62fc5e6
Basic functionality of intersection field interpolation.
jbrezmorf Aug 20, 2024
1cf3363
Push some temporary work on ObjectSet.
jbrezmorf Nov 5, 2024
b057e88
Make vectorized rotation staticmethod
jbrezmorf Dec 23, 2024
b1b6146
Fixed all stochastic tests
jbrezmorf Oct 2, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tests/upscale/2d_flow_homo_samples
# Merge
*.py.orig

Expand Down
4 changes: 2 additions & 2 deletions .run/pytest in stochastic.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_keywords" value="&quot;&quot;" />
Expand Down
4 changes: 2 additions & 2 deletions .run/pytest test_bspline_approx.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_keywords" value="&quot;&quot;" />
Expand Down
4 changes: 2 additions & 2 deletions .run/pytest in gmsh.run.xml → .run/test in gmsh.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_keywords" value="&quot;&quot;" />
Expand Down
4 changes: 2 additions & 2 deletions .run/test_brep_writer.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="_new_keywords" value="&quot;&quot;" />
Expand Down
3 changes: 2 additions & 1 deletion .run/tox all.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="/usr/bin/python3" />
<option name="SDK_NAME" value="Python 3.10 (2)" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
Expand Down
4 changes: 3 additions & 1 deletion .run/tox in gmsh.run.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="tox in gmsh" type="Tox" factoryName="Tox">
<module name="bgem" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="/usr/bin/python3" />
<option name="SDK_NAME" value="Python 3.10 (2)" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
Expand Down
3 changes: 2 additions & 1 deletion .run/tox in stochastic.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<module name="bgem" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="/usr/bin/python3.8" />
<option name="SDK_HOME" value="/usr/bin/python3" />
<option name="SDK_NAME" value="Python 3.10 (2)" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
Expand Down
8 changes: 5 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pytest
numpy
scipy
bih

# some test plottings that are not part of auto test suite
matplotlib
joblib
plotly
pyvista # fem plot
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
# eg: 'keyword1', 'keyword2', 'keyword3',
],

packages=['bgem', 'bgem.polygons', 'bgem.bspline', 'bgem.gmsh', 'bgem.external', 'bgem.geometry', 'bgem.stochastic'], #setuptools.find_packages(where='src'),
packages=['bgem', 'bgem.polygons', 'bgem.bspline', 'bgem.gmsh', 'bgem.external', 'bgem.geometry', 'bgem.stochastic',
'bgem.core', 'bgem.upscale'], #setuptools.find_packages(where='src'),
package_dir={'': 'src'},
#py_modules=[os.path.splitext(os.path.basename(path))[0] for path in glob.glob('src/*.py')],
# package_data={
Expand All @@ -56,7 +57,7 @@
include_package_data=True,
zip_safe=False,
#install_requires=['numpy', 'scipy', 'bih', 'gmsh-sdk<=4.5.1'],
install_requires=['numpy>=1.13.4', 'pandas', 'scipy', 'bih', 'gmsh>=4.10.4'],
install_requires=['numpy>=1.13.4', 'pandas', 'scipy', 'gmsh>=4.10.4', 'pyyaml-include', 'bih', 'nptyping', 'pyamg', 'attrs'],
# incompatible changes in SDK in release 4.6.0 to be changed in the new release of bgem
python_requires='>=3',
# extras_require={
Expand Down
7 changes: 7 additions & 0 deletions src/bgem/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .memoize import EndorseCache, memoize, File
from .common import substitute_placeholders, sample_from_population, workdir, array_attr
from .config import dotdict, load_config, apply_variant, dump_config
from .report import report
from .flow_call import call_flow, FlowOutput

year = 365.2425 * 24 * 60 * 60
169 changes: 169 additions & 0 deletions src/bgem/core/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import os.path
from typing import *
import shutil
import attrs
import numpy.typing as npt

from pathlib import Path
import numpy as np
import logging

from .memoize import File

class workdir:
"""
Context manager for creation and usage of a workspace dir.

name: the workspace directory
inputs: list of files and directories to copy into the workspaceand
TODO: fine a sort of robust ad portable reference
clean: if true the workspace would be deleted at the end of the context manager.
TODO: clean_before / clean_after
TODO: File constructor taking current workdir environment, openning virtually copied files.
TODO: Workdir would not perform change of working dir, but provides system interface for: subprocess, file openning
only perform CD just before executing a subprocess also interacts with concept of an executable.
portable reference and with lazy evaluation. Optional true copy possible.
"""
CopyArgs = Union[str, Tuple[str, str]]
def __init__(self, name:str="sandbox", inputs:List[CopyArgs] = None, clean=False):

if inputs is None:
inputs = []
self._inputs = inputs
self.work_dir = os.path.abspath(name)
Path(self.work_dir).mkdir(parents=True, exist_ok=True)
self._clean = clean
self._orig_dir = os.getcwd()

def copy(self, src, dest=None):
"""
:param src: Realtive or absolute path.
:param dest: Relative path with respect to work dir.
Default is the same as the relative source path,
for abs path it is the just the last name in the path.
"""
if isinstance(src, File):
src = src.path
if isinstance(dest, File):
dest = dest.path
#if dest == ".":
# if os.path.isabs(src):
# dest = os.path.basename(src)
# else:
# dest = src
if dest is None:
dest = ""
dest = os.path.join(self.work_dir, dest, os.path.basename(src))
dest_dir, _ = os.path.split(dest)
if not os.path.isdir(dest_dir):
#print(f"MAKE DIR: {dest_dir}")
Path(dest_dir).mkdir(parents=True, exist_ok=True)
abs_src = os.path.abspath(src)

# ensure that we always update the target
if os.path.isdir(dest):
shutil.rmtree(dest)
elif os.path.isfile(dest):
os.remove(dest)

# TODO: perform copy, link or redirectio to src during extraction of the File object from dictionary
# assumes custom tag for file, file_link, file_copy etc.
if os.path.isdir(src):
#print(f"COPY DIR: {abs_src} TO DESTINATION: {dest}")
shutil.copytree(abs_src, dest, dirs_exist_ok=True)
else:
try:
shutil.copy2(abs_src, dest)
except FileNotFoundError:
FileNotFoundError(f"COPY FILE: {abs_src} TO DESTINATION: {dest}")

def __enter__(self):
for item in self._inputs:
#print(f"treat workspace item: {item}")
if isinstance(item, Tuple):
self.copy(*item)
else:
self.copy(item)
os.chdir(self.work_dir)

return self.work_dir

def __exit__(self, type, value, traceback):
os.chdir(self._orig_dir)
if self._clean:
shutil.rmtree(self.work_dir)


def substitute_placeholders(file_in: str, file_out: str, params: Dict[str, Any]):
"""
In the template `file_in` substitute the placeholders in format '<name>'
according to the dict `params`. Write the result to `file_out`.
TODO: set Files into params, in order to compute hash from them.
TODO: raise for missing value in dictionary
"""
used_params = []
files = []
with open(file_in, 'r') as src:
text = src.read()
for name, value in params.items():
if isinstance(value, File):
files.append(value)
value = value.path
placeholder = '<%s>' % name
n_repl = text.count(placeholder)
if n_repl > 0:
used_params.append(name)
text = text.replace(placeholder, str(value))
with open(file_out, 'w') as dst:
dst.write(text)

return File(file_out, files), used_params


# Directory for all flow123d main input templates.
# These are considered part of the software.

# TODO: running with stdout/ stderr capture, test for errors, log but only pass to the main in the case of
# true error

def array_attr(shape, dtype=np.double, default=[]):
"""
Numpy array attribut definition for the attrs library.
- shape and dtype specification and auto conversion
- defalut empty array
"""
# Unfortunately broadcast_to does not support -1 in the target_shape
# assume shape in form (-1, ...).
assert shape[0] == -1

def converter(x):
rev_shape = reversed( (len(x), *shape[1:]) )
return np.broadcast_to(np.array(x).T, rev_shape).T

return attrs.field(
type=npt.NDArray[dtype],
converter=converter,
default=default)


def sample_from_population(n_samples:int, frequency:Union[np.array, int]):
if type(frequency) is int:
frequency = np.full(len(frequency), 1, dtype=int)
else:
frequency = np.array(frequency, dtype=int)

cumul_freq = np.cumsum(frequency)
total_samples = np.sum(frequency)
samples = np.random.randint(0, total_samples, size=n_samples + 1)
samples[-1] = total_samples # stopper
sample_seq = np.sort(samples)
# put samples into bins given by cumul_freq
bin_samples = np.empty_like(samples)
i_sample = 0
for ifreq, c_freq in enumerate(cumul_freq):

while sample_seq[i_sample] < c_freq:
bin_samples[i_sample] = ifreq
i_sample += 1

return bin_samples[:-1]
Loading
Loading