-
Notifications
You must be signed in to change notification settings - Fork 2
79 add random field feature #109
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
Draft
danielandresarcones
wants to merge
17
commits into
main
Choose a base branch
from
79-add-random-field-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8a41a39
First implementation metadata report
danielandresarcones 19c4133
Testing sensor metadata reporting
danielandresarcones cc96dc7
Fix format and units
danielandresarcones e06cd40
Fixed units for push tests
danielandresarcones 4dc82e8
Initial schema
danielandresarcones d519065
Implemented sensor metadata report and schema
danielandresarcones bae469d
Validation of sensors against schema
danielandresarcones 78b8d3b
Updated environment
danielandresarcones b909283
Fixed test_sensor
danielandresarcones aa7bb1c
Initial rebuilding sensor
danielandresarcones b039860
Rebuild sensors works without units
danielandresarcones 6e1caad
Set units in rebuilt sensors
danielandresarcones 0612020
Merge branch '79-add-random-field-feature' of https://github.com/BAMr…
danielandresarcones d8d88d0
Initial GRF translation
danielandresarcones 05e76bb
Working GRF problem
danielandresarcones 452cc0a
Modified paraview output
danielandresarcones ed89c6a
GRF parameters as input
danielandresarcones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"tests" | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true, | ||
"python.analysis.typeCheckingMode": "basic" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ dependencies: | |
- matplotlib | ||
- python-gmsh | ||
- meshio | ||
- jsonschema | ||
- scipy | ||
# tests | ||
- pytest | ||
- coverage | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/fenicsxconcrete/finite_element_problem/linear_elasticity_grf.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from pathlib import Path, PosixPath | ||
|
||
import dolfinx as df | ||
import numpy as np | ||
import pint | ||
import ufl | ||
from petsc4py.PETSc import ScalarType | ||
|
||
from fenicsxconcrete.experimental_setup.base_experiment import Experiment | ||
from fenicsxconcrete.finite_element_problem.linear_elasticity import LinearElasticity | ||
from fenicsxconcrete.gaussian_random_field import Randomfield | ||
|
||
|
||
class LinearElasticityGRF(LinearElasticity): | ||
"""Material definition for linear elasticity""" | ||
|
||
def __init__( | ||
self, | ||
experiment: Experiment, | ||
parameters: dict[str, pint.Quantity], | ||
pv_name: str = "pv_output_full", | ||
pv_path: str = None, | ||
) -> None: | ||
super().__init__(experiment, parameters, pv_name, pv_path) | ||
# TODO There should be more elegant ways of doing this | ||
self.pv_egrf_file = Path(pv_path) / (pv_name + "egrf.xdmf") | ||
self.pv_nugrf_file = Path(pv_path) / (pv_name + "nugrf.xdmf") | ||
|
||
def setup(self) -> None: | ||
self.field_function_space = df.fem.FunctionSpace(self.experiment.mesh, ("CG", 1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the degree 1 compulsory or should it accept any? |
||
self.lambda_ = df.fem.Function(self.field_function_space) | ||
self.mu = df.fem.Function(self.field_function_space) | ||
|
||
lame1, lame2 = self.get_lames_constants() | ||
self.lambda_.vector[:] = lame1 | ||
self.mu.vector[ | ||
: | ||
] = lame2 # make this vector as a fenics constant array. Update the lame1 and lame2 in each iteration. | ||
|
||
# define function space ets. | ||
self.V = df.fem.VectorFunctionSpace(self.mesh, ("Lagrange", self.p["degree"])) # 2 for quadratic elements | ||
self.V_scalar = df.fem.FunctionSpace(self.mesh, ("Lagrange", self.p["degree"])) | ||
|
||
# Define variational problem | ||
self.u_trial = ufl.TrialFunction(self.V) | ||
self.v = ufl.TestFunction(self.V) | ||
|
||
# initialize L field, not sure if this is the best way... | ||
zero_field = df.fem.Constant(self.mesh, ScalarType(np.zeros(self.p["dim"]))) | ||
self.L = ufl.dot(zero_field, self.v) * ufl.dx | ||
|
||
# apply external loads | ||
external_force = self.experiment.create_force_boundary(self.v) | ||
if external_force: | ||
self.L = self.L + external_force | ||
|
||
body_force = self.experiment.create_body_force(self.v) | ||
if body_force: | ||
self.L = self.L + body_force | ||
|
||
# boundary conditions only after function space | ||
bcs = self.experiment.create_displacement_boundary(self.V) | ||
|
||
self.a = ufl.inner(self.sigma(self.u_trial), self.epsilon(self.v)) * ufl.dx | ||
self.weak_form_problem = df.fem.petsc.LinearProblem( | ||
self.a, | ||
self.L, | ||
bcs=bcs, | ||
petsc_options={"ksp_type": "preonly", "pc_type": "lu"}, | ||
) | ||
|
||
# Random E and nu fields. | ||
def random_field_generator( | ||
self, | ||
field_function_space, | ||
cov_name, | ||
mean, | ||
correlation_length1, | ||
correlation_length2, | ||
variance, | ||
no_eigen_values, | ||
ktol, | ||
): | ||
random_field = Randomfield( | ||
field_function_space, | ||
cov_name, | ||
mean, | ||
correlation_length1, | ||
correlation_length2, | ||
variance, | ||
no_eigen_values, | ||
ktol, | ||
) | ||
# random_field.solve_covariance_EVP() | ||
return random_field | ||
|
||
def parameters_conversion(self, lognormal_mean, lognormal_sigma): | ||
from math import sqrt | ||
|
||
normal_mean = np.log(lognormal_mean / sqrt(1 + (lognormal_sigma / lognormal_mean) ** 2)) | ||
normal_sigma = np.log(1 + (lognormal_sigma / lognormal_mean) ** 2) | ||
return normal_mean, normal_sigma | ||
|
||
def get_lames_constants( | ||
self, | ||
): | ||
# Random E and nu fields. | ||
E_mean, E_variance = self.parameters_conversion(self.p["E"], 100e9) # 3 | ||
Nu_mean, Nu_variance = self.parameters_conversion(self.p["nu"], 0.3) # 0.03 | ||
|
||
danielandresarcones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.E_randomfield = self.random_field_generator( | ||
self.field_function_space, "squared_exp", E_mean, 0.3, 0.05, E_variance, 3, 0.01 | ||
) | ||
self.E_randomfield.create_random_field(_type="random", _dist="LN") | ||
|
||
self.nu_randomfield = self.random_field_generator( | ||
self.field_function_space, "squared_exp", Nu_mean, 0.3, 0.05, Nu_variance, 3, 0.01 | ||
) | ||
self.nu_randomfield.create_random_field(_type="random", _dist="LN") | ||
|
||
lame1 = (self.E_randomfield.field.vector[:] * self.nu_randomfield.field.vector[:]) / ( | ||
(1 + self.nu_randomfield.field.vector[:]) * (1 - 2 * self.nu_randomfield.field.vector[:]) | ||
) | ||
lame2 = self.E_randomfield.field.vector[:] / (2 * (1 + self.nu_randomfield.field.vector[:])) | ||
return lame1, lame2 | ||
|
||
# TODO move this to sensor definition!?!?! | ||
def pv_plot(self, t: int = 0) -> None: | ||
# TODO add possibility for multiple time steps??? | ||
# Displacement Plot | ||
|
||
# "Displacement.xdmf" | ||
# pv_output_file | ||
# TODO Look into how to unify in one file | ||
with df.io.XDMFFile(self.mesh.comm, self.pv_output_file, "w") as xdmf: | ||
danielandresarcones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
xdmf.write_mesh(self.mesh) | ||
xdmf.write_function(self.displacement) | ||
with df.io.XDMFFile(self.mesh.comm, self.pv_egrf_file, "w") as xdmf: | ||
xdmf.write_mesh(self.mesh) | ||
xdmf.write_function(self.E_randomfield.field) | ||
with df.io.XDMFFile(self.mesh.comm, self.pv_nugrf_file, "w") as xdmf: | ||
xdmf.write_mesh(self.mesh) | ||
xdmf.write_function(self.nu_randomfield.field) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.