-
Notifications
You must be signed in to change notification settings - Fork 303
/
WhitneyAVSolver.F90
3933 lines (3274 loc) · 143 KB
/
WhitneyAVSolver.F90
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
!/*****************************************************************************/
! *
! * Elmer, A Finite Element Software for Multiphysical Problems
! *
! * Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland
! *
! * This program is free software; you can redistribute it and/or
! * modify it under the terms of the GNU General Public License
! * as published by the Free Software Foundation; either version 2
! * of the License, or (at your option) any later version.
! *
! * This program is distributed in the hope that it will be useful,
! * but WITHOUT ANY WARRANTY; without even the implied warranty of
! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! * GNU General Public License for more details.
! *
! * You should have received a copy of the GNU General Public License
! * along with this program (in file fem/GPL-2); if not, write to the
! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
! * Boston, MA 02110-1301, USA.
! *
! *****************************************************************************/
!
!/******************************************************************************
! *
! * Authors: Juha Ruokolainen
! * Email: Juha.Ruokolainen@csc.fi
! * Web: http://www.csc.fi/elmer
! * Address: CSC - IT Center for Science Ltd.
! * Keilaranta 14
! * 02101 Espoo, Finland
! *
! * Original Date: 08 Jun 1997
! *
! *****************************************************************************/
!> \ingroup Solvers
!------------------------------------------------------------------------------
SUBROUTINE WhitneyAVSolver_Init0(Model,Solver,dt,Transient)
!------------------------------------------------------------------------------
USE MagnetoDynamicsUtils
IMPLICIT NONE
!------------------------------------------------------------------------------
TYPE(Solver_t) :: Solver
TYPE(Model_t) :: Model
REAL(KIND=dp) :: dt
LOGICAL :: Transient
!------------------------------------------------------------------------------
LOGICAL :: Found, PiolaVersion, SecondOrder, LagrangeGauge, StaticConductivity, &
ElectroDynamics
TYPE(ValueList_t), POINTER :: SolverParams
TYPE(ValueListEntry_t), POINTER :: VariablePtr
INTEGER, PARAMETER :: b_empty = 0, b_Piola = 1, &
b_Secondorder = 2, b_Gauge = 4, b_Transient = 8, b_StaticCond = 16
INTEGER :: Paramlist
CHARACTER(:), ALLOCATABLE :: ElemType
TYPE(Solver_t), POINTER :: Solvers(:)
INTEGER :: i,j,k,n
CHARACTER(:), ALLOCATABLE :: eq
INTEGER, POINTER :: ActiveSolvers(:)
Paramlist = 0
SolverParams => GetSolverParams()
ElectroDynamics = ListGetLogical( SolverParams, 'Electrodynamics model', Found )
IF ( ElectroDynamics) THEN
CALL ListAddInteger( SolverParams, 'Time derivative order', 2)
END IF
StaticConductivity = ListGetLogical( SolverParams,'Static Conductivity',Found )
IF( .NOT. Found ) THEN
IF( ListCheckPrefixAnyBodyForce(Model, "Angular Velocity") .OR. &
ListCheckPrefixAnyBodyForce(Model, "Lorentz Velocity") ) THEN
CALL Info("WhitneyAVSolver_Init0", "Moving material triggers the use of scalar potential",Level=10)
StaticConductivity = .TRUE.
END IF
IF( ListCheckPrefixAnyBC(Model, "Electric Current Density") ) THEN
CALL Info("WhitneyAVSolver_Init0", &
"> Electric Current Density < triggers the use of scalar potential",Level=10)
StaticConductivity = .TRUE.
END IF
END IF
IF (.NOT. Transient .AND. StaticConductivity) THEN
CALL Info("WhitneyAVSolver_Init0",'Including scalar potential in AV equation!',Level=6)
END IF
LagrangeGauge = GetLogical(SolverParams, 'Use Lagrange Gauge', Found)
IF ( .NOT.ListCheckPresent(SolverParams, "Element") ) THEN
PiolaVersion = GetLogical(SolverParams, &
'Use Piola Transform', Found )
SecondOrder = GetLogical(SolverParams, 'Quadratic Approximation', Found)
IF (.NOT. PiolaVersion .AND. SecondOrder) THEN
CALL Warn("WhitneyAVSolver_Init0", &
"Requested Quadratic Approximation without Piola Transform. Setting Use Piola Transform = True.")
PiolaVersion = .TRUE.
CALL ListAddLogical( SolverParams, 'Use Piola Transform', .TRUE. )
END IF
IF (PiolaVersion) Paramlist = Paramlist + b_Piola
IF (SecondOrder) Paramlist = Paramlist + b_Secondorder
IF (LagrangeGauge) Paramlist = Paramlist + b_Gauge
IF (StaticConductivity) Paramlist = Paramlist + b_StaticCond
IF (Transient .OR. ElectroDynamics) Paramlist = Paramlist + b_Transient
SELECT CASE (Paramlist)
CASE (b_Piola + b_Transient + b_Secondorder, &
b_Piola + b_Gauge + b_Secondorder, &
b_Piola + b_Transient + b_Secondorder + b_StaticCond, &
b_Piola + b_Secondorder + b_StaticCond)
ElemType = "n:1 e:2 -brick b:6 -prism b:2 -pyramid b:3 -quad_face b:4 -tri_face b:2"
CASE (b_Piola + b_Transient, &
b_Piola + b_Transient + b_StaticCond, &
b_Piola + b_Transient + b_Gauge)
ElemType = "n:1 e:1 -brick b:3 -quad_face b:2"
CASE (b_Piola + b_Gauge)
ElemType = "n:1 e:1 -brick b:3 -quad_face b:2"
CASE (b_Piola + b_Secondorder)
ElemType = "n:0 e:2 -brick b:6 -pyramid b:3 -prism b:2 -quad_face b:4 -tri_face b:2"
CASE (b_Piola)
ElemType = "n:0 e:1 -brick b:3 -quad_face b:2"
CASE (b_Piola + b_StaticCond )
ElemType = "n:1 e:1 -brick b:3 -quad_face b:2"
CASE (b_Transient, &
b_Transient + b_StaticCond, &
b_StaticCond, &
b_Gauge + b_Transient, &
b_Gauge)
ElemType = "n:1 e:1"
CASE (b_empty)
ElemType = "n:0 e:1"
CASE default
WRITE (Message,*) 'Unsupported degree-gauge-transient combination', Paramlist
CALL Fatal('WhitneyAVSolver_Init0', Message)
END SELECT
CALL Info('WhitneyAVSolver_Init0','Setting element type to: "'//TRIM(ElemType)//'"',Level=6)
CALL ListAddString( SolverParams,'Element',ElemType )
IF( GetString(SolverParams,'Linear System Solver',Found) == 'block' ) THEN
IF ( PiolaVersion ) THEN
CALL Fatal('WhitneyAVSolver_Init0','Block strategy not applicable to piola version!')
ELSE
CALL ListAddLogical( SolverParams, "Optimize Bandwidth", .FALSE.)
END IF
END IF
END IF
IF (.NOT. Transient .AND. .NOT. ( StaticConductivity .OR. LagrangeGauge ) ) THEN
CALL ListAddNewLogical( SolverParams,'Variable Output',.FALSE.)
END IF
CALL ListAddLogical( SolverParams,'Use Global Mass Matrix',.TRUE.)
! This is for internal communication with the saving routines
CALL ListAddLogical( SolverParams,'Hcurl Basis',.TRUE.)
CALL ListAddNewString( SolverParams,'Variable','AV')
BLOCK
LOGICAL :: SRDecomposition
SRDecomposition = GetLogical( SolverParams, 'Source-Reaction Decomposition', Found )
IF (Found .AND. SRDecomposition) THEN
CALL ListAddNewString( SolverParams,'Exported Variable 1','-ip -dofs 3 Src Vector Potential')
CALL ListAddNewString( SolverParams,'Exported Variable 2','-ip -dofs 3 Src Magnetic Flux Density')
!CALL ListAddNewString( SolverParams,'Exported Variable 1','Src Potential')
END IF
END BLOCK
IF (LagrangeGauge .AND. Transient .AND. &
ListCheckPrefixAnyBC( Model, "Mortar BC" ) ) THEN
CALL Info("WhitneyAVSolver_Init0", "Gauge field is not projected across mortar boundaries.")
END IF
! THIS ENFORCES THE NEW STRATEGY !!!!
CALL ListAddLogical( SolverParams,'Generic Source Fixing',.TRUE.)
! Add solvers, if "Helmholtz projection" requested
IF (ListGetLogical(SolverParams, 'Helmholtz Projection', Found)) THEN
Solvers => Model % Solvers
n = Model % NumberOfSolvers
Model % NumberOfSolvers = n+2
ALLOCATE(Model % Solvers(Model % NumberOfSolvers))
Model % Solvers(1:n) = Solvers
DO i=n+1,n+2
Model % Solvers(i) % PROCEDURE = 0
Model % Solvers(i) % Matrix => Null()
Model % Solvers(i) % Mesh => Null()
Model % Solvers(i) % Variable => Null()
Model % Solvers(i) % ActiveElements => Null()
Model % Solvers(i) % NumberOfActiveElements = 0
END DO
DO i=1,Model % NumberOfSolvers
IF(.NOT.ASSOCIATED(Model % Solvers(i) % Values)) &
Model % Solvers(i) % Values => ListAllocate()
END DO
Eq = ListGetString( SolverParams, 'Equation' )
CALL ListAddIntegerArray( SolverParams, 'Post Solvers', 2, [n+1,n+2] )
CALL ListAddString( Model % Solvers(n+1) % Values, 'Procedure', &
'MagnetoDynamics HelmholtzProjectorT', CaseConversion=.FALSE. )
CALL ListAddString( Model % Solvers(n+1) % Values, 'Equation', 'HP' )
CALL ListAddString( Model % Solvers(n+1) % Values, 'Exec Solver', 'Never' )
CALL ListAddString( Model % Solvers(n+2) % Values, 'Procedure', &
'MagnetoDynamics RemoveKernelComponentT',CaseConversion=.FALSE. )
CALL ListAddString( Model % Solvers(n+2) % Values, 'Equation', 'RMC' )
CALL ListAddString( Model % Solvers(n+2) % Values, 'Exec Solver', 'Never' )
DO i=1,Model % NumberOFEquations
IF ( ListGetLogical( Model % Equations(i) % Values, Eq, Found ) ) THEN
CALL ListAddLogical( Model % Equations(i) % Values, 'HP', .TRUE. )
CALL ListAddLogical( Model % Equations(i) % Values, 'RMC' , .TRUE.)
ELSE
ActiveSolvers => ListGetIntegerArray( CurrentModel % Equations(i) % Values, &
'Active Solvers', Found )
IF ( Found ) THEN
DO k=1,SIZE(ActiveSolvers)
IF ( ActiveSolvers(k) == Solver % SolverId ) THEN
CALL ListAddLogical( Model % Equations(i) % Values, 'HP', .TRUE. )
CALL ListAddLogical( Model % Equations(i) % Values, 'RMC', .TRUE. )
EXIT
END IF
END DO
END IF
END IF
END DO
END IF
!------------------------------------------------------------------------------
END SUBROUTINE WhitneyAVSolver_Init0
!------------------------------------------------------------------------------
!------------------------------------------------------------------------------
SUBROUTINE WhitneyAVSolver_Init(Model,Solver,dt,Transient)
!------------------------------------------------------------------------------
USE MagnetoDynamicsUtils
IMPLICIT NONE
!------------------------------------------------------------------------------
TYPE(Solver_t) :: Solver
TYPE(Model_t) :: Model
REAL(KIND=dp) :: dt
LOGICAL :: Transient
!------------------------------------------------------------------------------
TYPE(Mesh_t), POINTER :: Mesh
LOGICAL :: Found
Mesh => GetMesh()
IF( Mesh % MeshDim /= 3 ) THEN
CALL Fatal('WhitneyAVSolver_Init','Solver requires 3D mesh!')
END IF
IF( CurrentCoordinateSystem() == AxisSymmetric .OR. &
CurrentCoordinateSystem() == CylindricSymmetric ) THEN
CALL Fatal('WhitneyAVSolver_Init','Solver not applicable to axially axisymmetric cases!')
END IF
! Historically a real array could be used for H-B Curve.
! This dirty piece of code makes things backward compatible.
BLOCK
INTEGER :: i
LOGICAL :: Cubic
TYPE(ValueList_t), POINTER :: Material
DO i=1,Model % NumberOfMaterials
Material => Model % Materials(i) % Values
IF( ListCheckPresent( Material, 'H-B Curve') ) THEN
Cubic = GetLogical( Material, 'Cubic spline for H-B curve',Found)
CALL ListRealArrayToDepReal(Material,'H-B Curve','dummy',&
CubicTable=Cubic) !Monotone=.TRUE.)
END IF
END DO
END BLOCK
!------------------------------------------------------------------------------
END SUBROUTINE WhitneyAVSolver_Init
!------------------------------------------------------------------------------
!------------------------------------------------------------------------------
!> Solve a vector potential A and scalar potential V from
!
!> sigma @A/@t + rot (1/mu) rot A + sigma grad(V) = J^s + curl(M^s) - sigma grad(V^s)
!> -div(sigma*(@A/@t+grad(V))) = 0 .
!
!> by using edge elements for A + nodal basis for V.
!> \ingroup Solvers
!------------------------------------------------------------------------------
SUBROUTINE WhitneyAVSolver( Model,Solver,dt,Transient )
!------------------------------------------------------------------------------
USE MagnetoDynamicsUtils
USE CircuitUtils
IMPLICIT NONE
!------------------------------------------------------------------------------
TYPE(Solver_t), TARGET :: Solver
TYPE(Model_t) :: Model
REAL(KIND=dp) :: dt
LOGICAL :: Transient
!------------------------------------------------------------------------------
! Local variables
!------------------------------------------------------------------------------
LOGICAL :: AllocationsDone = .FALSE., Found
TYPE(Element_t),POINTER :: Element, Edge
REAL(KIND=dp) :: Norm, PrevDT=-1, RelChange
TYPE(ValueList_t), POINTER :: SolverParams, BodyForce, Material, &
BC, BodyParams, PrevMaterial
INTEGER :: n,nb,nd,t,istat,i,j,k,l,nNodes,Active,FluxCount=0, &
NoIterationsMax,NoIterationsMin, nsize
TYPE(Mesh_t), POINTER :: Mesh
REAL(KIND=dp), POINTER :: VecPot(:)
REAL(KIND=dp), POINTER :: Cwrk(:,:,:), Acoef_t(:,:,:) => NULL()
REAL(KIND=dp), ALLOCATABLE :: LOAD(:,:), Acoef(:), Tcoef(:,:,:), &
GapLength(:), AirGapMu(:), LamThick(:), &
LamCond(:), Wbase(:), RotM(:,:,:), &
ThinLineCrossect(:),ThinLineCond(:), &
SRDA(:,:), SRDV(:)
REAL(KIND=dp), ALLOCATABLE :: STIFF(:,:), MASS(:,:), DAMP(:,:), FORCE(:), &
JFixFORCE(:), JFixVec(:,:),PrevSol(:)
CHARACTER(LEN=MAX_NAME_LEN):: LaminateStackModel, CoilType
LOGICAL :: LaminateStack, CoilBody, HasHBCurve, HasReluctivityFunction, &
NewMaterial
INTEGER, POINTER :: Perm(:)
INTEGER, ALLOCATABLE :: FluxMap(:)
LOGICAL, ALLOCATABLE, SAVE :: TreeEdges(:)
LOGICAL :: Stat, EigenAnalysis, TG, DoneAssembly=.FALSE., &
SkipAssembly, ConstantSystem, ConstantBulk, JFix, JFixSolve, FoundRelax, &
PiolaVersion, SecondOrder, LFact, LFactFound, EdgeBasis, &
HasTensorReluctivity
LOGICAL :: SteadyGauge, TransientGauge, TransientGaugeCollected=.FALSE., &
HasStabC, RegularizeWithMass
REAL(KIND=dp) :: Relax, gauge_penalize_c, gauge_penalize_m, gauge_coeff, &
mass_reg_epsilon, newton_eps
REAL(KIND=dp) :: NewtonTol
INTEGER :: NewtonIter
LOGICAL :: Newton
TYPE(Variable_t), POINTER :: Var, JFixVar, CoordVar
TYPE(Matrix_t), POINTER :: A
TYPE(ListMatrix_t), POINTER, SAVE :: BasicCycles(:)
TYPE(ValueList_t), POINTER :: CompParams
TYPE(Matrix_t), POINTER :: CM=>NULL()
INTEGER :: n_n, n_e
INTEGER, POINTER :: Vperm(:), Aperm(:)
REAL(KIND=dp), POINTER :: Avals(:), Vvals(:)
CHARACTER(LEN=MAX_NAME_LEN):: CoilCurrentName
TYPE(Variable_t), POINTER :: CoilCurrentVar, BsVar, AsVar
REAL(KIND=dp) :: CurrAmp
LOGICAL :: UseCoilCurrent, ElemCurrent, ElectroDynamics, EigenSystem
LOGICAL :: SRDecomposition=.FALSE.
TYPE(ValueHandle_t), SAVE :: mu_h
TYPE(Solver_t), POINTER :: pSolver
SAVE STIFF, LOAD, MASS, DAMP, FORCE, JFixFORCE, JFixVec, Tcoef, GapLength, AirGapMu, &
Acoef, Cwrk, LamThick, LamCond, Wbase, RotM, AllocationsDone, &
Acoef_t, ThinLineCrossect, ThinLineCond, SRDecomposition, SRDA, SRDV
!------------------------------------------------------------------------------
IF ( .NOT. ASSOCIATED( Solver % Matrix ) ) RETURN
CALL Info('WhitneyAVSolver','',Level=6 )
CALL Info('WhitneyAVSolver','-------------------------------------------------',Level=6 )
IF( Transient ) THEN
CALL Info('WhitneyAVSolver','Solving transient AV equations with edge elements',Level=5 )
ELSE
CALL Info('WhitneyAVSolver','Solving steady-state AV equations with edge elements',Level=5 )
END IF
SolverParams => GetSolverParams()
pSolver => Solver
ElectroDynamics = ListGetLogical( SolverParams, 'Electrodynamics model', Found )
EigenSystem = ListGetLogical( SolverParams, 'Eigen Analysis', Found )
SecondOrder = GetLogical( SolverParams, 'Quadratic Approximation', Found )
IF( SecondOrder ) THEN
PiolaVersion = .TRUE.
ELSE
PiolaVersion = GetLogical( SolverParams, 'Use Piola Transform', Found )
END IF
IF (PiolaVersion) THEN
CALL Info('WhitneyAVSolver', &
'Using Piola Transformed element basis functions',Level=4)
END IF
SteadyGauge = GetLogical(SolverParams, 'Use Lagrange Gauge', Found) .AND. .NOT. Transient
TransientGauge = GetLogical(SolverParams, 'Use Lagrange Gauge', Found) .AND. Transient
CoilCurrentName = GetString( SolverParams,'Current Density Name',UseCoilCurrent )
IF(.NOT. UseCoilCurrent ) THEN
UseCoilCurrent = GetLogical(SolverParams,'Use Nodal CoilCurrent',Found )
IF(UseCoilCurrent) THEN
CoilCurrentName = 'CoilCurrent'
ELSE
UseCoilCurrent = GetLogical(SolverParams,'Use Elemental CoilCurrent',Found )
IF(UseCoilCurrent) CoilCurrentName = 'CoilCurrent e'
END IF
END IF
ElemCurrent = .FALSE.
IF( UseCoilCurrent ) THEN
CoilCurrentVar => VariableGet(Solver % Mesh % Variables, CoilCurrentName )
IF( ASSOCIATED( CoilCurrentVar ) ) THEN
CALL Info('WhitneyAVSolver','Using precomputed field for current density: '//TRIM(CoilCurrentName),Level=5)
IF( CoilCurrentVar % TYPE == Variable_on_nodes_on_elements ) THEN
ElemCurrent = .TRUE.
ELSE
CALL Warn('WhitneyAVSolver','Precomputed CoilCurrent is not an elemental field!')
END IF
ELSE
CALL Fatal('WhitneyAVSolver','Elemental current requested but not found:'//TRIM(CoilCurrentName))
END IF
END IF
IF (SteadyGauge) THEN
CALL Info("WhitneyAVSolver", "Utilizing Lagrange multipliers for gauge condition in steady state computation")
IF(.not. ListCheckPresent( SolverParams, 'Linear System Refactorize') ) THEN
CALL ListAddLogical( SolverParams, 'Linear System Refactorize', .TRUE. )
END IF
END IF
IF(TransientGauge) THEN
CALL Info("WhitneyAVSolver", "Utilizing Lagrange multipliers for gauge condition in transient computation")
IF (.NOT. ListCheckPresent( SolverParams, "enforce exact dirichlet bcs" ) ) THEN
CALL ListAddLogical(SolverParams,"enforce exact dirichlet bcs",.FALSE.)
CALL Info("WhitneyAVSolver", "Setting 'enforce exact dirichlet bcs = Logical False'")
END IF
IF (.NOT. ListCheckPresent( SolverParams, "optimize bandwidth" ) ) THEN
CALL ListAddLogical(SolverParams,"optimize bandwidth",.FALSE.)
CALL Info("WhitneyAVSolver", "Setting 'Optimize Bandwidth = Logical False'")
ELSEIF (ListGetLogical(SolverParams, "Optimize bandwidth")) THEN
CALL Warn("WhitneyAVSolver", &
"Optimize bandwidth and use lagrange gauge in transient is known not to work. ")
END IF
IF(.not. ListCheckPresent( SolverParams, 'Linear System Refactorize') ) THEN
CALL ListAddLogical( SolverParams, 'Linear System Refactorize', .TRUE. )
END IF
! TODO: Check if there is mortar boundaries and report the above in that case only.
END IF
Newton = .FALSE.
newton_eps = GetCReal(SolverParams, 'Newton epsilon', Found )
IF(.NOT. Found) newton_eps = 1.0e-3
mass_reg_epsilon = GetCReal(SolverParams, 'Mass regularize epsilon', RegularizeWithMass)
IF (RegularizeWithMass .AND. mass_reg_epsilon == 0.0_dp) THEN
RegularizeWithMass = .FALSE.
END IF
IF (RegularizeWithMass) THEN
WRITE (Message, *) 'Mass regularization epsilon', mass_reg_epsilon
CALL Info("WhitneyAVSolver", Message)
END IF
gauge_coeff = GetCReal(SolverParams, 'Lagrange Gauge coefficient',Found )
IF(.NOT. Found ) gauge_coeff = 1.0_dp
gauge_penalize_c = GetCReal(SolverParams, 'Lagrange Gauge Penalization coefficient', HasStabC)
gauge_penalize_m = GetCReal(SolverParams, 'Lagrange Gauge Penalization coefficient mass', Found)
HasStabC = HasStabC .OR. Found
IF ( HasStabC ) THEN
WRITE (Message, *) 'Lagrange Gauge penalization coefficient', gauge_penalize_c
CALL Info('WhitneyAVSolver', message)
WRITE (Message, *) 'Lagrange Gauge penalization coefficient mass', gauge_penalize_m
CALL Info('WhitneyAVSolver', message)
END IF
! Gauge tree, if requested or using direct solver:
! ------------------------------------------------
TG = GetLogical(SolverParams,'Use tree gauge', Found)
IF (.NOT. Found) THEN
IF (.NOT. (SteadyGauge .OR. TransientGauge)) THEN
IF( GetString(SolverParams,'Linear System Solver',Found)=='direct') THEN
IF( PiolaVersion ) THEN
CALL Fatal('WhitneyAVSolver','Direct solver (with tree gauge) is only possible with the lowest order edge basis!')
ELSE
CALL Info('WhitneyAVSolver','Defaulting to tree gauge when using direct solver')
TG = .TRUE.
END IF
END IF
END IF
END IF
IF( PiolaVersion .AND. TG ) THEN
CALL Fatal('WhitneyAVSolver', &
'Tree Gauge cannot be used in conjunction with Piola transformation')
END IF
!Allocate some permanent storage, this is done first time only:
!--------------------------------------------------------------
Mesh => GetMesh()
nNodes = Mesh % NumberOfNodes
Perm => Solver % Variable % Perm
Vecpot => Solver % Variable % Values
IF ( .NOT. AllocationsDone .OR. Solver % MeshChanged ) THEN
N = Mesh % MaxElementDOFs ! just big enough
SRDecomposition = GetLogical( SolverParams, 'Source-Reaction Decomposition', Found )
IF ( .NOT. Found ) SRDecomposition = .False.
IF ( SRDecomposition ) THEN
ALLOCATE( SRDA(3,n), SRDV(n), STAT=istat )
IF ( istat /= 0 ) THEN
CALL Fatal( 'WhitneyAVSolver', 'Memory allocation error.' )
END IF
BsVar => VariableGet(Solver % Mesh % Variables, &
'src magnetic flux density', ThisOnly=.TRUE., UnFoundFatal=.TRUE.)
IF(ASSOCIATED(BsVar)) THEN
IF(BsVar % TYPE /= Variable_on_gauss_points) THEN
CALL Fatal('WhitneyAVSolver','BsVar should be an IP variable!')
END IF
END IF
AsVar => VariableGet(Solver % Mesh % Variables, &
'src vector potential', ThisOnly=.TRUE., UnFoundFatal=.TRUE.)
IF(ASSOCIATED(AsVar)) THEN
IF(AsVar % TYPE /= Variable_on_gauss_points) THEN
CALL Fatal('WhitneyAVSolver','AsVar should be an IP variable!')
END IF
END IF
END IF
IF(ALLOCATED(FORCE)) THEN
DEALLOCATE(FORCE, JFixFORCE, JFixVec, LOAD, STIFF, MASS, DAMP, TCoef, GapLength, AirGapMu, &
Acoef, LamThick, LamCond, WBase, RotM, ThinLineCrossect, ThinLineCond )
END IF
ALLOCATE( FORCE(N), JFixFORCE(n), JFixVec(3,n), LOAD(7,N), STIFF(N,N), &
MASS(N,N), DAMP(N,N), Tcoef(3,3,N), GapLength(N), &
AirGapMu(N), Acoef(N), LamThick(N), &
LamCond(N), Wbase(N), RotM(3,3,N), &
ThinLineCrossect(N), ThinLineCond(N), STAT=istat )
IF ( istat /= 0 ) THEN
CALL Fatal( 'WhitneyAVSolver', 'Memory allocation error.' )
END IF
IF(GetString(SolverParams,'Linear System Solver',Found)=='block') THEN
n = Mesh % NumberOfNodes
n_n = COUNT(Perm(1:n)>0)
n_e = COUNT(Perm(n+1:)>0)
ALLOCATE( Avals(n_e), Vvals(n_n) )
Vvals = Vecpot(1:n)
Avals = Vecpot(n+1:)
ALLOCATE(Aperm(SIZE(Perm)),Vperm(SIZE(Perm)))
Aperm = 0; Vperm = 0
DO i=1,SIZE(AVals)
Aperm(i+n) = i
END DO
DO i=1,SIZE(Vvals)
Vperm(i)=i
END DO
CALL VariableAdd(Mesh % Variables,Mesh,Solver, &
GetVarName(Solver % Variable)//' 1',1,Vvals,Vperm)
CALL VariableAdd(Mesh % Variables,Mesh,Solver, &
GetVarName(Solver % Variable)//' 2',1,Avals,Aperm)
END IF
NULLIFY( Cwrk )
IF (TransientGauge) THEN
A => GetMatrix()
IF (.NOT. TransientGaugeCollected) &
CM => AddConstraintFromBulk(A, Solver % Matrix % ConstraintMatrix)
END IF
AllocationsDone = .TRUE.
END IF
ConstantSystem = GetLogical( SolverParams, 'Constant System', Found )
ConstantBulk = GetLogical( SolverParams, 'Constant Bulk System', Found )
SkipAssembly = DoneAssembly.AND.(ConstantBulk.OR.ConstantSystem)
JFix = GetLogical(SolverParams,'Fix input Current Density', Found)
IF (.NOT. ( Found .OR. Transient ) ) THEN
! Only fix the current density if there is one
JFix = ListCheckPrefixAnyBodyForce(Model, 'Current Density')
END IF
JFixSolve = JFix
IF (JFix) THEN
JFixPhase = 1
CALL JFixPotentialSolver(Model,Solver,dt,Transient)
JFixVar => VariableGet(Mesh % Variables, 'JFix')
IF(.NOT. ASSOCIATED( JFixRhs ) ) THEN
CALL Fatal('WhitneyAVSolver','JFixRhs should be associated!')
END IF
IF(.NOT. ASSOCIATED( JFixSurfacePerm ) ) THEN
CALL Fatal('WhitneyAVSolver','JFixSurfacePerm should be associated!')
END IF
IF(.NOT. ALLOCATED( JFixSurfaceVec ) ) THEN
CALL Fatal('WhitneyAVSolver','JFixSurfaceVec should be associated!')
END IF
END IF
!
! Use vec.pot. dofs only for convergence:
! ----------------------------------------
CALL ListAddInteger(SolverParams,'Norm Permutation',nNodes+1)
! Resolve internal non.linearities, if requested:
! ----------------------------------------------
NoIterationsMax = GetInteger( SolverParams, 'Nonlinear System Max Iterations',Found)
IF(.NOT. Found) NoIterationsMax = 1
NoIterationsMin = GetInteger( SolverParams, 'Nonlinear System Min Iterations',Found)
IF(.NOT. Found) NoIterationsMin = 1
! Use also these keyword for compatibility with ElmerGUI and old practices
NewtonIter = GetInteger( SolverParams,&
'Nonlinear System Newton After Iterations',Found )
IF(.NOT. Found ) NewtonIter = NoIterationsMax
NewtonTol = GetCReal( SolverParams,&
'Nonlinear System Newton After Tolerance',Found )
! Not refactorizing seems to break things with gauges
! IF (SteadyGauge) THEN
! IF(.not. ListCheckPresent( SolverParams, 'Linear System Refactorize') ) THEN
! CALL ListAddLogical( SolverParams, 'Linear System Refactorize', .TRUE. )
! END IF
! END IF
LFact = GetLogical( SolverParams,'Linear System Refactorize', LFactFound )
IF ( dt /= PrevDT .AND. LFactFound .AND. .NOT. LFact ) THEN
CALL ListAddLogical( SolverParams, 'Linear System Refactorize', .TRUE. )
END IF
EdgeBasis = .NOT.LFactFound .AND. GetLogical( SolverParams, 'Edge Basis', Found )
CALL DefaultStart()
DO i=1,NoIterationsMax
Newton = GetLogical( SolverParams,'Newton-Raphson iteration',Found)
IF(.NOT. Found ) Newton = ( i > NewtonIter .OR. Solver % Variable % NonlinChange < NewtonTol )
IF( NoIterationsMax > 1 ) THEN
CALL Info('WhitneyAVSolver','Nonlinear iteration: '//I2S(i),Level=8 )
END IF
IF( DoSolve(i) ) THEN
IF(i>=NoIterationsMin) EXIT
END IF
IF( EdgeBasis ) CALL ListAddLogical(SolverParams,'Linear System Refactorize',.FALSE.)
! Currently assume that the source terms are constant over the nonlinear iteration
JFixSolve = .FALSE.
END DO
IF ( EdgeBasis ) CALL ListRemove( SolverParams, 'Linear System Refactorize' )
IF ( dt /= PrevDT .AND. LFactFound .AND. .NOT. LFact ) THEN
CALL ListAddLogical( SolverParams, 'Linear System Refactorize', .FALSE. )
END IF
PrevDT = dt
CALL CalculateLumped(Model % NumberOfBodyForces)
CoordVar => VariableGet(Mesh % Variables,'Coordinates')
IF(ASSOCIATED(CoordVar)) THEN
DO i=1,Mesh % NumberOfNodes
j = 3*(CoordVar % Perm(i)-1)
CoordVar % Values(j+1) = Mesh % Nodes % x(i)
CoordVar % Values(j+2) = Mesh % Nodes % y(i)
CoordVar % Values(j+3) = Mesh % Nodes % z(i)
END DO
END IF
CALL DefaultFinish()
CALL Info('WhitneyAVSolver','All done',Level=8 )
CALL Info('WhitneyAVSolver','-------------------------------------------',Level=8 )
CONTAINS
!------------------------------------------------------------------------------
LOGICAL FUNCTION DoSolve(IterNo) RESULT(Converged)
!------------------------------------------------------------------------------
IMPLICIT NONE
CHARACTER(LEN=MAX_NAME_LEN) :: potname
INTEGER :: i,j,k,t,n,nd,nb,IterNo
REAL(KIND=dp)::TOL,Norm,PrevNorm, NonLinError, LinTol, RelTol, BaseTol
LOGICAL :: Found, FoundMagnetization, CalculateNonlinearResidual, LFactFound
LOGICAL :: AdaptiveTols, FoundAny, ConstraintActive, GotCoil
TYPE(Matrix_t), POINTER :: MMatrix
REAL(KIND=dp), POINTER :: Mx(:), Mb(:), Mr(:)
REAL(KIND=dp), DIMENSION(:), ALLOCATABLE :: TmpRVec, TmpRHSVec
CHARACTER(LEN=MAX_NAME_LEN) :: ConvergenceType
REAL(KIND=dp), POINTER CONTIG :: SaveValues(:), SaveRHS(:), ConstraintValues(:)
SAVE TmpRHSVec, TmpRVec
!-----------------
!System assembly:
!-----------------
A => GetMatrix()
IF (TransientGauge) ConstraintValues => CM % Values
300 CONTINUE
IF ( SkipAssembly) THEN
DO i=1,SIZE(A % RHS)
A % RHS(i) = A % BulkRHS(i)
END DO
DO i=1,SIZE(A % Values)
A % Values(i) = A % BulkValues(i)
END DO
IF ( ConstantBulk ) GOTO 100
IF ( ConstantSystem ) GOTO 200
END IF
! Timing
CALL ResetTimer('MGDynAssembly')
CALL DefaultInitialize()
Active = GetNOFActive()
IF( ListCheckPresentAnyMaterial(Model,'Reluctivity Function') ) THEN
CALL ListInitElementKeyword( mu_h,'Material','Reluctivity Function',&
EvaluateAtIp=.TRUE.,DummyCount=3)
END IF
PrevMaterial => NULL()
DO t=1,active
Element => GetActiveElement(t)
n = GetElementNOFNodes() ! kulmat
nd = GetElementNOFDOFs() ! vapausasteet
nb = GetElementNOFBDOFs() ! sisäiset vapausasteet
IF (SIZE(Tcoef,3) /= n) THEN
DEALLOCATE(Tcoef)
ALLOCATE(Tcoef(3,3,n), STAT=istat)
IF ( istat /= 0 ) THEN
CALL Fatal( 'WhitneyAVSolver', 'Memory allocation error.' )
END IF
END IF
LOAD = 0.0d0
! This way we don't have to inquire the list for all three components separately.
! Also writing of the sif file becomes more economical.
BodyForce => GetBodyForce()
FoundMagnetization = .FALSE.
! If the coil current field is elemental it is discontinuous and need not be limited
! to the body force. For nodal ones we don't have the same luxury.
GotCoil = .FALSE.
IF( UseCoilCurrent ) THEN
IF( ElemCurrent .OR. ASSOCIATED(BodyForce) ) THEN
CALL GetVectorLocalSolution( Load,UElement=Element,UVariable=CoilCurrentVar,Found=GotCoil)
END IF
END IF
IF ( ASSOCIATED(BodyForce) ) THEN
! If not already given by CoilCurrent, request for current density
IF( .NOT. GotCoil) THEN
CALL GetRealVector( BodyForce, Load(1:3,1:n), 'Current Density', Found )
END IF
CurrAmp = ListGetCReal( BodyForce,'Current Density Multiplier',Found )
IF(Found) Load(1:3,1:n) = CurrAmp * Load(1:3,1:n)
CALL GetRealVector( BodyForce, Load(4:6,1:n), &
'Magnetization', FoundMagnetization )
Load(7,1:n) = GetReal( BodyForce, 'Electric Potential', Found )
END IF
Material => GetMaterial( Element )
NewMaterial = .NOT. ASSOCIATED(Material, PrevMaterial)
IF (NewMaterial) THEN
HasHBCurve = ListCheckPresent(Material, 'H-B Curve')
HasReluctivityFunction = ListCheckPresent(Material,'Reluctivity Function')
PrevMaterial => Material
END IF
IF(ASSOCIATED(Material).AND..NOT.FoundMagnetization) THEN
CALL GetRealVector( Material, Load(4:6,1:n), 'Magnetization', FoundMagnetization )
END IF
CoilBody = .FALSE.
CompParams => GetComponentParams( Element )
CoilType = ''
RotM = 0._dp
ConstraintActive = .TRUE.
IF (ASSOCIATED(CompParams)) THEN
CoilType = GetString(CompParams, 'Coil Type', Found)
IF (Found) THEN
SELECT CASE (CoilType)
CASE ('stranded')
CoilBody = .TRUE.
CASE ('massive')
CoilBody = .TRUE.
CASE ('foil winding')
CoilBody = .TRUE.
CALL GetElementRotM(Element, RotM, n)
CASE DEFAULT
CALL Fatal ('WhitneyAVSolver', 'Non existent Coil Type Chosen!')
END SELECT
ConstraintActive = GetLogical(CompParams, 'Activate Constraint', Found )
END IF
END IF
LaminateStack = .FALSE.
LaminateStackModel = ''
HasTensorReluctivity = .FALSE.
Acoef = 0.0d0
Tcoef = 0.0d0
IF ( ASSOCIATED(Material) ) THEN
IF ( .NOT. ( HasHBCurve .OR. HasReluctivityFunction ) ) THEN
CALL GetReluctivity(Material,Acoef_t,n,HasTensorReluctivity)
IF (HasTensorReluctivity) THEN
IF (SIZE(Acoef_t,1)==1 .AND. SIZE(Acoef_t,2)==1) THEN
i = MIN(SIZE(Acoef), SIZE(Acoef_t,3))
Acoef(1:i) = Acoef_t(1,1,1:i)
HasTensorReluctivity = .FALSE.
END IF
ELSE
CALL GetReluctivity(Material,Acoef,n)
END IF
IF (HasTensorReluctivity) THEN
IF (size(Acoef_t,1)/=3) CALL Fatal('WhitneyAVSolver', &
'Reluctivity tensor should be of size 3x3')
END IF
END IF
!------------------------------------------------------------------------------
! Read conductivity values (might be a tensor)
!------------------------------------------------------------------------------
Tcoef = GetElectricConductivityTensor(Element,n,'re',CoilBody,CoilType)
LaminateStackModel = GetString( Material, 'Laminate Stack Model', LaminateStack )
END IF
LamThick = 0.0_dp
LamCond = 0.0_dp
IF (LaminateStack) THEN
SELECT CASE(LaminateStackModel)
CASE('low-frequency model')
LamThick(1:n) = GetReal( Material, 'Laminate Thickness', Found )
IF (.NOT. Found) CALL Fatal('WhitneyAVSolver', 'Laminate Thickness not found!')
LamCond(1:n) = GetReal( Material, 'Laminate Stack Conductivity', Found )
IF (.NOT. Found) CALL Fatal('WhitneyAVSolver', 'Laminate Stack Conductivity not found!')
CASE DEFAULT
CALL WARN('WhitneyAVSolver', 'Nonexistent Laminate Stack Model chosen!')
END SELECT
END IF
!Get element local matrix and rhs vector:
!----------------------------------------
CALL LocalMatrix( MASS, DAMP, STIFF, FORCE, JFixFORCE, JFixVec, LOAD, &
Tcoef, Acoef, LaminateStack, LaminateStackModel, &
LamThick, LamCond, CoilBody, CoilType, RotM, ConstraintActive, &
Element, n, nd+nb, PiolaVersion, SecondOrder, t==1)
! Update global matrix and rhs vector from local matrix & vector:
!---------------------------------------------------------------
IF (Transient .OR. EigenSystem) THEN
CALL DefaultUpdateMass(MASS)
IF ( ElectroDynamics ) CALL DefaultUpdateDamp(DAMP)
END IF
! Collect weak divergence constraint.
!-----------------------------------------------------------------
IF (Transient .AND. TransientGauge .AND. .NOT. TransientGaugeCollected) THEN
CALL LocalConstraintMatrix(Element, n, nd+nb, PiolaVersion, SecondOrder)
END IF
CALL DefaultUpdateEquations(STIFF,FORCE)
! Memorize stuff for the fixing potential
! 1) Divergence of the source term
! 2) The source terms at the surface to determine the direction
!-------------------------------------------------------------------
IF( JFixSolve ) THEN
JFixRhs(JFixVar % Perm(Element % NodeIndexes)) = &
JFixRhs(JFixVar % Perm(Element % NodeIndexes)) + JFixFORCE(1:n)
DO i=1,n
j = JFixSurfacePerm(Element % NodeIndexes(i) )
IF( j > 0 ) JFixSurfaceVec(3*j-2:3*j) = &
JFixSurfaceVec(3*j-2:3*j) + JFixVec(1:3,i)
END DO
END IF
END DO
CALL DefaultFinishBulkAssembly(BulkUpdate=ConstantBulk)
! If we are solving the fixing potential for this nonlinear iteration then
! add its contribution to the AV equation.
IF( JFixSolve ) THEN
CALL Info('WhitneyAVSolver','Solving the fixing potential',Level=7)
JFixPhase = 2
IF( ListGetLogical( SolverParams,'Precomputed Fixing Term',Found ) ) THEN
CALL Info('WhitneyAVSolver','Adding precomputed source term: g fix')
Var => VariableGet( Mesh % Variables,'g fix')
JFixRhs = JfixRhs + Var % Values
END IF
CALL JFixPotentialSolver(Model,Solver,dt,Transient)
CALL Info('WhitneyAVSolver','Adding the fixing potential to the r.h.s. of AV equation',Level=10)
DO t=1,active
Element => GetActiveElement(t)
n = GetElementNOFNodes()
nd = GetElementNOFDOFs()
nb = GetElementNOFBDOFs()
CALL LocalFixMatrix( FORCE, Element, n, nd+nb, PiolaVersion, SecondOrder)
END DO
CALL Info('WhitneyAVSolver','Finished adding the fixing potential',Level=10)
END IF
! This adds a precomputed source term to r.h.s. of the equation.
! Note that this is assumed to be already mapped to nodes.
IF( ListGetLogical( SolverParams,'Precomputed Source Term',Found ) ) THEN
CALL Info('WhitneyAVSolver','Adding precomputed source term: g')
Var => VariableGet( Mesh % Variables,'g')
Solver % Matrix % Rhs = Solver % Matrix % Rhs + Var % Values
END IF
100 CONTINUE
!
! Robin type of BC in terms of H:
!--------------------------------
Active = GetNOFBoundaryElements()
DO t=1,Active
Element => GetBoundaryElement(t)
BC=>GetBC()
IF (.NOT. ASSOCIATED(BC) ) CYCLE
SELECT CASE(GetElementFamily())
CASE(1)
CYCLE
CASE(2)
k = GetBoundaryEdgeIndex(Element,1); Element => Mesh % Edges(k)
CASE(3,4)
k = GetBoundaryFaceIndex(Element) ; Element => Mesh % Faces(k)
END SELECT
IF (.NOT. ActiveBoundaryElement(Element)) CYCLE
Model % CurrentElement => Element
nd = GetElementNOFDOFs(Element)
n = GetElementNOFNodes(Element)
CALL GetRealVector( BC, Load(1:3,1:n), 'Magnetic Field Strength', Found )
FoundAny = Found
Acoef(1:n) = GetReal( BC, 'Magnetic Transfer Coefficient', Found )
FoundAny = FoundAny .OR. Found
Load(4,1:n) = GetReal( BC, 'Electric Current Density', Found )
FoundAny = FoundAny .OR. Found