dsblock3.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Begin dsblock3.c
  2. File version: 1.3, 1998-10-29 */
  3. /*
  4. * Copyright (C) 1997-2001 Dynasim AB.
  5. * All rights reserved.
  6. *
  7. */
  8. if (Init_) {
  9. int i1_;
  10. for (i1_ = 1; i1_ <= nrel_; i1_++) {
  11. QZold_[2*i1_-2] = 1;
  12. QZold_[2*i1_-1] = 1;
  13. QZ_[2*i1_-2] = 1;
  14. QZ_[2*i1_-1] = 1;
  15. }
  16. }
  17. if (Init_) {
  18. int i1_;
  19. for (i1_=0; i1_<sizeof(QPre_)/sizeof(QPre_[0]); i1_++) {
  20. RefPre_[i1_] = 0;
  21. QPre_[i1_] = 0;
  22. }
  23. }
  24. if (DymolaOneIteration_<3) Iter = 0;
  25. if (did_->DymolaEventOptional_var && *idemand_==5 && RootFinder_ ) {
  26. if (DYNTime<NextTimeEvent && !triggerStepEvent_) {
  27. DYNEvent=false;
  28. *idemand_=4;
  29. Iter=1;
  30. }
  31. }
  32. if (DymolaOneIteration_==5) goto iterate2;
  33. iterateEvent:
  34. if (DYNEvent) {
  35. NextTimeEvent=1e100;
  36. did_->HaveEventIterated_var=true;
  37. }
  38. if (*idemand_ == 4 || (DYNEvent && DymolaOneIteration_!=3 && DymolaOneIteration_!=4)) {
  39. int i1_;
  40. for (i1_ = 1; i1_ <= nrel_; i1_++) Qenable_[i1_] = false;
  41. }
  42. if (DYNEvent) {
  43. ClearNextSample(did_);
  44. }
  45. iterate:
  46. DYNHReject=0;
  47. if (DYNEvent) ++dymolaEventsNr_;
  48. if ((Iter%11)==10) {
  49. int i1_;
  50. for (i1_ = 1; i1_ <= nrel_; i1_++) Qenable_[i1_] = false;
  51. }
  52. PerformIteration_ = false;
  53. Iter = Iter + 1;
  54. AnyEvent_ = false;
  55. AnyEventParameter_ = false;
  56. #if defined(DYNCALLFMUREINIT)
  57. DelayedUpdateReinit(time, X_, W_, U_, Y_, did_);
  58. #endif
  59. iterate2:
  60. equations_(idemand_, icall_,
  61. time, X_, XD_, U_,
  62. DP_, IP_, LP_,
  63. F_, Y_, W_, QZ_,
  64. duser_, iuser_, cuser_, did_,
  65. QiErr, tid);
  66. if (*QiErr>=1 && *QiErr<=4 && DYNTime<=InitTime && Iter<=1 && DymolaHomotopyUsed && !DymolaUserHomotopy) {
  67. if (ResetCounter_!=did_->oldReset_var) {
  68. did_->oldReset_var=ResetCounter_;
  69. if (!(PrintEvent&(1<<10))) DymosimMessage("Trying to solve non-linear system using global homotopy-method.");
  70. DymolaUserHomotopy=1;
  71. *QiErr=-997;
  72. }
  73. }
  74. if (*QiErr==-997) {
  75. if(DymolaUserHomotopy && DymolaHomotopyLambda!=0) {
  76. DymolaHomotopyLambda=0;
  77. UpdateInitVars(time, X_, XD_, U_, DP_, IP_, LP_,
  78. F_, Y_, W_, QZ_, duser_, iuser_, cuser_, did_);
  79. GlobalError_=0;
  80. *QiErr=0;
  81. goto iterate2;
  82. }
  83. } else if (*QiErr==-996) {
  84. if (DymolaUserHomotopy) {
  85. GlobalError_=0;
  86. *QiErr=0;
  87. if (DymolaHomotopyLambda<1) {
  88. if (DymolaHomotopyLambdaDelta<0.08 && DymolaHomotopyLambda>DymolaHomotopyLambdaFail+DymolaHomotopyLambdaDelta*6)
  89. DymolaHomotopyLambdaDelta*=2;
  90. DymolaHomotopyLambda+=DymolaHomotopyLambdaDelta;
  91. if (DymolaHomotopyLambda>1-1e-9) DymolaHomotopyLambda=1;
  92. DYNHReject=0;
  93. UpdateInitVars(time, X_, XD_, U_, DP_, IP_, LP_,
  94. F_, Y_, W_, QZ_, duser_, iuser_, cuser_, did_);
  95. goto iterate2;
  96. } else DymolaUserHomotopy=0;
  97. }
  98. } else if (*QiErr!=0 && DymolaUserHomotopy) {
  99. if (DymolaHomotopyLambda<=0) DymosimMessage("Error: could not solve simplified initialization for homotopy method.");
  100. else if (DymolaHomotopyLambdaDelta<=1e-4) {
  101. if (DymolaHomotopyLambdaFail>0)
  102. DymosimMessage("Error: adaptive homotopy method got stuck after starting. Can set scripting flag Advanced.DebugHomotopy=true; simulate again, and open continuation.csv to investigate");
  103. else
  104. DymosimMessage("Error: adaptive homotopy method could not start using actual - check that actual and simplified arguments are similar.");
  105. } else {
  106. GlobalError_=0;
  107. *QiErr=0;
  108. DymolaHomotopyLambda-=DymolaHomotopyLambdaDelta;
  109. DymolaHomotopyLambdaDelta*=0.5;
  110. DymolaHomotopyLambdaFail=DymolaHomotopyLambda;
  111. DymolaHomotopyLambda+=DymolaHomotopyLambdaDelta;
  112. DymosimMessage("Error: reducing step size for homotopy.");
  113. DYNHReject=1;
  114. UpdateInitVars(time, X_, XD_, U_, DP_, IP_, LP_,
  115. F_, Y_, W_, QZ_, duser_, iuser_, cuser_, did_);
  116. goto iterate2;
  117. }
  118. }
  119. if (PerformIteration_)
  120. goto iterate;
  121. if (DYNEvent) {
  122. /* End dsblock3.c */