E.vector()[:] = 2
E.vector()[pos]=5
would assign the value 5 the cell with index pos
and 2 to all other cells
E.vector()[:] = 2
E.vector()[pos]=5
would assign the value 5 the cell with index pos
and 2 to all other cells
Hi Dokken,
Thank you very much.
Hi,
I am getting an error in a readable file that I am not understanding.
Code
import meshio
msh = meshio.read("/home/pierre/Desktop/GMSH1/carre.msh")
for cell in msh.cells:
triangle_cells = cell.data
for key in msh.cell_data_dict["gmsh:physical"].keys():
if key == "triangle":
triangle_data = msh.cell_data_dict["gmsh:physical"][key]
mesh =meshio.Mesh(points=msh.points,
cells=[("triangle", triangle_cells)],
cell_data={"name_to_read":[triangle_data]})
meshio.write("test.xdmf",mesh)
E = 2.1e11 #p[0]
nu = 0.3 #p[1]
beta = (E)/(1-(nu*nu))
C = as_matrix([[beta, beta*nu, 0],[beta*nu, beta, 0],[0, 0, beta*(1-nu)/2]])
#********************************************************************#
def eps(v):
return sym(grad(v))
def strain2voigt(e):
"""e is a 2nd-order tensor, returns its Voigt vectorial representation"""
return as_vector([e[0,0],e[1,1],2*e[0,1]])
def voigt2stress(s):
"""
s is a stress-like vector (no 2 factor on last component)
returns its tensorial representation
"""
return as_tensor([[s[0],s[2]],[s[2],s[1]]])
def sigma(v):
return voigt2stress(dot(C,strain2voigt(eps(v))))
#********************************************************************#
class Top(SubDomain):
def inside(self, x, on_boundary):
return near(x[1],10) and on_boundary
class Left(SubDomain):
def inside(self, x, on_boundary):
return near(x[0],0) and on_boundary
class Bottom(SubDomain):
def inside(self, x, on_boundary):
return near(x[1],0) and on_boundary
#********************************************************************#
# exterior facets MeshFunction
facets = MeshFunction("size_t", mesh, 1)
Error message
File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 59, in <module>
facets = MeshFunction("size_t", mesh, 1)
File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/mesh/meshfunction.py", line 18, in __new__
return fn(mesh, dim)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int)
2. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int, arg2: int)
3. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: str)
4. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: int, arg2: dolfin.cpp.mesh.MeshDomains)
5. dolfin.cpp.mesh.MeshFunctionSizet(arg0: dolfin.cpp.mesh.Mesh, arg1: dolfin::MeshValueCollection<unsigned long>)
Invoked with: <meshio mesh object>
Number of points: 146
Number of cells:
triangle: 250
Cell data: name_to_read, 1
I tried to correct it based on the error message but unsuccessfully.
Please need your help again.
Thanks
You need to load the mesh from XDMF
. i.e.
from dolfin import *
# First write mesh to xdmf
# ...
# Then define functions and classes
# ...
# Then load mesh
mesh = Mesh()
with XDMFFile("test.xdmf") as infile:
infile.read(mesh)
# Then create mesh function
facets = MeshFunction("size_t", mesh, 1)
Thank you dokken,
I have followed your instructions, but I have another error message:
Shapes do not match: <ListTensor id=140219301787456> and <Sym id=140219301787200>.
Traceback (most recent call last):
File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 86, in <module>
a = inner(sigma(du), eps(u_))*dx
File "/usr/lib/python3/dist-packages/ufl/operators.py", line 158, in inner
return Inner(a, b)
File "/usr/lib/python3/dist-packages/ufl/tensoralgebra.py", line 147, in __new__
error("Shapes do not match: %s and %s." % (ufl_err_str(a), ufl_err_str(b)))
File "/usr/lib/python3/dist-packages/ufl/log.py", line 158, in error
raise self._exception_type(self._format_raw(*message))
UFLException: Shapes do not match: <ListTensor id=140219301787456> and <Sym id=140219301787200>.
Please, how to solve this?
You need to provide a minimal code example that reproduces the error, as this is related to the variational form
Ok, based on your previous advice, I have developed the code bellow:
from dolfin import *
# First write mesh to xdmf
# ...
# Then define functions and classes
# ...
# Then load mesh
mesh = Mesh()
with XDMFFile("test.xdmf") as infile:
infile.read(mesh)
# exterior facets MeshFunction
facets = MeshFunction("size_t", mesh, 1)
facets.set_all(0)
Top().mark(facets, 1)
Left().mark(facets, 2)
Bottom().mark(facets, 3)
ds = Measure("ds", subdomain_data = facets)
# Define function space
V = VectorFunctionSpace(mesh, 'CG', 1)
# Define variational problem
du = TrialFunction(V)
u_ = TestFunction(V)
u = Function(V, name='Displacement')
a = inner(sigma(du), eps(u_))*dx
# uniform traction on top boundary
Ttrac = Constant((0., 1e7))
l = dot(Ttrac, u_)*ds(1)
It is the code that gives the previous error I sent you.
If your mesh is 2 dimensional, you Need to prune the z-coordinates of points before writing to file (you can use mesh.prune_z_0()
for the mesh above.
You can verify this by printing mesh.geometry().dim()
after loading the mesh into dolfin; it should be 2
Sorry Dokken, another error occurs when I was trying to prune the z-coordinates.
from __future__ import print_function
from fenics import *
from dolfin import *
from mshr import *
import numpy as np
from numpy import linalg as LA
import meshio
msh = meshio.read("/home/pierre/Desktop/GMSH1/carre.msh")
for cell in msh.cells:
triangle_cells = cell.data
for key in msh.cell_data_dict["gmsh:physical"].keys():
if key == "triangle":
triangle_data = msh.cell_data_dict["gmsh:physical"][key]
mesh =meshio.Mesh(points=msh.points,
cells=[("triangle", triangle_cells)],
cell_data={"name_to_read":[triangle_data]})
mesh.prune_z_0()
meshio.write("test.xdmf",mesh)
Error message:
File "/home/pierre/Desktop/MesEssais/essai28_jun/essai28_jun22.py", line 18, in <module>
mesh.prune_z_0()
AttributeError: 'Mesh' object has no attribute 'prune_z_0'
Thank you for spending your time with me.
What version of meshio are you using? This function is part of: meshio/_mesh.py at 20ef6275f4fdd07c2a85082761dd38fccf393476 · nschloe/meshio · GitHub
and has been in meshio since version 4.3.0
meshio version : 2.3.10 on ubuntu 20.04 environment.
I am trying to update it to the latest version.
Come back soon
thanks
Hi,
My meshio version is 5.3.4 but I get the same error. Can you please help me with this?
Thanks
It worked. Thank you so much.
Hi Dokken, thank you very much for your help! I have a question to ask you. When I run this code, it returns this error. Is this a problem with my “fenics_adjoint”, or is it something else?
See reply in: Implement point source in the variational formulation - #11 by dokken
Pleaee avoid making duplicate questions.
I apologize for this and thank you for the reminder.
Hi Dokken, thank you very much for your help. Am I to understand that the position (1, 1) where delta is loaded is its function space and not its coordinates in the actual space.