From bb06b151cb7aa73797e840562ebe09e55bf112d3 Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Wed, 1 Feb 2017 10:42:31 +0200 Subject: [PATCH] problems_mortar_3d.jl: rename function contains to approx_in (#86) * problems_mortar_3d.jl: rename function contains to approx_in See issue #85. `contains` is now renamed to `approx_in`. I also switched argument order, so this function is now called in a same way function `in()`. Usage example: julia> P = Vector[[1.0, 1.0], [2.0, 2.0]] 2-element Array{Array{T,1},1}: [1.0,1.0] [2.0,2.0] julia> q = [1.0, 1.0] + eps(Float64) 2-element Array{Float64,1}: 1.0 1.0 julia> in(q, P) false julia> approx_in(q, P) true Also added docstring and usage example. * Removed `importall base` from code --- src/JuliaFEM.jl | 4 +++- src/abaqus.jl | 2 +- src/fields.jl | 14 ++++++------ src/postprocess_utils.jl | 2 -- src/preprocess.jl | 2 +- src/preprocess_abaqus_reader.jl | 2 -- src/problems_mortar_3d.jl | 40 +++++++++++++++++++++++---------- 7 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/JuliaFEM.jl b/src/JuliaFEM.jl index 7bf8a6c..8033acc 100644 --- a/src/JuliaFEM.jl +++ b/src/JuliaFEM.jl @@ -8,7 +8,9 @@ This is JuliaFEM -- Finite Element Package """ module JuliaFEM -importall Base +import Base: getindex, setindex!, convert, length, size, isapprox, similar, + start, first, next, done, last, endof, vec, ==, +, -, *, /, haskey, copy, + push!, isempty, empty!, append!, sparse, full, read using Logging diff --git a/src/abaqus.jl b/src/abaqus.jl index 64638fe..2002a80 100644 --- a/src/abaqus.jl +++ b/src/abaqus.jl @@ -1,7 +1,7 @@ # This file is a part of JuliaFEM. # License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md -importall Base +import Base: getindex, length using JuliaFEM using JuliaFEM.Preprocess diff --git a/src/fields.jl b/src/fields.jl index b3e7541..d13bc66 100644 --- a/src/fields.jl +++ b/src/fields.jl @@ -89,12 +89,12 @@ function length(f::DCTI) return 1 end -function Base.:*(c::Number, f::DCTI) +function *(c::Number, f::DCTI) return c*f.data end """ Kind of spatial interpolation of DCTI. """ -function Base.:*(N::Matrix, f::DCTI) +function *(N::Matrix, f::DCTI) @assert length(N) == 1 return N[1]*f.data end @@ -179,11 +179,11 @@ function start(field::DVTI) return 1 end -function Base.:+(f1::DVTI, f2::DVTI) +function +(f1::DVTI, f2::DVTI) return DVTI(f1.data + f2.data) end -function Base.:-(f1::DVTI, f2::DVTI) +function -(f1::DVTI, f2::DVTI) return DVTI(f1.data - f2.data) end @@ -192,20 +192,20 @@ function update!(field::DVTI, data::Union{Vector, Dict}) end """ Take scalar product of DVTI and constant T. """ -function Base.:*(T::Number, field::DVTI) +function *(T::Number, field::DVTI) return DVTI(T*field.data) end """ Take dot product of DVTI field and vector T. Vector length must match to the field length and this can be used mainly for interpolation purposes, i.e., u = ∑ Nᵢuᵢ. """ -function Base.:*(T::Vector, f::DVTI) +function *(T::Vector, f::DVTI) @assert length(T) <= length(f) return sum([T[i]*f[i] for i=1:length(T)]) end """ Take outer product of DVTI field and matrix T. """ -function Base.:*(T::Matrix, f::DVTI) +function *(T::Matrix, f::DVTI) n, m = size(T) return sum([kron(T[:,i], f[i]') for i=1:m])' end diff --git a/src/postprocess_utils.jl b/src/postprocess_utils.jl index d4c4016..c6e5cae 100644 --- a/src/postprocess_utils.jl +++ b/src/postprocess_utils.jl @@ -1,8 +1,6 @@ # This file is a part of JuliaFEM. # License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md -importall Base - using JuliaFEM using DataFrames using HDF5 diff --git a/src/preprocess.jl b/src/preprocess.jl index 98b5d8b..58561a1 100644 --- a/src/preprocess.jl +++ b/src/preprocess.jl @@ -12,7 +12,7 @@ - etc only topology related stuff =# -importall Base +import Base: copy using JuliaFEM diff --git a/src/preprocess_abaqus_reader.jl b/src/preprocess_abaqus_reader.jl index af9165a..7a0993d 100644 --- a/src/preprocess_abaqus_reader.jl +++ b/src/preprocess_abaqus_reader.jl @@ -1,8 +1,6 @@ # This file is a part of JuliaFEM. # License is MIT: see https://github.com/JuliaFEM/JuliaFEM.jl/blob/master/LICENSE.md -importall Base - element_has_nodes(::Type{Val{:C3D4}}) = 4 element_has_type( ::Type{Val{:C3D4}}) = :Tet4 diff --git a/src/problems_mortar_3d.jl b/src/problems_mortar_3d.jl index 08d6d3f..92a6b2a 100644 --- a/src/problems_mortar_3d.jl +++ b/src/problems_mortar_3d.jl @@ -72,16 +72,32 @@ function get_cells(P, C; allow_quads=false) return cells end -""" Test does P contain q. """ -function contains{T}(P::Vector{T}, q::T; check_is_close=true, rtol=1.0e-4) - if q in P - return true - end - if check_is_close - for p in P - if isapprox(p, q; rtol=rtol) - return true - end +""" Test does vector P contain approximately q. This function uses isapprox() +internally to make boolean test. + +Examples +-------- +julia> P = Vector[[1.0, 1.0], [2.0, 2.0]] +2-element Array{Array{T,1},1}: + [1.0,1.0] + [2.0,2.0] + +julia> q = [1.0, 1.0] + eps(Float64) +2-element Array{Float64,1}: + 1.0 + 1.0 + +julia> in(q, P) +false + +julia> approx_in(q, P) +true + +""" +function approx_in{T}(q::T, P::Vector{T}; rtol=1.0e-4, atol=0.0) + for p in P + if isapprox(q, p; rtol=rtol, atol=atol) + return true end end return false @@ -104,7 +120,7 @@ function get_polygon_clip(xs, xm, n) # 2. test is slave point inside master, if yes, add to clip for i=1:ns if vertex_inside_polygon(xs[i], xm) - contains(P, xs[i]) && continue + approx_in(xs[i], P) && continue debug("2. $(xs[i]) inside M -> push") push!(P, xs[i]) end @@ -127,7 +143,7 @@ function get_polygon_clip(xs, xm, n) q = xs1 + t*(xs2 - xs1) #info("t=$t, q=$q, q ∈ xm ? $(vertex_inside_polygon(q, xm))") if vertex_inside_polygon(q, xm) - contains(P, q) && continue + approx_in(q, P) && continue debug("3. $q inside M -> push") push!(P, q) end