-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllg_problem.h
1408 lines (1129 loc) · 45.1 KB
/
llg_problem.h
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
78
79
80
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
161
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef OOMPH_IMPLICIT_LLG_EXCHANGE_PROBLEM_H
#define OOMPH_IMPLICIT_LLG_EXCHANGE_PROBLEM_H
// General includes
// oomph-lib includes
// Micromagnetics includes
#include "micromagnetics_element.h"
#include "vector_helpers.h"
#include "magnetics_helpers.h"
#include "my_generic_problem.h"
#include "mallinson_solution.h"
#include "residual_calculator.h"
#include "boundary_element_handler.h"
#include "micromagnetics_flux_element.h"
#include "generic_poisson_problem.h"
#include "micromag_types.h"
#include "renormalisation_handler.h"
namespace oomph
{
class ResidualCalculator;
/// Enumeration for how to handle phi_1's singularity
namespace phi_1_singularity_handling
{
enum phi_1_singularity_handling {pin_any, pin_bulk, pin_boundary,
normalise,
nothing,
jacobian};
}
// ============================================================
///
// ============================================================
class LLGProblem : public MyProblem
{
public:
/// Default constructor - do nothing except nulling pointers.
LLGProblem() :
Compare_with_mallinson(false),
Compare_with_mallinson_m(false),
Previous_energies(5, 0.0)
{
Boundary_solution_pt = 0;
// Needed for if we want to switch solvers in runs
Super_LU_solver_pt = new SuperLUSolver;
// Storage for residual calculator
Residual_calculator_pt = 0;
renormalisation_handler_pt = 0;
// Initialise storage for energies etc.
Exchange_energy = MyProblem::Dummy_doc_data;
Zeeman_energy = MyProblem::Dummy_doc_data;
Crystalline_anisotropy_energy = MyProblem::Dummy_doc_data;
Magnetostatic_energy = MyProblem::Dummy_doc_data;
Abs_damping_error = MyProblem::Dummy_doc_data;
Rel_damping_error = MyProblem::Dummy_doc_data;
Initial_energy = MyProblem::Dummy_doc_data;
// Bem stuff
Bem_handler_pt = 0;
Flux_mesh_pt = 0;
Flux_mesh_factory_pt = 0;
Phi_1_singularity_method = phi_1_singularity_handling::pin_any;
H_app_relax = 0;
Decoupled_ms = false;
Extrapolate_decoupled_ms = false;
Disable_ms = false;
Analytic_ms_fct_pt = 0;
Inside_explicit_timestep = false;
Inside_segregated_magnetostatics = false;
#ifdef PARANOID
Check_angles = true;
#else
Check_angles = false;
#endif
Disable_magnetostatic_solver_optimistations = false;
Force_gaussian_quadrature_in_energy = false;
// Debugging switches
Pin_boundary_m = false;
Use_fd_jacobian = false;
Compare_with_gauss_quadrature = false;
}
/// Virtual destructor. Policy decision: my problem classes won't call
/// delete on anything. That's up to the driver code or
/// whatever. Ideally we should just start using c++11 smart pointers
/// and not have to worry so much about memory management!
virtual ~LLGProblem() {}
std::string problem_name() const {return "LLG";}
/// Get the jacobian as a SumOfMatrices. This is probably the best way
/// to deal with Jacobians involving a dense block (i.e. in fully
/// implicit bem). If we aren't using that then this is basically the
/// same as using the cr matrix form but with the Jacobian wrapped
/// inside a SumOfMatrices class.
void get_jacobian(DoubleVector &residuals, SumOfMatrices &jacobian)
{
// Get the fem Jacobian and the residuals
CRDoubleMatrix* fem_jacobian_pt =
new CRDoubleMatrix(residuals.distribution_pt());
Problem::get_jacobian(residuals, *fem_jacobian_pt);
// maybe modify a phi_1 jacobian entry to remove singularity
maybe_twiddle_phi_1_jacobian_entry(*fem_jacobian_pt);
// Assign the fem jacobian to be the "main" sumofmatrices matrix. Do
// delete it when done.
jacobian.main_matrix_pt() = fem_jacobian_pt;
jacobian.set_delete_main_matrix();
// If we're doing bem here then add on the bem matrix and the
// d(phibound)/d(phibound) block identity matrix.
if(implicit_ms_flag())
{
// Add the bem matrix to the jacobian in the right places. Don't
// delete it when done.
jacobian.add_matrix(bem_handler_pt()->bem_matrix_pt(),
bem_handler_pt()->row_lookup_pt(),
bem_handler_pt()->col_lookup_pt(),
false);
// Create identity CRDoubleMatrix
unsigned bem_nnode = bem_handler_pt()->bem_mesh_pt()->nnode();
unsigned nrow = fem_jacobian_pt->nrow();
LinearAlgebraDistribution* dist_pt = fem_jacobian_pt->distribution_pt();
Vector<double> values(bem_nnode, -1.0);
Vector<int> row_index(bem_nnode), row_start;
for(unsigned nd=0; nd<bem_nnode; nd++)
{
unsigned i = bem_handler_pt()->output_lookup_pt()
->added_to_main(nd);
row_index[nd] = i;
}
// Sort the row index list then create row starts vector. DO NOT
// COPY THIS CODE FOR CREATION OF OTHER MATRICES: sorting is only
// safe because all the values are the same.
std::sort(row_index.begin(), row_index.end());
VectorOps::rowindex2rowstart(row_index, nrow, row_start);
// Create the matrix (col_index == row index since on diagonal)
CRDoubleMatrix bem_block_identity(dist_pt, nrow, values,
row_index, row_start);
// Add it on
VectorOps::cr_matrix_add(*fem_jacobian_pt, bem_block_identity,
*fem_jacobian_pt);
}
}
void maybe_twiddle_phi_1_jacobian_entry(CRDoubleMatrix &jacobian) const
{
if(twiddle_phi_1_jacobian())
{
// Find a phi 1 equation
int phi_1_dof = mesh_pt()->node_pt(0)->eqn_number(phi_1_index());
// If not pinnned (phi1 is either all pinned or not pinned at all,
// which is why we have this problem in the first place).
if(phi_1_dof >= 0)
{
// Then add some small amount to the Jacobian to make it
// non-singular, writing to CR matrices is hard...
bool success = false;
for(long k=jacobian.row_start()[phi_1_dof];
k<jacobian.row_start()[phi_1_dof+1];
k++)
{
if(jacobian.column_index()[k] == phi_1_dof)
{
jacobian.value()[k] += 1e-3;
success = true;
break;
}
}
if(!success)
{
std::string err = "Failed to modify phi1 jacobian entry.";
throw OomphLibError(err, OOMPH_CURRENT_FUNCTION,
OOMPH_EXCEPTION_LOCATION);
}
}
}
}
void output_solution(const unsigned& t, std::ostream& outstream,
const unsigned& npoints=2) const
{
MyProblem::output_solution(t, outstream, npoints);
// Output error in |m| if requested.
// ??ds refactor to merge with other addtional doc stuff somehow?
if(Doc_m_length_error)
{
const std::string dir = Doc_info.directory();
const std::string num = to_string(Doc_info.number());
std::ofstream ml_file((dir + "/ml_error" + num + ".csv").c_str(),
std::ios::out);
doc_m_length_error(t, ml_file);
ml_file.close();
}
}
/// Get the Jacobian as a CRDoubleMatrix (the normal matrix format). If
/// we are using fully implicit bem then this is not a good idea for
/// "real" problems but useful for tests. Otherwise this is exactly the
/// same as Problem::get_jacobian().
void get_jacobian(DoubleVector &residuals, CRDoubleMatrix &jacobian)
{
// If we're calculating ms here then include the bem matrix. Warning:
// this is not going to be fast! Use the sumofmatrices version
// instead if possible.
if(implicit_ms_flag())
{
Vector<double> sum_values;
Vector<int> sum_rows, sum_cols, sum_row_start;
unsigned ncol = 0;
LinearAlgebraDistribution dist;
// These braces make sure that the fem jacobian is destroyed asap
// to reduce memory usage.
{
// Get as a sum of matrices jacobian
SumOfMatrices sum_jacobian;
get_jacobian(residuals, sum_jacobian);
// Copy out the data we need
ncol = sum_jacobian.ncol();
dist = *(checked_dynamic_cast<CRDoubleMatrix*>
(sum_jacobian.main_matrix_pt())->distribution_pt());
// // Convert to indicies ??ds SLOW (N^2)
// sum_jacobian.get_as_indices(sum_cols, sum_rows, sum_values);
VectorOps::get_as_indicies(sum_jacobian, sum_values, sum_cols,
sum_rows);
} // sum_jacobian destroyed -> fem_jacobian destroyed, but
// information we need is still in the vectors.
// Convert to rowstart form and make a cr matrix out of it
VectorOps::rowindex2rowstart(sum_rows, ncol, sum_row_start);
jacobian.build(&dist, ncol, sum_values, sum_cols, sum_row_start);
}
// Otherwise just do it as normal
else
{
Problem::get_jacobian(residuals, jacobian);
}
// maybe modify a phi_1 jacobian entry to remove singularity
maybe_twiddle_phi_1_jacobian_entry(jacobian);
}
/// Overload to set flag to avoid BEM interfering with get mass matrix
/// and to swap to ll form of residual if needed.
void get_dvaluesdt(DoubleVector& f)
{
Inside_explicit_timestep = true;
bool needs_reset = false;
if(Residual_calculator_pt->use_gilbert_form())
{
Residual_calculator_pt->set_use_ll_form();
needs_reset = true;
}
MyProblem::get_dvaluesdt(f);
if(needs_reset)
{
Residual_calculator_pt->set_use_gilbert_form();
}
Inside_explicit_timestep = false;
}
/// Function that does the real work of the constructors.
void build(Vector<Mesh*>& bulk_mesh_pts);
/// Call the renormalisation handler to do whatever it does
void renormalise_magnetisation()
{
renormalisation_handler_pt->renormalise(this);
}
/// Relax magnetisation using strongly damped llg until torque is small
void relax(HAppFctPt h_app_relax, const double& relax_time)
{
// Backup values
// ============================================================
const double initial_damping = mag_parameters_pt()->Gilbert_damping;
const double initial_dt = time_pt()->dt();
const double initial_time = time_pt()->time();
const HAppFctPt initial_happ = mag_parameters_pt()->Applied_field_fct_pt;
const unsigned old_doc_info_number = Doc_info.number();
// Relax magnetisation
// ============================================================
// Set a high damping and the relaxation field
Magnetic_parameters_pt->Gilbert_damping = 1.0;
Magnetic_parameters_pt->Applied_field_fct_pt = h_app_relax;
// Initialise loop variables
double dt = std::max(initial_dt/100, 1e-4);
double relax_tol = 1e-5;
// Doc the initial condition
Doc_info.number() = 0;
doc_solution(0, "relax_");
// ??ds big hack here: just relax for 250 time units, enough for
// mumag4... fix to use torque eventually
while(time() < relax_time)
{
// Output some basic info
oomph_info
<< std::endl
<< std::endl
<< "Relaxation step " << N_steps_taken
<< ", time = " << time()
<< ", dt = " << dt << std::endl
<< "=============================================" << std::endl
<< std::endl;
// Do the newton solve (different ones depending flags set)
dt = smart_time_step(dt, relax_tol);
// Output
doc_solution(0, "relax_");
}
// Revert everything
// ============================================================
// Revert time info
N_steps_taken = 0;
time_pt()->time() = initial_time;
initialise_dt(initial_dt);
// Revert the damping and field
Magnetic_parameters_pt->Gilbert_damping = initial_damping;
Magnetic_parameters_pt->Applied_field_fct_pt = initial_happ;
// Reset the doc solution counter
Doc_info.number() = old_doc_info_number;
// (Re)-set initial condition to be the relaxed values all the way
// back in time. This must go after reverting other things so that
// intial energy etc. are correct
set_up_impulsive_initial_condition();
// overwrite the "current" soln.dat etc. (ie write out final relaxed
// state as initial condition for time integration.
Doc_info.number()--;
doc_solution();
}
/// ??ds
void proper_relax(HAppFctPt h_app_max,
const double& torque_tol,
const double& h_app_step)
{
// Backup values
// ============================================================
const double initial_damping = mag_parameters_pt()->Gilbert_damping;
const double initial_dt = time_pt()->dt();
const double initial_time = time_pt()->time();
const HAppFctPt initial_happ = mag_parameters_pt()->Applied_field_fct_pt;
const unsigned old_doc_info_number = Doc_info.number();
const double old_happ_debug_coeff = Magnetic_parameters_pt->Applied_field_debug_coeff;
// Set relaxation parameters
// ============================================================
// Set a high damping and the relaxation field
Magnetic_parameters_pt->Gilbert_damping = 1.0;
Magnetic_parameters_pt->Applied_field_fct_pt = h_app_max;
Magnetic_parameters_pt->Applied_field_debug_coeff = 1.0;
// Doc the initial condition
Doc_info.number() = 0;
doc_solution(0, "relax_");
// Step down applied field and relax each time. Use integer
// arithmetic to avoid floating point issues (using floating point
// steps will result the final step being slightly below zero, which
// ruins everything).
const int nsteps = 100;
for(int step=nsteps; step >= 0; step--)
{
double dt = std::max(initial_dt/100, 1e-4);
const double relax_tol = 1e-5;
unsigned i = 0;
double maxtorque = this->max_torque();
Magnetic_parameters_pt->Applied_field_debug_coeff = double(step)/double(nsteps);
while(std::abs(maxtorque) > torque_tol || i < 5)
{
// Output some basic info
oomph_info
<< std::endl
<< std::endl
<< "Relaxation step " << N_steps_taken
<< ", time = " << time()
<< ", dt = " << dt
<< ", maxtorque = " << maxtorque
<< ", i = " << i
<< ", field coeff = " << Magnetic_parameters_pt->Applied_field_debug_coeff
<< std::endl
<< "=============================================" << std::endl
<< std::endl;
// Do the time step
dt = smart_time_step(dt, relax_tol);
// Output
doc_solution(0, "relax_");
maxtorque = this->max_torque();
i++;
}
}
// Revert everything
// ============================================================
// Revert time info
N_steps_taken = 0;
time_pt()->time() = initial_time;
initialise_dt(initial_dt);
// Revert the damping and field
Magnetic_parameters_pt->Gilbert_damping = initial_damping;
Magnetic_parameters_pt->Applied_field_fct_pt = initial_happ;
Magnetic_parameters_pt->Applied_field_debug_coeff = old_happ_debug_coeff;
// Reset the doc solution counter
Doc_info.number() = old_doc_info_number;
// (Re)-set initial condition to be the relaxed values all the way
// back in time. This must go after reverting other things so that
// intial energy etc. are correct
set_up_impulsive_initial_condition();
// overwrite the "current" soln.dat etc. (ie write out final relaxed
// state as initial condition for time integration.
Doc_info.number()--;
doc_solution();
}
double max_torque() const;
virtual void actions_before_time_integration()
{
MyProblem::actions_before_time_integration();
if(H_app_relax != 0)
{
if(Do_proper_relax)
{
proper_relax(H_app_relax, 1e-5, 1e-2);
}
else
{
relax(H_app_relax, Relax_m_time);
}
}
}
virtual void actions_before_implicit_timestep()
{
MyProblem::actions_before_implicit_timestep();
// Project phi to correct time if necessary
if(Decoupled_ms)
{
if(Extrapolate_decoupled_ms)
{
extrapolate_phi(time_stepper_pt()->time_pt()->dt(),
time_stepper_pt()->time_pt()->dt(1));
}
}
// If we are using Dirichlet boundary conditions then update them.
if(Pin_boundary_m)
{
update_dirichlet_conditions();
}
}
void update_dirichlet_conditions()
{
const double t = time_pt()->time();
const unsigned n_msh = nsub_mesh();
for(unsigned msh=0; msh<n_msh; msh++)
{
Mesh* mesh_pt = this->mesh_pt(msh);
const unsigned n_boundary = mesh_pt->nboundary();
for(unsigned b=0; b<n_boundary; b++)
{
const unsigned n_boundary_node = mesh_pt->nboundary_node(b);
for(unsigned nd=0; nd<n_boundary_node; nd++)
{
Node* nd_pt = mesh_pt->boundary_node_pt(b, nd);
// Get position
Vector<double> x(dim(), 0.0); nd_pt->position(x);
// Calculate m
Vector<double> m = boundary_solution(t, x);
// Write to node data
for(unsigned j=0; j<3; j++)
{
unsigned i = m_index(j);
#ifdef PARANOID
if(!nd_pt->is_pinned(i))
{
std::string err
= "Boundary m value not pinned but tried to set condition";
throw OomphLibError(err, OOMPH_EXCEPTION_LOCATION,
OOMPH_CURRENT_FUNCTION);
}
#endif
nd_pt->set_value(i, m[i]);
}
}
}
}
}
Vector<double> boundary_solution(const double& t, Vector<double>& x) const
{
#ifdef PARANOID
if(Boundary_solution_pt == 0)
{
std::string err = "Boundary_solution_pt is null!";
throw OomphLibError(err, OOMPH_EXCEPTION_LOCATION,
OOMPH_CURRENT_FUNCTION);
}
#endif
return (*Boundary_solution_pt)(t, x);
}
virtual void actions_after_implicit_timestep_and_error_estimation()
{
// Might need to renormalise to keep |M| = 1. This needs to go here
// and not in actions_after_newton_ solve so that it doesn't
// interfere with the adaptivity or the bdf-imr update.
renormalise_magnetisation();
}
virtual void actions_after_implicit_timestep()
{
MyProblem::actions_after_implicit_timestep();
if(Decoupled_ms)
{
// Unpin phis (from m solve)
undo_segregated_pinning();
// Solve for the magnetostatic field.
magnetostatics_solve();
}
// check neighbouring magnetisation angles if requested
maybe_check_angles();
// Calculate and store the energy (ready to be output)
calculate_energies();
}
virtual void actions_before_newton_step()
{
oomph_info << std::endl
<< "Newton step " << Nnewton_iter_taken + 1 << std::endl
<< "---------------------------------------" << std::endl;
if(!Inside_segregated_magnetostatics)
{
// Call base class version
MyProblem::actions_before_newton_step();
}
}
virtual void actions_after_newton_step()
{
if(!Inside_segregated_magnetostatics)
{
// Call base class version
MyProblem::actions_after_newton_step();
if(Compare_with_gauss_quadrature)
{
compare_with_gauss_quadrature();
}
}
}
virtual void actions_before_newton_solve()
{
if(!Inside_segregated_magnetostatics)
{
// Call base class version
MyProblem::actions_before_newton_solve();
// Update BEM magnetostatics boundary conditions (if we are doing
// them fully implicitly).
if(implicit_ms_flag())
{
bem_handler_pt()->get_bem_values_and_copy_into_values();
}
// For decoupled fem/bem solves pin everything but m
else if(fembem_ms_flag())
{
check_not_segregated(OOMPH_CURRENT_FUNCTION);
Vector<unsigned> non_m_indices;
non_m_indices.push_back(phi_1_index());
non_m_indices.push_back(phi_index());
segregated_pin_indices(non_m_indices);
}
if(Compare_with_gauss_quadrature)
{
compare_with_gauss_quadrature();
}
}
}
virtual void actions_before_explicit_timestep()
{
// Set this variable to avoid getting BEM in mass matrix (due to a
// hack in oomph core.. fix that instead?)
Inside_explicit_timestep = true;
// Check we're not inside a segregated solve already
check_not_segregated(OOMPH_CURRENT_FUNCTION);
MyProblem::actions_before_explicit_timestep();
}
virtual void actions_after_explicit_timestep()
{
MyProblem::actions_after_explicit_timestep();
// We need to keep M normalised...
renormalise_magnetisation();
// check neighbouring magnetisation angles if requested
maybe_check_angles();
// Explicit timestep is over now
Inside_explicit_timestep = false;
}
virtual void actions_before_explicit_stage()
{
MyProblem::actions_before_explicit_stage();
// Check we're not inside a segregated solve already
check_not_segregated(OOMPH_CURRENT_FUNCTION);
// Segregated-pin phi values ready for m step
Vector<unsigned> non_m_indices;
non_m_indices.push_back(phi_1_index());
non_m_indices.push_back(phi_index());
segregated_pin_indices(non_m_indices);
}
virtual void actions_after_explicit_stage()
{
// Remove m-only segregation
undo_segregated_pinning();
// Solve for the new magnetostatic field.
magnetostatics_solve();
MyProblem::actions_after_explicit_stage();
}
virtual void actions_after_newton_solve()
{
oomph_info << std::endl
<< "Finalising" << std::endl
<< "-------------------------------------------" << std::endl;
if(!Inside_segregated_magnetostatics)
{
MyProblem::actions_after_newton_solve();
}
}
void maybe_check_angles()
{
if(Check_angles)
{
// From the nmag user manual:
// [Begin quote M Donahue]
// * if the spin angle is approaching 180 degrees,
// then the results are completely bogus.
// * over 90 degrees the results are highly questionable.
// * Under 30 degrees the results are probably reliable.
// [end quote]
// (the spin angle is the angle between two neighbouring magnetisations).
// Check this:
Vector<double> a = elemental_max_m_angle_variations();
double max_angle_var = *std::max_element(a.begin(), a.end());
if(max_angle_var > MathematicalConstants::Pi/6)
{
std::string error_msg
= "Large angle variations of " + to_string(max_angle_var)
+ " > " + to_string(MathematicalConstants::Pi/6)
+ " across a single element,\n";
error_msg += "this often means that your mesh is not sufficiently refined.";
throw OomphLibError(error_msg, OOMPH_CURRENT_FUNCTION,
OOMPH_EXCEPTION_LOCATION);
}
}
}
/// Calculate residuals using Gaussian quadrature so we can compare
/// values. Not const because we have to swap out the quadrature
/// pointers.
void compare_with_gauss_quadrature()
{
const unsigned n_ele = mesh_pt()->nelement();
Vector<Integral*> backup_q_pt(n_ele);
std::auto_ptr<Integral> gauss_pt
(Factories::gauss_integration_factory
(ele_pt()->dim(), ele_pt()->nnode_1d(), ele_pt()->element_geometry()));
// switch integration schemes
for(unsigned ele=0; ele<n_ele; ele++)
{
FiniteElement* ele_pt = mesh_pt()->finite_element_pt(ele);
backup_q_pt[ele] = ele_pt->integral_pt();
ele_pt->set_integration_scheme(gauss_pt.get());
}
DoubleVector residuals;
get_residuals(residuals);
// revert integration schemes
for(unsigned ele=0; ele<n_ele; ele++)
{
FiniteElement* ele_pt = mesh_pt()->finite_element_pt(ele);
ele_pt->set_integration_scheme(backup_q_pt[ele]);
}
std::cout << "***" << residuals.max() << std::endl;
}
virtual void actions_before_newton_convergence_check()
{
// Call base class actions function
MyProblem::actions_before_newton_convergence_check();
// Update BEM magnetostatics boundary conditions (if we are doing them
// fully implicitly).
if(implicit_ms_flag())
{
bem_handler_pt()->get_bem_values_and_copy_into_values();
}
// Maybe subtract largest phi value from all phi values to get it
// O(1) (technically it has an arbitrary added constant).
if(normalise_phi_1())
{
const unsigned n_node = mesh_pt()->nnode();
const unsigned index = phi_1_index();
// Find the min phi1 value
double min_phi_1 = mesh_pt()->node_pt(0)->value(index);
for(unsigned nd=1; nd<n_node; nd++)
{
double this_phi_1 = mesh_pt()->node_pt(nd)
->value(index);
if(this_phi_1 < min_phi_1)
{
min_phi_1 = this_phi_1;
}
}
std::cout << "first min phi " << min_phi_1 << std::endl;
// subtract it from each
for(unsigned nd=0; nd<n_node; nd++)
{
double this_phi_1 = mesh_pt()->node_pt(nd)->value(index);
mesh_pt()->node_pt(0)->set_value(index,
this_phi_1 - min_phi_1);
}
// Find the max phi1 value
double new_max_phi_1 = mesh_pt()->node_pt(0)->value(index);
for(unsigned nd=1; nd<n_node; nd++)
{
double this_phi_1 = mesh_pt()->node_pt(nd)
->value(index);
if(this_phi_1 > new_max_phi_1)
{
new_max_phi_1 = this_phi_1;
}
}
std::cout << new_max_phi_1 << std::endl;
}
}
void write_additional_trace_headers(std::ofstream& trace_file) const
{
trace_file
<< Trace_seperator << "m_length_error_means"
<< Trace_seperator << "m_length_error_std_devs"
<< Trace_seperator << "max_angle_errors"
<< Trace_seperator << "mean_mxs"
<< Trace_seperator << "mean_mys"
<< Trace_seperator << "mean_mzs"
<< Trace_seperator << "exchange_energy"
<< Trace_seperator << "zeeman_energy"
<< Trace_seperator << "crystalline_anisotropy_energy"
<< Trace_seperator << "magnetostatic_energy"
<< Trace_seperator << "total_energy"
<< Trace_seperator << "abs_damping_error"
<< Trace_seperator << "rel_damping_error"
<< Trace_seperator << "h_applied_first_element"
<< Trace_seperator << "m_length_error_maxes"
<< Trace_seperator << "energy_change"
<< Trace_seperator << "aux_error_norms"
;
}
void write_additional_trace_data(const unsigned& t_hist,
std::ofstream& trace_file) const
{
Vector<Vector<double> > ms = MManipulation::nodal_magnetisations(t_hist,
*this);
Vector<double> ml_errors = MManipulation::nodal_m_length_errors(ms);
Vector<double> angle_variations = elemental_max_m_angle_variations(t_hist);
Vector<double> mean_m = MManipulation::mean_nodal_magnetisation(ms);
Vector<double> h_app = first_element_happ(t_hist);
// Energies only available for present time
double exchange_energy = Dummy_doc_data, zeeman_energy = Dummy_doc_data,
crystalline_anisotropy_energy = Dummy_doc_data,
magnetostatic_energy = Dummy_doc_data,
micromagnetic_energy = Dummy_doc_data,
abs_damping_error = Dummy_doc_data, rel_damping_error = Dummy_doc_data,
energy_change = Dummy_doc_data
;
Vector<double> aux_error_norms(0);
if(t_hist == 0)
{
exchange_energy = Exchange_energy;
zeeman_energy = Zeeman_energy;
crystalline_anisotropy_energy = Crystalline_anisotropy_energy;
magnetostatic_energy = Magnetostatic_energy;
micromagnetic_energy = this->micromagnetic_energy();
abs_damping_error = Abs_damping_error;
rel_damping_error = Rel_damping_error;
energy_change = micromagnetic_energy - Initial_energy;
}
if(dynamic_cast<InitialM::LLGWaveSolution*>(Exact_solution_pt) != 0)
{
aux_error_norms.push_back(ErrorNorms::wave_phase_error_norm(*this));
aux_error_norms.push_back(ErrorNorms::wave_mz_error_norm(*this));
}
trace_file
<< Trace_seperator << VectorOps::mean(ml_errors)
<< Trace_seperator << VectorOps::stddev(ml_errors)
<< Trace_seperator
<< *std::max_element(angle_variations.begin(), angle_variations.end())
<< Trace_seperator << mean_m[0]
<< Trace_seperator << mean_m[1]
<< Trace_seperator << mean_m[2]
<< Trace_seperator << exchange_energy
<< Trace_seperator << zeeman_energy
<< Trace_seperator << crystalline_anisotropy_energy
<< Trace_seperator << magnetostatic_energy
<< Trace_seperator << micromagnetic_energy
<< Trace_seperator << abs_damping_error
<< Trace_seperator << rel_damping_error
<< Trace_seperator << h_app
<< Trace_seperator << VectorOps::max(ml_errors)
<< Trace_seperator << energy_change
<< Trace_seperator << aux_error_norms
;
}
/// Get happ value from first element
Vector<double> first_element_happ(const unsigned& t_hist=0) const
{
// get time
double t = time_pt()->time(t_hist);
// get a position inside the element
Vector<double> dummy_s(dim(), 0.0), dummy_x(dim());
ele_pt()->interpolated_x(t_hist, dummy_s, dummy_x);
// get the field
return ele_pt()->get_applied_field(t, dummy_x);
}
void initial_doc_additional() const
{
// If we have a H matrix then write out its rank data
if(Bem_handler_pt != 0)
{
bem_handler_pt()->maybe_write_h_matrix_data(Doc_info.directory());
}
}
/// \short Return a vector of the maximum angle variation in each
/// element.
Vector<double> elemental_max_m_angle_variations(const unsigned& t_hist=0) const
{
Vector<double> angles;
for(unsigned msh=0, nmsh=nsub_mesh(); msh<nmsh; msh++)
{
// Skip non-bulk meshes
if((mesh_pt(msh)->nnode() == 0)
|| (mesh_pt(msh)->node_pt(0)->ndim() != Dim)) continue;
for(unsigned e=0, ne=mesh_pt(msh)->nelement(); e < ne; e++)
{
MicromagEquations* ele_pt =
checked_dynamic_cast<MicromagEquations*>
(mesh_pt(msh)->element_pt(e));
angles.push_back(ele_pt->max_m_angle_variation(t_hist));
}
}
return angles;
}
/// \short Error for adaptive timestepper (rms of nodal error determined by
/// comparison with explicit timestepper result).
double global_temporal_error_norm();
virtual void actions_after_set_initial_condition()
{