DispersionSolver.cc 8.9 KB
Newer Older
Bastian Telgen's avatar
Bastian Telgen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// © 2021 ETH Zurich, Mechanics and Materials Lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this fKpath except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/////////////////////////////////////////////////////////////////////
// compare to https://doi.org/10.1038/s41598-019-46089-9
/////////////////////////////////////////////////////////////////////

#include "ae108/assembly//plugins/AssembleMassMatrixPlugin.h"
#include "ae108/assembly/Assembler.h"
#include "ae108/cmdline/CommandLineOptionParser.h"
#include "ae108/cpppetsc/Context.h"
#include "ae108/cpppetsc/GeneralizedMeshBoundaryCondition.h"
#include "ae108/cpppetsc/Mesh.h"
#include "ae108/cpppetsc/ParallelComputePolicy.h"
#include "ae108/cpppetsc/Vector.h"
#include "ae108/cpppetsc/asTransformedMatrix.h"
#include "ae108/cpppetsc/asTransposedMatrix.h"
#include "ae108/cpppetsc/computeElementsOfMatrix.h"
#include "ae108/cppslepc/Context.h"
#include "ae108/elements/TimoshenkoBeamElementWithMass.h"
#include "ae108/elements/mesh/read_mesh_from_file.h"
#include "ae108/elements/mesh/refine_segment_mesh.h"
#include "ae108/elements/tensor/as_vector.h"
#include "ae108/solve/boundaryConditionsToTransform.h"
#include "utilities/assign_lattice_to.h"
#include "utilities/circular_beam_properties.h"
#include "utilities/compute_eigenvalues.h"
#include "utilities/read_config.h"

#include <fstream>
#include <iostream>
#include <sstream>

using namespace ae108;

using Policy = cpppetsc::ParallelComputePolicy;
using Context = cppslepc::Context<Policy>;
using Mesh = cpppetsc::Mesh<Policy>;
using Vector = cpppetsc::Vector<Policy>;
using BoundaryCondition = cpppetsc::GeneralizedMeshBoundaryCondition<Mesh>;

constexpr auto topological_dimension = Mesh::TopologicalDimension{1};
constexpr auto coordinate_dimension = Mesh::CoordinateDimension{3};

using Point = std::array<Mesh::real_type, coordinate_dimension>;

using Element = elements::TimoshenkoBeamElementWithMass<
    coordinate_dimension, Vector::value_type, Vector::real_type>;

using Properties =
    elements::TimoshenkoBeamWithMassProperties<Mesh::real_type,
                                               coordinate_dimension>;

using Plugins =
    assembly::FeaturePlugins<assembly::plugins::AssembleEnergyPlugin,
                             assembly::plugins::AssembleForceVectorPlugin,
                             assembly::plugins::AssembleStiffnessMatrixPlugin,
                             assembly::plugins::AssembleMassMatrixPlugin>;

using Assembler = assembly::Assembler<Element, Plugins, Policy>;

using elements::tensor::as_vector;

int main(int argc, char **argv) {
  const auto context = Context(&argc, &argv);

Bastian Telgen's avatar
Bastian Telgen committed
78
79
  PetscPrintf(Policy::communicator(), "Read config\n");

Bastian Telgen's avatar
Bastian Telgen committed
80
  auto index = Mesh::size_type(1);
Bastian Telgen's avatar
Bastian Telgen committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  std::string config_file = "/mnt/io/validation/data/cube.config";
  std::string input_folder = "/mnt/io/validation/data/";
  std::string output_folder = "/mnt/io/";
  auto number_of_eigenfrequencies = Mesh::size_type(18);
  auto max_element_length = Mesh::real_type(0.004);
  cmdline::CommandLineOptionParser(std::cerr)
      .withOption("config_file,c", "Config file.", &config_file)
      .withOption("index,n", "Index of configuration.", &index)
      .withOption("input_folder,i", "Input folder.", &input_folder)
      .withOption("output_folder,o", "Output folder.", &output_folder)
      .withOption("number_of_eigenfrequencies,e",
                  "Requested number of eigenfrequencies.",
                  &number_of_eigenfrequencies)
      .withOption("max_element_length,m", "Maximum length of a beam element.",
                  &max_element_length)
      .parse(argc, argv);

  std::ifstream fConfig(config_file);
  const auto config = read_config<coordinate_dimension>(fConfig, index);

  std::ifstream fKpath(input_folder + config.name + ".kpath");
  FILE *out = fopen((output_folder + config.name + ".dispersion").c_str(), "w");

  auto properties = circular_beam_properties<Properties>(
      config.young_modulus, config.poisson_ratio, config.radius,
      config.density);

  const auto &lattice_vectors = config.lattice_vectors;

  const auto geometry = elements::mesh::refine_segment_mesh(
      elements::mesh::read_mesh_from_file<Point>(input_folder + config.name +
                                                 ".dat"),
      max_element_length);
Bastian Telgen's avatar
Bastian Telgen committed
114
115
116
  PetscPrintf(Policy::communicator(),
              "The mesh consists of %d elements and %d nodes\n",
              geometry.connectivity().size(), geometry.number_of_positions());
Bastian Telgen's avatar
Bastian Telgen committed
117
118
119
120
121
122
123

  const auto mesh = Mesh::fromConnectivity(
      topological_dimension, coordinate_dimension, geometry.connectivity(),
      geometry.number_of_positions(), Element::degrees_of_freedom(), 0);

  auto assembler = Assembler();

Bastian Telgen's avatar
Bastian Telgen committed
124
  PetscPrintf(Policy::communicator(), "Find periodic couples\n");
Bastian Telgen's avatar
Bastian Telgen committed
125
126
127
  auto element_source = ae108::homogenization::utilities::assign_lattice_to(
      geometry.connectivity(), geometry.positions(), lattice_vectors);

Bastian Telgen's avatar
Bastian Telgen committed
128
129
130
131
  const auto source_of = ae108::homogenization::utilities::assign_lattice_to(
      geometry.positions(), lattice_vectors);

  PetscPrintf(Policy::communicator(), "Assemble mass and stiffness matrix\n");
Bastian Telgen's avatar
Bastian Telgen committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  for (const auto &element : mesh.localElements()) {
    elements::tensor::Tensor<Mesh::real_type, coordinate_dimension>
        element_axis;
    as_vector(&element_axis) =
        as_vector(&geometry.position_of_vertex(
            geometry.connectivity().at(element.index()).at(1))) -
        as_vector(&geometry.position_of_vertex(
            geometry.connectivity().at(element.index()).at(0)));

    const auto weight =
        1. / std::count(element_source.begin(), element_source.end(),
                        element_source[element.index()]);

    assembler.emplaceElement(
        element,
        Element::Element(weight * timoshenko_beam_stiffness_matrix(element_axis,
                                                                   properties)),
        weight *
            timoshenko_beam_consistent_mass_matrix(element_axis, properties));
  }

  const auto K = [&]() {
    auto K = Mesh::matrix_type::fromMesh(mesh);
    assembler.assembleStiffnessMatrix(Mesh::vector_type::fromLocalMesh(mesh),
                                      Element::Time{0.}, &K);
    K.finalize();
    return K;
  }();

Bastian Telgen's avatar
Bastian Telgen committed
161
  PetscPrintf(Policy::communicator(), "Solve Bloch Eigenvalue Problems\n");
Bastian Telgen's avatar
Bastian Telgen committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  const auto M = [&]() {
    auto M = Mesh::matrix_type::fromMesh(mesh);
    assembler.assembleMassMatrix(&M);
    M.finalize();
    return M;
  }();

  std::string line;
  while (std::getline(fKpath, line)) {
    std::stringstream linestream(line);
    std::string value;
    Point wave_vector({0, 0, 0});
    std::size_t i = 0;
    for (std::size_t i = 0; i < coordinate_dimension; i++) {
      std::getline(linestream, value, ',');
      wave_vector[i] = std::atof(value.c_str());
    }
    const auto T = [&](const Point &wave_vector) {
      std::vector<BoundaryCondition> boundary_conditions;
      for (const auto &vertex : mesh.localVertices()) {
        const Mesh::size_type &target = vertex.index();
        const Mesh::size_type &source = source_of[target];
        if (source != target) {
          const auto k = [&] {
            Point translation;
            as_vector(&translation) =
                as_vector(&geometry.position_of_vertex(target)) -
                as_vector(&geometry.position_of_vertex(source));
            return PETSC_i *
                   as_vector(&wave_vector).dot(as_vector(&translation));
          }();
          for (Mesh::size_type dof = 0; dof < vertex.numberOfDofs(); dof++) {
            boundary_conditions.push_back(
                {{target, dof}, {{std::exp(k), {source, dof}}}, 0.});
          }
        }
      }
      return solve::boundaryConditionsToTransform(boundary_conditions, mesh)
          .matrix;
    }(wave_vector);

    auto Tt = cpppetsc::asTransposedMatrix(&T);
    const auto KT = cpppetsc::asTransformedMatrix(&K, &Tt);
    const auto MT = cpppetsc::asTransformedMatrix(&M, &Tt);

    const auto result = cppslepc::computeGeneralizedEigenvaluesWithSINVERT(
        cpppetsc::computeElementsOfMatrix(KT),
        cpppetsc::computeElementsOfMatrix(MT), number_of_eigenfrequencies);

    PetscFPrintf(Policy::communicator(), out, "%f,%f,%f", wave_vector[0],
                 wave_vector[1], wave_vector[2]);
    for (std::size_t n = 0; n < number_of_eigenfrequencies; n++)
      PetscFPrintf(Policy::communicator(), out, ",%f",
                   std::sqrt(result[n]).real() / (2 * M_PI));
    PetscFPrintf(Policy::communicator(), out, "\n");
  }
  fKpath.close();
  fclose(out);
Bastian Telgen's avatar
Bastian Telgen committed
220
  PetscPrintf(Policy::communicator(), "Everything is finished\n");
Bastian Telgen's avatar
Bastian Telgen committed
221
}