GithubHelp home page GithubHelp logo

naruki-ichihara / fenics_optimize Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 2.0 1.07 MB

optfx is a module of the FEniCS computing platform for multiphysics optimization problems.

Home Page: https://naruki-ichihara.github.io/fenics_optimize/

License: MIT License

Python 100.00%
python topology optimization topology-optimization fenics petsc mpi

fenics_optimize's Introduction

logo

opt f(x)

Github issues Github forks Github stars Github top language Github license

Note (updated May 2024): This repository is not currently maintained. An updated version is continued to pytop.

Motivation and significance

optfx is a library for the large scale field optimization platform based on FEniCS ecosystem. This module enables reusable and straightforward UFL coding for inverse problem of multi-physics.

This software is the third-party module of fenics project.

This software is based on Lagacy FEniCS (FEniCS2019.1.0). The new version, FEniCSx, is not supported.

This README and documentation is now under construction.

Getting started

import numpy as np
import optfx as opt

opt.parameters["form_compiler"]["optimize"] = True
opt.parameters["form_compiler"]["cpp_optimize"] = True
opt.parameters['form_compiler']['quadrature_degree'] = 5

comm = opt.MPI.comm_world
m = 0.30   # Target rate of the material amount
p = 5      # Penalty parameter
eps = 1e-3 # Material lower bound
R = 0.01   # Helmholtz filter radius
n = 256    # Resolution

def k(a):
    return eps + (1 - eps) * a ** p

mesh = opt.UnitSquareMesh(comm, n, n)
X = opt.FunctionSpace(mesh, 'CG', 1)
f = opt.interpolate(opt.Constant(1e-2), X)

class Initial_density(opt.UserExpression):
    def eval(self, value, x):
        value[0] = m

class Left(opt.SubDomain):
    def inside(self, x, on_boundary):
        gamma = 1/250 + 1e-5
        return x[0] == 0.0 and 0.5 - gamma < x[1] < 0.5 + gamma and on_boundary

U = opt.FunctionSpace(mesh, 'CG', 1)
t = opt.TrialFunction(U)
dt = opt.TestFunction(U)
bc = opt.DirichletBC(U, opt.Constant(0.0), Left())

class PoissonProblem(opt.Module):
    def problem(self, controls):
        rho = controls[0]
        rho = opt.helmholtzFilter(rho, X, R)
        a = opt.inner(opt.grad(t), k(rho)*opt.grad(dt))*opt.dx
        L = opt.inner(f, dt)*opt.dx
        Th = opt.Function(U, name='Temperture')
        opt.solve(a==L, Th, bc)
        J = opt.assemble(opt.inner(opt.grad(Th), k(rho)*opt.grad(Th))*opt.dx)
        rho_bulk = opt.project(opt.Constant(1.0), X)
        rho_0 = opt.assemble(rho_bulk*opt.dx)
        rho_total = opt.assemble(controls[0]*opt.dx)
        rel = rho_total/rho_0
        self.volumeFraction = rel
        return J
    def constraint_volume(self):
        return self.volumeFraction - m

x0 = opt.Function(X)
x0.interpolate(Initial_density())
N = opt.Function(X).vector().size()
min_bounds = np.zeros(N)
max_bounds = np.ones(N)

setting = {'set_lower_bounds': min_bounds,
           'set_upper_bounds': max_bounds,
           'set_maxeval': 1000
          }
params = {'verbosity': 1}

problem = PoissonProblem()
solution = opt.optimize(problem, [x0], {'constraint_volume': [0]}, setting, params)

Installation

Docker

We recomennd to use our docker image. First you should install docker.io or relavant systems. Our docker image was stored in the dockerhub.

If you use the docker.io and docker-compose, the following command pull and run the above image.

docker-compose up

install on your local host

First, make sure to install the following dependencies:

Second, install using pip.

pip install git+https://github.com/Naruki-Ichihara/fenics_optimize.git@main

Contributors

API and documentation

API: API Documentation: Docs

References

Cite

To cite this repository:

@software{fenics_optimize2023github,
  author = {Naruki Ichihara},
  title = {{optfx}: Large scale field optimization platform based on fenics in python},
  url = {https://github.com/Naruki-Ichihara/fenics_optimize},
  version = {0.3.0},
  year = {2023},
}

logo

fenics_optimize's People

Contributors

naruki-ichihara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

iitrabhi mfkiwl

fenics_optimize's Issues

Example in documentation gives 'function' object is not iterable

Describe the bug
I have copied the example in the documentation to an example.py file and have runned it in the Singularity container, which will result in TypeError: 'function' object is not iterable.

To Reproduce

Singularity> python3 example.py
No protocol specified
Calling FFC just-in-time (JIT) compiler, this may take some time.
Traceback (most recent call last):
  File "example.py", line 52, in <module>
    op.MMAoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)
  File "/fenics-optimize/fenics_optimize/Optimizer.py", line 35, in MMAoptimize
    for pack in zip(constraints, constraints_targets):
TypeError: 'function' object is not iterable
Singularity>

Changing in rule 52 to to HSLOptimize instead of MMAoptimize will result in a different error.

op.HSLoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)

Singularity> python3 example.py
No protocol specified
Traceback (most recent call last):
  File "example.py", line 52, in <module>
    op.HSLoptimize(problemSize, x0, forward, constraint, maxeval=100, bounds=[0, 1], rel=1e-20)
  File "/fenics-optimize/fenics_optimize/Optimizer.py", line 92, in HSLoptimize
    cl = [-1e19] * len(constraints_targets)
TypeError: object of type 'NoneType' has no len()

Expected behavior
A flawless execution of the example.

Screenshots
N.A.

Desktop (please complete the following information):

  • OS: Ubuntu 20.04 with Sigularity 3.9.8
  • Browser N.A.
  • Version N.A.

Additional context
Add any other context about the problem here.

Visualization of angle optimization

Hi, thanks for the great tool.
I was wondering how to visualize the angles after optimizing.
The files are written in .pvd doesn't show appropriate angles in paraview.
Is any other visualization that can be helpful ?

Thanks

Element-wise constraint

Hi
Thanks for the great tool.
I am trying to do a Discrete material optimization (DMO).
I am not sure how to apply the element-wise constraints.
For example, I have several variables. But at each element, the sum should be 1.

Thanks

Docs and api

I will write docs included some physical problem examples.

Concurrent topology optimization

Hi
Thanks for the great tool.
I am trying to do a Concurrent topology optimization.
Optimizing both macro and microstructure.
However, i get an error
"Adjoint value is None, is the functional independent of the control variable?"
My set up is following:
``

    rho = controls[0]

    rho_m = controls[1]

    rho = op.helmholtzFilter(rho, X, R)

    rho_m = op.helmholtzFilter(rho_m, X_m, R)

    Q, us_m = Homogenization_2D(rho_m)

    Q = as_matrix(Q)

``

I am getting the homogenized material property from the function.
my objective is minimize the compliance of Macro structure.
and with two constraints:

``

 def constraint_microvolume(self):

    return self.microfraction - target

def constraint_volume(self):

    return self.fraction - target

``
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.