Debian Codegraph @

packages/nox/examples/lapack/LOCA_Brusselator/BrusselatorHopfContinuation.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // LOCA: Library of Continuation Algorithms Package
4 //
5 // Copyright 2001-2005 NTESS and the LOCA contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "LOCA.H"
11 #include "LOCA_LAPACK.H"
12 #include "BrusselatorProblemInterface.H"
13
14 int main()
15 {
16 double pi = 4.0*atan(1.0);
17 int n = 100;
18 double alpha = 0.25;
19 double D1 = 1.0/40.0;
20 double D2 = 1.0/40.0;
21 double beta = 2.0*pi*pi*D1 + 1.0 + alpha*alpha;
22 int maxNewtonIters = 10;
23
24 try {
25 // Create output file to save solutions
26 std::ofstream outFile("BrusselatorHopfContinuation.dat");
27 outFile.setf(std::ios::scientific, std::ios::floatfield);
28 outFile.precision(14);
29
30 // Save size of discretizations
31 outFile << n << std::endl;
32
33 // Create initial guess for the real and imaginary eigenvectors
34 NOX::LAPACK::Vector y(2*n), z(2*n);
35 double h = 1.0 / double(n-1);
36 double lambda_real = (beta - 1.0 - alpha*alpha)/2.0;
37 double lambda_imag = sqrt(alpha*alpha - lambda_real*lambda_real);
38 double v1_real = -alpha*alpha;
39 double v1_imag = 0.0;
40 double v2_real = beta - 1.0 - lambda_real;
41 double v2_imag = -lambda_imag;
42 double x;
43 for (int i=0; i<n; i++) {
44 x = sin(pi*h*i);
45 y(i) = v1_real*x;
46 z(i) = v1_imag*x;
47
48 y(i+n) = v2_real*x;
49 z(i+n) = v2_imag*x;
50 }
51
52 // Initial guess for frequency (valid for |alpha| > (pi^2)*|D1|)
53 double w = lambda_imag;
54
55 // Create length scaling vector (phi)
56 NOX::LAPACK::Vector phi(2*n);
57 phi.init(1.0);
58
59 Teuchos::RCP<NOX::Abstract::Vector> y_vec =
60 Teuchos::rcp(&y,false);
61 Teuchos::RCP<NOX::Abstract::Vector> z_vec =
62 Teuchos::rcp(&z,false);
63 Teuchos::RCP<NOX::Abstract::Vector> phi_vec =
64 Teuchos::rcp(&phi,false);
65
66 // Create initial values for a and b for minimally augmented method
67 Teuchos::RCP<NOX::Abstract::Vector> a_vec_real =
68 Teuchos::rcp(new NOX::LAPACK::Vector(2*n));
69 Teuchos::RCP<NOX::Abstract::Vector> a_vec_imag =
70 Teuchos::rcp(new NOX::LAPACK::Vector(2*n));
71 Teuchos::RCP<NOX::Abstract::Vector> b_vec_real =
72 Teuchos::rcp(new NOX::LAPACK::Vector(2*n));
73 Teuchos::RCP<NOX::Abstract::Vector> b_vec_imag =
74 Teuchos::rcp(new NOX::LAPACK::Vector(2*n));
75 *a_vec_real = *y_vec;
76 *a_vec_imag = *z_vec;
77 *b_vec_real = *y_vec;
78 *b_vec_imag = *z_vec;
79
80 // Create parameter list
81 Teuchos::RCP<Teuchos::ParameterList> paramList =
82 Teuchos::rcp(new Teuchos::ParameterList);
83
84 // Create LOCA sublist
85 Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA");
86
87 // Create the stepper sublist and set the stepper parameters
88 Teuchos::ParameterList& stepperList = locaParamsList.sublist("Stepper");
89 stepperList.set("Continuation Method", "Arc Length"); // Default
90 //stepperList.set("Continuation Method", "Natural"); // Default
91 stepperList.set("Continuation Parameter", "alpha");
92 stepperList.set("Initial Value", alpha);
93 stepperList.set("Max Value", 1.0);
94 stepperList.set("Min Value", 0.24);
95 stepperList.set("Max Steps", 100);
96 stepperList.set("Max Nonlinear Iterations", maxNewtonIters);
97
98 // Create bifurcation sublist
99 Teuchos::ParameterList& bifurcationList =
100 locaParamsList.sublist("Bifurcation");
101 bifurcationList.set("Type", "Hopf");
102 bifurcationList.set("Bifurcation Parameter", "beta"); // Must set
103 bifurcationList.set("Initial Frequency", w); // Must set
104
105 // // For Moore-Spence Formulation
106 // bifurcationList.set("Formulation", "Moore-Spence"); // Default
107 // bifurcationList.set("Solver Method", "Salinger Bordering"); // Default
108 // bifurcationList.set("Length Normalization Vector", phi_vec); // Must set
109 // bifurcationList.set("Initial Real Eigenvector", y_vec); // Must set
110 // bifurcationList.set("Initial Imaginary Eigenvector", z_vec); // Must set
111
112 // For minimally augmented formulation
113 bifurcationList.set("Formulation", "Minimally Augmented");
114 bifurcationList.set("Initial Real A Vector", a_vec_real); // Must set
115 bifurcationList.set("Initial Imaginary A Vector", a_vec_imag); // Must set
116 bifurcationList.set("Initial Real B Vector", b_vec_real); // Must set
117 bifurcationList.set("Initial Imaginary B Vector", b_vec_imag); // Must set
118 bifurcationList.set("Update Null Vectors Every Continuation Step", true);
119
120 // For minimally augmented method, should set these for good performance
121 // Direct solve of bordered equations
122 bifurcationList.set("Bordered Solver Method", "LAPACK Direct Solve");
123 // Combine arc-length and turning point bordered rows & columns
124 stepperList.set("Bordered Solver Method", "Nested");
125 Teuchos::ParameterList& nestedList =
126 stepperList.sublist("Nested Bordered Solver");
127 // Direct solve of combined bordered system
128 nestedList.set("Bordered Solver Method", "LAPACK Direct Solve");
129
130 // Create predictor sublist
131 Teuchos::ParameterList& predictorList =
132 locaParamsList.sublist("Predictor");
133 predictorList.set("Method", "Secant"); // Default
134
135 // // Should use w/Secant predictor & Moore-Spence formulation
136 // Teuchos::ParameterList& firstStepPredictor
137 // = predictorList.sublist("First Step Predictor");
138 // firstStepPredictor.set("Method", "Random");
139 // firstStepPredictor.set("Epsilon", 1.0e-3);
140
141 // // Should use w/Secant predictor & Moore-Spence fomulation
142 // Teuchos::ParameterList& lastStepPredictor
143 // = predictorList.sublist("Last Step Predictor");
144 // lastStepPredictor.set("Method", "Random");
145 // lastStepPredictor.set("Epsilon", 1.0e-3);
146
147 // Create step size sublist
148 Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size");
149 stepSizeList.set("Method", "Adaptive"); // Default
150 stepSizeList.set("Initial Step Size", 0.02);
151 stepSizeList.set("Min Step Size", 1.0e-3);
152 stepSizeList.set("Max Step Size", 0.1);
153 stepSizeList.set("Aggressiveness", 0.5);
154
155 // Create the "Solver" parameters sublist to be used with NOX Solvers
156 Teuchos::ParameterList& nlParams = paramList->sublist("NOX");
157 Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
158 nlPrintParams.set("Output Precision", 3);
159 nlPrintParams.set("Output Information",
160 NOX::Utils::OuterIteration +
161 NOX::Utils::OuterIterationStatusTest +
162 NOX::Utils::InnerIteration +
163 NOX::Utils::Details +
164 NOX::Utils::Warning +
165 NOX::Utils::StepperIteration +
166 NOX::Utils::StepperDetails +
167 NOX::Utils::StepperParameters);
168
169 // Create the "Line Search" sublist for the "Line Search Based" solver
170 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
171 searchParams.set("Method", "Full Step");
172
173 // Create LAPACK Factory
174 Teuchos::RCP<LOCA::LAPACK::Factory> lapackFactory =
175 Teuchos::rcp(new LOCA::LAPACK::Factory);
176
177 // Create global data object
178 Teuchos::RCP<LOCA::GlobalData> globalData =
179 LOCA::createGlobalData(paramList, lapackFactory);
180
181 // Set up the problem interface
182 BrusselatorProblemInterface brus(globalData, n, alpha, beta, D1, D2,
183 outFile);
184 LOCA::ParameterVector p;
185 p.addParameter("alpha",alpha);
186 p.addParameter("beta",beta);
187 p.addParameter("D1",D1);
188 p.addParameter("D2",D2);
189
190 // Create a group which uses that problem interface. The group will
191 // be initialized to contain the default initial guess for the
192 // specified problem.
193 Teuchos::RCP<LOCA::LAPACK::Group> grp =
194 Teuchos::rcp(new LOCA::LAPACK::Group(globalData, brus));
195
196 grp->setParams(p);
197
198 // Set up the status tests
199 Teuchos::RCP<NOX::StatusTest::NormF> statusTestA =
200 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-10,
201 NOX::StatusTest::NormF::Scaled));
202 Teuchos::RCP<NOX::StatusTest::MaxIters> statusTestB =
203 Teuchos::rcp(new NOX::StatusTest::MaxIters(maxNewtonIters));
204 Teuchos::RCP<NOX::StatusTest::Combo> combo =
205 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR,
206 statusTestA, statusTestB));
207
208 // Create the stepper
209 LOCA::Stepper stepper(globalData, grp, combo, paramList);
210
211 // Solve the nonlinear system
212 LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
213
214 if (status == LOCA::Abstract::Iterator::Finished)
215 std::cout << "All examples passed" << std::endl;
216 else {
217 if (globalData->locaUtils->isPrintType(NOX::Utils::Error))
218 globalData->locaUtils->out()
219 << "Stepper failed to converge!" << std::endl;
220 }
221
222 // Output the parameter list
223 if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
224 globalData->locaUtils->out()
225 << std::endl << "Final Parameters" << std::endl
226 << "****************" << std::endl;
227 stepper.getList()->print(globalData->locaUtils->out());
228 globalData->locaUtils->out() << std::endl;
229 }
230
231 outFile.close();
232
233 LOCA::destroyGlobalData(globalData);
234 }
235
236 catch (std::exception& e) {
237 std::cout << e.what() << std::endl;
238 }
239 catch (const char *s) {
240 std::cout << s << std::endl;
241 }
242 catch (...) {
243 std::cout << "Caught unknown exception!" << std::endl;
244 }
245
246 return 0;
247 }