Skip to content

Commit 2fc7d5e

Browse files
committed
warm lsearch
1 parent 6ea7d49 commit 2fc7d5e

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

include/reader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Reader {
3030
json materialProperties;
3131
double TOL;
3232
json errorParameters;
33+
int ls_max_iter{5};
34+
double ls_tol{1e-2};
35+
bool extrapolate_displacement{true};
3336
json microstructure;
3437
int n_it;
3538
vector<LoadCase> load_cases;

include/solver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,6 @@ void Solver<howmany, n_str>::postprocess(Reader &reader, int load_idx, int time_
702702
}
703703
reader.writeData("homogenized_tangent", load_idx, time_idx, homogenized_tangent.data(), dims, 2);
704704
}
705-
extrapolateDisplacement(); // prepare v_u for next time step
706705
}
707706

708707
template <int howmany, int n_str>

include/solverCG.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SolverCG : public Solver<howmany, n_str> {
2323
RealArray s_real;
2424
RealArray d_real;
2525
RealArray rnew_real;
26+
double alpha_warm{0.1};
2627

2728
void internalSolve();
2829
void LineSearchSecant();
@@ -64,6 +65,7 @@ void SolverCG<howmany, n_str>::internalSolve()
6465
printf("\n# Start FANS - Conjugate Gradient Solver \n");
6566

6667
bool islinear = this->matmanager->all_linear;
68+
alpha_warm = 0.1;
6769

6870
s_real.setZero();
6971
d_real.setZero();
@@ -118,11 +120,11 @@ template <int howmany, int n_str>
118120
void SolverCG<howmany, n_str>::LineSearchSecant()
119121
{
120122
double err = 10.0;
121-
const int MaxIter = 5;
122-
const double tol = 1e-2;
123+
const int MaxIter = this->reader.ls_max_iter;
124+
const double tol = this->reader.ls_tol;
123125
int _iter = 0;
124126
double alpha_prev = 0.0;
125-
double alpha_curr = 0.1;
127+
double alpha_curr = alpha_warm;
126128

127129
double rpd = dotProduct(v_r_real, d_real);
128130
v_u_real += d_real * alpha_curr;
@@ -151,7 +153,8 @@ void SolverCG<howmany, n_str>::LineSearchSecant()
151153
this->template compute_residual<0>(rnew_real, v_u_real);
152154
r1pd = dotProduct(rnew_real, d_real);
153155
}
154-
v_r_real = rnew_real;
156+
alpha_warm = (_iter == MaxIter && err > tol) ? 0.1 : alpha_curr;
157+
v_r_real = rnew_real;
155158
if (this->world_rank == 0)
156159
printf("line search iter %i, alpha %f - error %e - ", _iter, alpha_curr, err);
157160
}

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void runSolver(Reader &reader)
3434
}
3535
solver->solve();
3636
solver->postprocess(reader, load_path_idx, time_step_idx);
37+
if (reader.extrapolate_displacement)
38+
solver->extrapolateDisplacement();
3739
}
3840
delete solver;
3941
delete matmanager;

src/reader.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,11 @@ void Reader ::ReadInputFile(char input_fn[])
7575
microstructure = j["microstructure"];
7676
std::snprintf(ms_filename, sizeof(ms_filename), "%s", microstructure["filepath"].get<std::string>().c_str());
7777
// dataset name handling
78-
std::string tmp_str = microstructure["datasetname"].get<std::string>();
79-
if (tmp_str.empty()) {
80-
throw std::invalid_argument(
81-
"datasetname must not be empty and must refer to a valid HDF5 path");
82-
}
83-
// Ensure absolute HDF5 path, leeading slash
84-
if (tmp_str.front() != '/') {
85-
tmp_str.insert(tmp_str.begin(), '/');
86-
}
87-
std::snprintf(ms_datasetname, sizeof(ms_datasetname), "%s", tmp_str.c_str());
78+
const auto tmp_str = microstructure["datasetname"].get<std::string>();
79+
if (tmp_str.empty())
80+
throw std::invalid_argument("datasetname must not be empty and must refer to a valid HDF5 path");
81+
// Ensure absolute HDF5 path, leading slash
82+
std::snprintf(ms_datasetname, sizeof(ms_datasetname), "%s%s", tmp_str.front() == '/' ? "" : "/", tmp_str.c_str());
8883
L = microstructure["L"].get<vector<double>>();
8984

9085
if (j.contains("results_prefix")) {
@@ -100,6 +95,15 @@ void Reader ::ReadInputFile(char input_fn[])
10095
TOL = errorParameters["tolerance"].get<double>();
10196
n_it = j["n_it"].get<int>();
10297

98+
extrapolate_displacement = j.value("extrapolate_displacement", extrapolate_displacement);
99+
100+
if (j.contains("linesearch_parameters")) {
101+
ls_max_iter = j["linesearch_parameters"].value("max_iter", ls_max_iter);
102+
ls_tol = j["linesearch_parameters"].value("tol", ls_tol);
103+
if (ls_max_iter < 1 || ls_tol <= 0.0)
104+
throw std::invalid_argument("linesearch_parameters: max_iter >= 1 and tol > 0 required");
105+
}
106+
103107
problemType = j["problem_type"].get<string>();
104108
method = j["method"].get<string>();
105109

@@ -173,7 +177,7 @@ void Reader ::ReadInputFile(char input_fn[])
173177
}
174178

175179
} catch (const std::exception &e) {
176-
fprintf(stderr, "ERROR trying to read input file '%s' for FANS\n", input_fn);
180+
fprintf(stderr, "ERROR trying to read input file '%s' for FANS: %s\n", input_fn, e.what());
177181
exit(10);
178182
}
179183
}

0 commit comments

Comments
 (0)