-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Adapter.C
1672 lines (1412 loc) · 55.9 KB
/
Adapter.C
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
#include "Adapter.H"
#include "Interface.H"
#include "Utilities.H"
#include "IOstreams.H"
using namespace Foam;
preciceAdapter::Adapter::Adapter(const Time& runTime, const fvMesh& mesh)
: runTime_(runTime),
mesh_(mesh)
{
adapterInfo("Loaded the OpenFOAM-preCICE adapter - v1.3.1.", "info");
return;
}
bool preciceAdapter::Adapter::configFileRead()
{
// We need a try-catch here, as if reading preciceDict fails,
// the respective exception will be reduced to a warning.
// See also comment in preciceAdapter::Adapter::configure().
try
{
SETUP_TIMER();
adapterInfo("Reading preciceDict...", "info");
// TODO: static is just a quick workaround to be able
// to find the dictionary also out of scope (e.g. in KappaEffective).
// We need a better solution.
static IOdictionary preciceDict(
IOobject(
"preciceDict",
runTime_.system(),
mesh_,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE));
// Read and display the preCICE configuration file name
preciceConfigFilename_ = preciceDict.get<fileName>("preciceConfig");
DEBUG(adapterInfo(" precice-config-file : " + preciceConfigFilename_));
// Read and display the participant name
participantName_ = preciceDict.get<word>("participant");
DEBUG(adapterInfo(" participant name : " + participantName_));
// Read and display the list of modules
DEBUG(adapterInfo(" modules requested : "));
auto modules_ = preciceDict.get<wordList>("modules");
for (const auto& module : modules_)
{
DEBUG(adapterInfo(" - " + module + "\n"));
// Set the modules switches
if (module == "CHT")
{
CHTenabled_ = true;
}
if (module == "FSI")
{
FSIenabled_ = true;
}
if (module == "FF")
{
FFenabled_ = true;
}
}
// Every interface is a subdictionary of "interfaces",
// each with an arbitrary name. Read all of them and create
// a list (here: pointer) of dictionaries.
const auto* interfaceDictPtr = preciceDict.findDict("interfaces");
DEBUG(adapterInfo(" interfaces : "));
// Check if we found any interfaces
// and get the details of each interface
if (!interfaceDictPtr)
{
adapterInfo(" Empty list of interfaces", "warning");
return false;
}
else
{
for (const entry& interfaceDictEntry : *interfaceDictPtr)
{
if (interfaceDictEntry.isDict())
{
const dictionary& interfaceDict = interfaceDictEntry.dict();
struct InterfaceConfig interfaceConfig;
interfaceConfig.meshName = interfaceDict.get<word>("mesh");
DEBUG(adapterInfo(" - mesh : " + interfaceConfig.meshName));
// By default, assume "faceCenters" as locationsType
interfaceConfig.locationsType = interfaceDict.lookupOrDefault<word>("locations", "faceCenters");
DEBUG(adapterInfo(" locations : " + interfaceConfig.locationsType));
// By default, assume that no mesh connectivity is required (i.e. no nearest-projection mapping)
interfaceConfig.meshConnectivity = interfaceDict.lookupOrDefault<bool>("connectivity", false);
// Mesh connectivity only makes sense in case of faceNodes, check and raise a warning otherwise
if (interfaceConfig.meshConnectivity && (interfaceConfig.locationsType == "faceCenters" || interfaceConfig.locationsType == "volumeCenters" || interfaceConfig.locationsType == "volumeCentres"))
{
DEBUG(adapterInfo("Mesh connectivity is not supported for faceCenters or volumeCenters. \n"
"Please configure the desired interface with the locationsType faceNodes. \n"
"Have a look in the adapter documentation for detailed information.",
"warning"));
return false;
}
DEBUG(adapterInfo(" connectivity : " + std::to_string(interfaceConfig.meshConnectivity)));
DEBUG(adapterInfo(" patches : "));
auto patches = interfaceDict.get<wordList>("patches");
for (auto patch : patches)
{
interfaceConfig.patchNames.push_back(patch);
DEBUG(adapterInfo(" - " + patch));
}
DEBUG(adapterInfo(" cellSets : "));
auto cellSets = interfaceDict.lookupOrDefault<wordList>("cellSets", wordList());
for (auto cellSet : cellSets)
{
interfaceConfig.cellSetNames.push_back(cellSet);
DEBUG(adapterInfo(" - " + cellSet));
}
if (!interfaceConfig.cellSetNames.empty() && !(interfaceConfig.locationsType == "volumeCenters" || interfaceConfig.locationsType == "volumeCentres"))
{
adapterInfo("Cell sets are not supported for locationType != volumeCenters. \n"
"Please configure the desired interface with the locationsType volumeCenters. \n"
"Have a look in the adapter documentation for detailed information.",
"warning");
return false;
}
DEBUG(adapterInfo(" writeData : "));
auto writeData = interfaceDict.get<wordList>("writeData");
for (auto writeDatum : writeData)
{
interfaceConfig.writeData.push_back(writeDatum);
DEBUG(adapterInfo(" - " + writeDatum));
}
DEBUG(adapterInfo(" readData : "));
auto readData = interfaceDict.get<wordList>("readData");
for (auto readDatum : readData)
{
interfaceConfig.readData.push_back(readDatum);
DEBUG(adapterInfo(" - " + readDatum));
}
interfacesConfig_.push_back(interfaceConfig);
}
}
}
// NOTE: set the switch for your new module here
// If the CHT module is enabled, create it, read the
// CHT-specific options and configure it.
if (CHTenabled_)
{
CHT_ = new CHT::ConjugateHeatTransfer(mesh_);
if (!CHT_->configure(preciceDict))
{
return false;
}
}
// If the FSI module is enabled, create it, read the
// FSI-specific options and configure it.
if (FSIenabled_)
{
// Check for unsupported FSI with meshConnectivity
for (uint i = 0; i < interfacesConfig_.size(); i++)
{
if (interfacesConfig_.at(i).meshConnectivity == true)
{
adapterInfo(
"You have requested mesh connectivity (most probably for nearest-projection mapping) "
"and you have enabled the FSI module. "
"Mapping with connectivity information is not implemented for FSI, only for CHT-related fields. "
"warning");
return false;
}
}
FSI_ = new FSI::FluidStructureInteraction(mesh_, runTime_);
if (!FSI_->configure(preciceDict))
{
return false;
}
}
if (FFenabled_)
{
FF_ = new FF::FluidFluid(mesh_);
if (!FF_->configure(preciceDict))
{
return false;
}
}
// NOTE: Create your module and read any options specific to it here
if (!CHTenabled_ && !FSIenabled_ && !FFenabled_) // NOTE: Add your new switch here
{
adapterInfo("No module is enabled.", "error-deferred");
return false;
}
// TODO: Loading modules should be implemented in more general way,
// in order to avoid code duplication. See issue #16 on GitHub.
ACCUMULATE_TIMER(timeInConfigRead_);
}
catch (const Foam::error& e)
{
adapterInfo(e.message(), "error-deferred");
return false;
}
return true;
}
void preciceAdapter::Adapter::configure()
{
// Read the adapter's configuration file
if (!configFileRead())
{
// This method is called from the functionObject's read() method,
// which is called by the Foam::functionObjectList::read() method.
// All the exceptions triggered in this method are caught as
// warnings and the simulation continues simply without the
// functionObject. However, we want the simulation to exit with an
// error in case something is wrong. We store the information that
// there was an error and it will be handled by the first call to
// the functionObject's execute(), which can throw errors normally.
errorsInConfigure = true;
return;
}
try
{
// Check the timestep type (fixed vs adjustable)
DEBUG(adapterInfo("Checking the timestep type (fixed vs adjustable)..."));
adjustableTimestep_ = runTime_.controlDict().lookupOrDefault("adjustTimeStep", false);
if (adjustableTimestep_)
{
DEBUG(adapterInfo(" Timestep type: adjustable."));
}
else
{
DEBUG(adapterInfo(" Timestep type: fixed."));
}
// Construct preCICE
SETUP_TIMER();
DEBUG(adapterInfo("Creating the preCICE solver interface..."));
DEBUG(adapterInfo(" Number of processes: " + std::to_string(Pstream::nProcs())));
DEBUG(adapterInfo(" MPI rank: " + std::to_string(Pstream::myProcNo())));
precice_ = new precice::Participant(participantName_, preciceConfigFilename_, Pstream::myProcNo(), Pstream::nProcs());
DEBUG(adapterInfo(" preCICE solver interface was created."));
ACCUMULATE_TIMER(timeInPreciceConstruct_);
// Create interfaces
REUSE_TIMER();
DEBUG(adapterInfo("Creating interfaces..."));
for (uint i = 0; i < interfacesConfig_.size(); i++)
{
std::string namePointDisplacement = FSIenabled_ ? FSI_->getPointDisplacementFieldName() : "default";
std::string nameCellDisplacement = FSIenabled_ ? FSI_->getCellDisplacementFieldName() : "default";
bool restartFromDeformed = FSIenabled_ ? FSI_->isRestartingFromDeformed() : false;
Interface* interface = new Interface(*precice_, mesh_, interfacesConfig_.at(i).meshName, interfacesConfig_.at(i).locationsType, interfacesConfig_.at(i).patchNames, interfacesConfig_.at(i).cellSetNames, interfacesConfig_.at(i).meshConnectivity, restartFromDeformed, namePointDisplacement, nameCellDisplacement);
interfaces_.push_back(interface);
DEBUG(adapterInfo("Interface created on mesh " + interfacesConfig_.at(i).meshName));
DEBUG(adapterInfo("Adding coupling data writers..."));
for (uint j = 0; j < interfacesConfig_.at(i).writeData.size(); j++)
{
std::string dataName = interfacesConfig_.at(i).writeData.at(j);
unsigned int inModules = 0;
// Add CHT-related coupling data writers
if (CHTenabled_ && CHT_->addWriters(dataName, interface))
{
inModules++;
}
// Add FSI-related coupling data writers
if (FSIenabled_ && FSI_->addWriters(dataName, interface))
{
inModules++;
}
// Add FF-related coupling data writers
if (FFenabled_ && FF_->addWriters(dataName, interface))
{
inModules++;
}
if (inModules == 0)
{
adapterInfo("I don't know how to write \"" + dataName
+ "\". Maybe this is a typo or maybe you need to enable some adapter module?",
"error-deferred");
}
else if (inModules > 1)
{
adapterInfo("It looks like more than one modules can write \"" + dataName
+ "\" and I don't know how to choose. Try disabling one of the modules.",
"error-deferred");
}
// NOTE: Add any coupling data writers for your module here.
} // end add coupling data writers
DEBUG(adapterInfo("Adding coupling data readers..."));
for (uint j = 0; j < interfacesConfig_.at(i).readData.size(); j++)
{
std::string dataName = interfacesConfig_.at(i).readData.at(j);
unsigned int inModules = 0;
// Add CHT-related coupling data readers
if (CHTenabled_ && CHT_->addReaders(dataName, interface)) inModules++;
// Add FSI-related coupling data readers
if (FSIenabled_ && FSI_->addReaders(dataName, interface)) inModules++;
// Add FF-related coupling data readers
if (FFenabled_ && FF_->addReaders(dataName, interface)) inModules++;
if (inModules == 0)
{
adapterInfo("I don't know how to read \"" + dataName
+ "\". Maybe this is a typo or maybe you need to enable some adapter module?",
"error-deferred");
}
else if (inModules > 1)
{
adapterInfo("It looks like more than one modules can read \"" + dataName
+ "\" and I don't know how to choose. Try disabling one of the modules.",
"error-deferred");
}
// NOTE: Add any coupling data readers for your module here.
} // end add coupling data readers
// Create the interface's data buffer
interface->createBuffer();
}
ACCUMULATE_TIMER(timeInMeshSetup_);
// Initialize preCICE and exchange the first coupling data
initialize();
// If checkpointing is required, specify the checkpointed fields
// and write the first checkpoint
if (requiresWritingCheckpoint())
{
checkpointing_ = true;
// Setup the checkpointing (find and add fields to checkpoint)
setupCheckpointing();
// Write checkpoint (for the first iteration)
writeCheckpoint();
}
// Adjust the timestep for the first iteration, if it is fixed
if (!adjustableTimestep_)
{
adjustSolverTimeStepAndReadData();
}
// If the solver tries to end before the coupling is complete,
// e.g. because the solver's endTime was smaller or (in implicit
// coupling) equal with the max-time specified in preCICE,
// problems may occur near the end of the simulation,
// as the function object may be called only once near the end.
// See the implementation of Foam::Time::run() for more details.
// To prevent this, we set the solver's endTime to "infinity"
// and let only preCICE control the end of the simulation.
// This has the side-effect of not triggering the end() method
// in any function object normally. Therefore, we trigger it
// when preCICE dictates to stop the coupling.
adapterInfo(
"Setting the solver's endTime to infinity to prevent early exits. "
"Only preCICE will control the simulation's endTime. "
"Any functionObject's end() method will be triggered by the adapter. "
"You may disable this behavior in the adapter's configuration.",
"info");
const_cast<Time&>(runTime_).setEndTime(GREAT);
}
catch (const Foam::error& e)
{
adapterInfo(e.message(), "error-deferred");
errorsInConfigure = true;
}
return;
}
void preciceAdapter::Adapter::execute()
{
if (errorsInConfigure)
{
// Handle any errors during configure().
// See the comments in configure() for details.
adapterInfo(
"There was a problem while configuring the adapter. "
"See the log for details.",
"error");
}
// The solver has already solved the equations for this timestep.
// Now call the adapter's methods to perform the coupling.
// TODO add a function which checks if all fields are checkpointed.
// if (ncheckpointed is nregisterdobjects. )
// Write the coupling data in the buffer
writeCouplingData();
// Advance preCICE
advance();
// Read checkpoint if required
if (requiresReadingCheckpoint())
{
readCheckpoint();
}
// Write checkpoint if required
if (requiresWritingCheckpoint())
{
writeCheckpoint();
}
// As soon as OpenFOAM writes the results, it will not try to write again
// if the time takes the same value again. Therefore, during an implicit
// coupling, we write again when the coupling timestep is complete.
// Check the behavior e.g. by using watch on a result file:
// watch -n 0.1 -d ls --full-time Fluid/0.01/T.gz
SETUP_TIMER();
if (checkpointing_ && isCouplingTimeWindowComplete())
{
// Check if the time directory already exists
// (i.e. the solver wrote results that need to be updated)
if (runTime_.timePath().type() == fileName::DIRECTORY)
{
adapterInfo(
"The coupling timestep completed. "
"Writing the updated results.",
"info");
const_cast<Time&>(runTime_).writeNow();
}
}
ACCUMULATE_TIMER(timeInWriteResults_);
// Adjust the timestep, if it is fixed
if (!adjustableTimestep_)
{
adjustSolverTimeStepAndReadData();
}
// If the coupling is not going to continue, tear down everything
// and stop the simulation.
if (!isCouplingOngoing())
{
adapterInfo("The coupling completed.", "info");
// Finalize the preCICE solver interface and delete data
finalize();
// Tell OpenFOAM to stop the simulation.
// Set the solver's endTime to now. The next evaluation of
// runTime.run() will be false and the solver will exit.
const_cast<Time&>(runTime_).setEndTime(runTime_.value());
adapterInfo(
"The simulation was ended by preCICE. "
"Calling the end() methods of any functionObject explicitly.",
"info");
adapterInfo("Great that you are using the OpenFOAM-preCICE adapter! "
"Next to the preCICE library and any other components, please also cite this adapter. "
"Find how on https://precice.org/adapter-openfoam-overview.html.",
"info");
const_cast<Time&>(runTime_).functionObjects().end();
}
return;
}
void preciceAdapter::Adapter::adjustTimeStep()
{
adjustSolverTimeStepAndReadData();
return;
}
void preciceAdapter::Adapter::readCouplingData(double relativeReadTime)
{
SETUP_TIMER();
DEBUG(adapterInfo("Reading coupling data..."));
for (uint i = 0; i < interfaces_.size(); i++)
{
interfaces_.at(i)->readCouplingData(relativeReadTime);
}
ACCUMULATE_TIMER(timeInRead_);
return;
}
void preciceAdapter::Adapter::writeCouplingData()
{
SETUP_TIMER();
DEBUG(adapterInfo("Writing coupling data..."));
for (uint i = 0; i < interfaces_.size(); i++)
{
interfaces_.at(i)->writeCouplingData();
}
ACCUMULATE_TIMER(timeInWrite_);
return;
}
void preciceAdapter::Adapter::initialize()
{
DEBUG(adapterInfo("Initializing the preCICE solver interface..."));
SETUP_TIMER();
if (precice_->requiresInitialData())
writeCouplingData();
DEBUG(adapterInfo("Initializing preCICE data..."));
precice_->initialize();
preciceInitialized_ = true;
ACCUMULATE_TIMER(timeInInitialize_);
adapterInfo("preCICE was configured and initialized", "info");
return;
}
void preciceAdapter::Adapter::finalize()
{
if (NULL != precice_ && preciceInitialized_ && !isCouplingOngoing())
{
DEBUG(adapterInfo("Finalizing the preCICE solver interface..."));
// Finalize the preCICE solver interface
SETUP_TIMER();
precice_->finalize();
ACCUMULATE_TIMER(timeInFinalize_);
preciceInitialized_ = false;
// Delete the solver interface and all the related data
teardown();
}
else
{
adapterInfo("Could not finalize preCICE.", "error");
}
return;
}
void preciceAdapter::Adapter::advance()
{
DEBUG(adapterInfo("Advancing preCICE..."));
SETUP_TIMER();
precice_->advance(timestepSolver_);
ACCUMULATE_TIMER(timeInAdvance_);
return;
}
void preciceAdapter::Adapter::adjustSolverTimeStepAndReadData()
{
DEBUG(adapterInfo("Adjusting the solver's timestep..."));
// The timestep size that the solver has determined that it wants to use
double timestepSolverDetermined;
/* In this method, the adapter overwrites the timestep used by OpenFOAM.
If the timestep is not adjustable, OpenFOAM will not try to re-estimate
the timestep or read it again from the controlDict. Therefore, store
the value that the timestep has is the beginning and try again to use this
in every iteration.
// TODO Treat also the case where the user modifies the timestep
// in the controlDict during the simulation.
*/
// Is the timestep adjustable or fixed?
if (!adjustableTimestep_)
{
// Have we already stored the timestep?
if (!useStoredTimestep_)
{
// Show a warning if runTimeModifiable is set
if (runTime_.runTimeModifiable())
{
adapterInfo(
"You have enabled 'runTimeModifiable' in the "
"controlDict. The preciceAdapter does not yet "
"fully support this functionality when "
"'adjustableTimestep' is not enabled. "
"If you modify the 'deltaT' in the controlDict "
"during the simulation, it will not be updated.",
"warning");
}
// Store the value
timestepStored_ = runTime_.deltaT().value();
// Ok, we stored it once, we will use this from now on
useStoredTimestep_ = true;
}
// Use the stored timestep as the determined solver's timestep
timestepSolverDetermined = timestepStored_;
}
else
{
// The timestep is adjustable, so OpenFOAM will modify it
// and therefore we can use the updated value
timestepSolverDetermined = runTime_.deltaT().value();
}
/* If the solver tries to use a timestep smaller than the one determined
by preCICE, that means that the solver is trying to subcycle.
This may not be allowed by the user.
If the solver tries to use a bigger timestep, then it needs to use
the same timestep as the one determined by preCICE.
*/
double tolerance = 1e-14;
if (precice_->getMaxTimeStepSize() - timestepSolverDetermined > tolerance)
{
// Add a bool 'subCycling = true' which is checked in the storeMeshPoints() function.
adapterInfo(
"The solver's timestep is smaller than the "
"coupling timestep. Subcycling...",
"info");
timestepSolver_ = timestepSolverDetermined;
// TODO subcycling is enabled. For FSI the oldVolumes must be written, which is normally not done.
if (FSIenabled_)
{
adapterInfo(
"The adapter does not fully support subcycling for FSI and instabilities may occur.",
"warning");
}
}
else if (timestepSolverDetermined - precice_->getMaxTimeStepSize() > tolerance)
{
// In the last time-step, we adjust to dt = 0, but we don't need to trigger the warning here
if (precice_->isCouplingOngoing())
{
adapterInfo(
"The solver's timestep cannot be larger than the coupling timestep."
" Adjusting from "
+ std::to_string(timestepSolverDetermined) + " to " + std::to_string(precice_->getMaxTimeStepSize()),
"warning");
}
timestepSolver_ = precice_->getMaxTimeStepSize();
}
else
{
DEBUG(adapterInfo("The solver's timestep is the same as the "
"coupling timestep."));
timestepSolver_ = precice_->getMaxTimeStepSize();
}
// Update the solver's timestep (but don't trigger the adjustDeltaT(),
// which also triggers the functionObject's adjustTimeStep())
// TODO: Keep this in mind if any relevant problem appears.
const_cast<Time&>(runTime_).setDeltaT(timestepSolver_, false);
DEBUG(adapterInfo("Reading coupling data associated to the calculated time-step size..."));
// Read the received coupling data from the buffer
// Fits to an implicit Euler
readCouplingData(runTime_.deltaT().value());
return;
}
bool preciceAdapter::Adapter::isCouplingOngoing()
{
bool isCouplingOngoing = false;
// If the coupling ends before the solver ends,
// the solver would try to access this method again,
// giving a segmentation fault if precice_
// was not available.
if (NULL != precice_)
{
isCouplingOngoing = precice_->isCouplingOngoing();
}
return isCouplingOngoing;
}
bool preciceAdapter::Adapter::isCouplingTimeWindowComplete()
{
return precice_->isTimeWindowComplete();
}
bool preciceAdapter::Adapter::requiresReadingCheckpoint()
{
return precice_->requiresReadingCheckpoint();
}
bool preciceAdapter::Adapter::requiresWritingCheckpoint()
{
return precice_->requiresWritingCheckpoint();
}
void preciceAdapter::Adapter::storeCheckpointTime()
{
couplingIterationTimeIndex_ = runTime_.timeIndex();
couplingIterationTimeValue_ = runTime_.value();
DEBUG(adapterInfo("Stored time value t = " + std::to_string(runTime_.value())));
return;
}
void preciceAdapter::Adapter::reloadCheckpointTime()
{
const_cast<Time&>(runTime_).setTime(couplingIterationTimeValue_, couplingIterationTimeIndex_);
// TODO also reset the current iteration?!
DEBUG(adapterInfo("Reloaded time value t = " + std::to_string(runTime_.value())));
return;
}
void preciceAdapter::Adapter::storeMeshPoints()
{
DEBUG(adapterInfo("Storing mesh points..."));
// TODO: In foam-extend, we would need "allPoints()". Check if this gives the same data.
meshPoints_ = mesh_.points();
oldMeshPoints_ = mesh_.oldPoints();
/*
// TODO This is only required for subcycling. It should not be called when not subcycling!!
// Add a bool 'subcycling' which can be evaluated every timestep.
if ( !oldVolsStored && mesh_.foundObject<volScalarField::Internal>("V00") ) // For Ddt schemes which use one previous timestep
{
setupMeshVolCheckpointing();
oldVolsStored = true;
}
// Update any volume fields from the buffer to the checkpointed values (if already exists.)
*/
DEBUG(adapterInfo("Stored mesh points."));
if (mesh_.moving())
{
if (!meshCheckPointed)
{
// Set up the checkpoint for the mesh flux: meshPhi
setupMeshCheckpointing();
meshCheckPointed = true;
}
writeMeshCheckpoint();
writeVolCheckpoint(); // Does not write anything unless subcycling.
}
}
void preciceAdapter::Adapter::reloadMeshPoints()
{
if (!mesh_.moving())
{
DEBUG(adapterInfo("Mesh points not moved as the mesh is not moving"));
return;
}
// In Foam::polyMesh::movePoints.
// TODO: The function movePoints overwrites the pointer to the old mesh.
// Therefore, if you revert the mesh, the oldpointer will be set to the points, which are the new values.
DEBUG(adapterInfo("Moving mesh points to their previous locations..."));
// TODO
// Switch oldpoints on for pure physics. (is this required?). Switch off for better mesh deformation capabilities?
// const_cast<pointField&>(mesh_.points()) = oldMeshPoints_;
const_cast<fvMesh&>(mesh_).movePoints(meshPoints_);
DEBUG(adapterInfo("Moved mesh points to their previous locations."));
// TODO The if statement can be removed in this case, but it is still included for clarity
if (meshCheckPointed)
{
readMeshCheckpoint();
}
/* // TODO This part should only be used when sybcycling. See the description in 'storeMeshPoints()'
// The if statement can be removed in this case, but it is still included for clarity
if ( oldVolsStored )
{
readVolCheckpoint();
}
*/
}
void preciceAdapter::Adapter::setupMeshCheckpointing()
{
// The other mesh <type>Fields:
// C
// Cf
// Sf
// magSf
// delta
// are updated by the function fvMesh::movePoints. Only the meshPhi needs checkpointing.
DEBUG(adapterInfo("Creating a list of the mesh checkpointed fields..."));
// Add meshPhi to the checkpointed fields
addMeshCheckpointField(
const_cast<surfaceScalarField&>(
mesh_.phi()));
#ifdef ADAPTER_DEBUG_MODE
adapterInfo(
"Added " + mesh_.phi().name() + " in the list of checkpointed fields.");
#endif
}
void preciceAdapter::Adapter::setupMeshVolCheckpointing()
{
DEBUG(adapterInfo("Creating a list of the mesh volume checkpointed fields..."));
// Add the V0 and the V00 to the list of checkpointed fields.
// For V0
addVolCheckpointField(
const_cast<volScalarField::Internal&>(
mesh_.V0()));
#ifdef ADAPTER_DEBUG_MODE
adapterInfo(
"Added " + mesh_.V0().name() + " in the list of checkpointed fields.");
#endif
// For V00
addVolCheckpointField(
const_cast<volScalarField::Internal&>(
mesh_.V00()));
#ifdef ADAPTER_DEBUG_MODE
adapterInfo(
"Added " + mesh_.V00().name() + " in the list of checkpointed fields.");
#endif
// Also add the buffer fields.
// TODO For V0
/* addVolCheckpointFieldBuffer
(
const_cast<volScalarField::Internal&>
(
mesh_.V0()
)
); */
#ifdef ADAPTER_DEBUG_MODE
adapterInfo(
"Added " + mesh_.V0().name() + " in the list of buffer checkpointed fields.");
#endif
// TODO For V00
/* addVolCheckpointFieldBuffer
(
const_cast<volScalarField::Internal&>
(
mesh_.V00()
)
);*/
#ifdef ADAPTER_DEBUG_MODE
adapterInfo(
"Added " + mesh_.V00().name() + " in the list of buffer checkpointed fields.");
#endif
}
void preciceAdapter::Adapter::setupCheckpointing()
{
SETUP_TIMER();
// Add fields in the checkpointing list - sorted for parallel consistency
DEBUG(adapterInfo("Adding in checkpointed fields..."));
#undef doLocalCode
#define doLocalCode(GeomField) \
/* Checkpoint registered GeomField objects */ \
for (const word& obj : mesh_.sortedNames<GeomField>()) \
{ \
addCheckpointField(mesh_.thisDb().getObjectPtr<GeomField>(obj)); \
DEBUG(adapterInfo("Checkpoint " + obj + " : " #GeomField)); \
}
doLocalCode(volScalarField);
doLocalCode(volVectorField);
doLocalCode(volTensorField);
doLocalCode(volSymmTensorField);
doLocalCode(surfaceScalarField);
doLocalCode(surfaceVectorField);
doLocalCode(surfaceTensorField);
doLocalCode(pointScalarField);
doLocalCode(pointVectorField);
doLocalCode(pointTensorField);
// NOTE: Add here other object types to checkpoint, if needed.
#undef doLocalCode
ACCUMULATE_TIMER(timeInCheckpointingSetup_);
}
// All mesh checkpointed fields
void preciceAdapter::Adapter::addMeshCheckpointField(surfaceScalarField& field)
{
{
meshSurfaceScalarFields_.push_back(&field);
meshSurfaceScalarFieldCopies_.push_back(new surfaceScalarField(field));
}
}
void preciceAdapter::Adapter::addMeshCheckpointField(surfaceVectorField& field)
{
{
meshSurfaceVectorFields_.push_back(&field);
meshSurfaceVectorFieldCopies_.push_back(new surfaceVectorField(field));
}
}
void preciceAdapter::Adapter::addMeshCheckpointField(volVectorField& field)
{
{
meshVolVectorFields_.push_back(&field);
meshVolVectorFieldCopies_.push_back(new volVectorField(field));
}
}
// TODO Internal field for the V0 (volume old) and V00 (volume old-old) fields
void preciceAdapter::Adapter::addVolCheckpointField(volScalarField::Internal& field)
{
{
volScalarInternalFields_.push_back(&field);
volScalarInternalFieldCopies_.push_back(new volScalarField::Internal(field));
}
}
void preciceAdapter::Adapter::addCheckpointField(volScalarField* field)
{
if (field)
{
volScalarFields_.push_back(field);
volScalarFieldCopies_.push_back(new volScalarField(*field));
}
}
void preciceAdapter::Adapter::addCheckpointField(volVectorField* field)
{
if (field)
{
volVectorFields_.push_back(field);
volVectorFieldCopies_.push_back(new volVectorField(*field));
}
}
void preciceAdapter::Adapter::addCheckpointField(surfaceScalarField* field)
{
if (field)
{
surfaceScalarFields_.push_back(field);
surfaceScalarFieldCopies_.push_back(new surfaceScalarField(*field));
}
}
void preciceAdapter::Adapter::addCheckpointField(surfaceVectorField* field)
{
if (field)
{
surfaceVectorFields_.push_back(field);
surfaceVectorFieldCopies_.push_back(new surfaceVectorField(*field));
}
}
void preciceAdapter::Adapter::addCheckpointField(pointScalarField* field)
{