<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://ascend4.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ksenija</id>
	<title>ASCEND - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://ascend4.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Ksenija"/>
	<link rel="alternate" type="text/html" href="https://ascend4.org/Special:Contributions/Ksenija"/>
	<updated>2026-04-28T20:37:08Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.6</generator>
	<entry>
		<id>https://ascend4.org/index.php?title=User:RaymondJoseph&amp;diff=5403</id>
		<title>User:RaymondJoseph</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:RaymondJoseph&amp;diff=5403"/>
		<updated>2015-09-08T01:37:22Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: A bit of formatting&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hi There!&lt;br /&gt;
&lt;br /&gt;
Thanks for stopping by.&lt;br /&gt;
&lt;br /&gt;
I&#039;m Harry Raymond Joseph, a master&#039;s student in EECS at IIT Madras, India. This page is intended to serve as a journal to keep track of my development work at Ascend as part of GSoC 2015. Updates and some relevant aspects of the GSoC proposal I submitted to Ascend are made available below. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Final Report on the Project==&lt;br /&gt;
&lt;br /&gt;
===About the Project===&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
The project was to better organize the entire ida solver module in Ascend. Specific sub-tasks included:&lt;br /&gt;
&lt;br /&gt;
  a) Writing a separate function to pre-process the integrator. Before the project this was packaged as part of the ida.c file.&lt;br /&gt;
  b) Work on ida.c and make it a lot more readable and understandable.&lt;br /&gt;
  c) Handle the events syntax, with particular requirements and constraints on time-steps after roots.  &lt;br /&gt;
  d) An overall goal was to make the code handling the solvers/ida moule compact.&lt;br /&gt;
&lt;br /&gt;
===The Hows===&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
Created process_integrator.c&lt;br /&gt;
&lt;br /&gt;
1. Initially, as already mentioned preparing the integrator and allocating memory to it was actually part of the ida.c module. This made it slightly more complicated and difficult to understand the main module. The separate prepare_integrator that was created has the following functionality:&lt;br /&gt;
&lt;br /&gt;
2. Allocate ida_mem : This is done by use of IDA_MALLOC.&lt;br /&gt;
&lt;br /&gt;
3. Pass the integrator configuration settings: several integration configuration settings (eg. Pass tolerance settings, type of solver, etc) need to be obtained from the user and passed as input to the IDA, these are accomplished by using a variety of functions, as listed in the IDA manula.&lt;br /&gt;
&lt;br /&gt;
4. Analyse the equations  - An initial analysis of the equations is completed – for instance checking solvability, etc.&lt;br /&gt;
&lt;br /&gt;
5. configure IDA (set up the sufficient memory for y, y_d etc vectors)&lt;br /&gt;
&lt;br /&gt;
Created Process_events_and_whens.c&lt;br /&gt;
&lt;br /&gt;
1. Process_events_and_whens.c is actually the main module that handles all the event processing and boundary checking.&lt;br /&gt;
&lt;br /&gt;
2. It returns a flag peaw (process_events_and_whens) – to the ida.c main function. Based on the value of the flag, the integrator makes several choices on the size of time-steps and direction of roots, etc.&lt;br /&gt;
&lt;br /&gt;
3. In this module, the lrslv solver is called to check for roots.&lt;br /&gt;
&lt;br /&gt;
4. Another important role of this function is that it returns root directions, so that if a boundary is crossed from one side, lrslv need not check that side for boundary crossings again. This is done by passing the array pointer by reference to the Process_events_and_whens.c function.&lt;br /&gt;
&lt;br /&gt;
Made changes to ida.c&lt;br /&gt;
&lt;br /&gt;
1. The complete configuration of the ida.c was changed. The following is a brief outline of how the new ida.c is organized:&lt;br /&gt;
&lt;br /&gt;
2. From within ida.c, the solver calls first prepare_integrator.c to make necessary memory allocations and to load integrator preferences.&lt;br /&gt;
&lt;br /&gt;
3. Once this has been done, the main solver routine calls process_events_and_whnens to solve initial conditions as well as to check for crossed boundaries.&lt;br /&gt;
&lt;br /&gt;
4. An important point to note is that this solving of initial condition happens only once within the loop – this is highly efficient, as this enabled combining together two functions – solve_initial_conditions_and_whens and process_events_and_whens.c into one single file.&lt;br /&gt;
&lt;br /&gt;
5. This was accomplished by means of the flag ‘first_run’ which is initialized to TRUE, but is set to FALSE as soon as the process_events_and_whens function is called for the first time.&lt;br /&gt;
&lt;br /&gt;
6. Now, during the subsequent calls, process_events_and_whens returns ‘peaw’, which is a flag, to ascertain the status if roots were found, and smaller integration time-steps need to be taken by the main solver routine calling process_events_and_whens.&lt;br /&gt;
&lt;br /&gt;
7. Peaw is 143 if roots have been found, if roots have been found it also reconfigures ida, before proceeding to next integration SMALLER timestep.&lt;br /&gt;
&lt;br /&gt;
8. The two cases (normal timestep and short after_root timestep) are handled in the if statements on peaw.&lt;br /&gt;
&lt;br /&gt;
9. subpeaw, is for auxiliary shorter timesteps, if there are further roots closer to the roots first found (indicated by peaw==143)&lt;br /&gt;
&lt;br /&gt;
10. Once peaw is set to 143 (root found), shorter timesteps are being taken. So integration now happens from t0 to t0+tmin instead of from t0 to tout.&lt;br /&gt;
&lt;br /&gt;
11. Now further shorter timesteps need to be taken, to check for changes - at this point we also take into account rootdir.&lt;br /&gt;
&lt;br /&gt;
12. Say, after the first root, we increment time by tmin, and we find another root - satisfying rootdir conditions, so within the loop, the integration now changes from t0+tmin*(auxcount-1) to t0+tmin*(auxcount).&lt;br /&gt;
&lt;br /&gt;
13. Once all the timesteps have been completed, the integration is complete, and the solution is returned.&lt;br /&gt;
&lt;br /&gt;
===To dos in the Project===&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
1. More testing is required to see if time-steps and changing happen properly; &lt;br /&gt;
&lt;br /&gt;
2. Process_events_and_whens needs t be properly debugged.&lt;br /&gt;
&lt;br /&gt;
3. At least the main ida.c and prepare_integrator seem to be working fine, but it might be better to add more comments within the code and perhaps search for places to make it more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Timeline==&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 13&#039;&#039;&#039; (17th July to 21st August)&lt;br /&gt;
&lt;br /&gt;
-Fixed all errors Ksenija pointed out on prepare_integrator earlier.&lt;br /&gt;
&lt;br /&gt;
-Finishing a final report for end-term review;&lt;br /&gt;
&lt;br /&gt;
-Successful in compiling working code with new main function and new prepare_integrator &lt;br /&gt;
&lt;br /&gt;
-Succeesful in building new process_events_and_whens, although with the older ida.c&lt;br /&gt;
&lt;br /&gt;
-Loaded some models but a syntax error was returned issue to be investigated.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 12&#039;&#039;&#039; (10th August to 16th August)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-Completed debugging prepare_integrator.&lt;br /&gt;
&lt;br /&gt;
-Several errors found when I started debugging process_events_and_whens with the new ida.c&lt;br /&gt;
&lt;br /&gt;
-Errors ranged from complex linking and type incompatibility errors to simple and trivial but difficult t fix beacket_unbalanced errors.&lt;br /&gt;
&lt;br /&gt;
-Also was a good time to revisit process_events_and_whens and see if the logic underlying the code was appropriate&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 11&#039;&#039;&#039; (3rd August to 9th August)&lt;br /&gt;
&lt;br /&gt;
-Received detailed feedback from Ksenija on particularly the process_events_and_whens function. In particular&lt;br /&gt;
 Ksenija pointed out the following issues:&lt;br /&gt;
&amp;quot;1. At the beginning you initialize the boundary state variables. But this function will do that *every* time &lt;br /&gt;
you call it regardless of whether it is the first call or not. I suggest putting it into an &#039;if (first_run || all_bnds_set == 0)&#039;. And I think that this&lt;br /&gt;
 boundary evaluation can be moved to a separate function - this will just be more readable.&lt;br /&gt;
&lt;br /&gt;
2. setup_lrslv is called each time when process_events_and_whens &lt;br /&gt;
is called, while it should be called only at the first run.&lt;br /&gt;
&lt;br /&gt;
3. The way you check if a boundary has been crossed is incorrect. IDA_ROOT_RETURN is a constant, &lt;br /&gt;
the actual status is returned by IDASolve and it should be compared to IDA_ROOT_RETURN.&lt;br /&gt;
&lt;br /&gt;
4. If &lt;br /&gt;
no boundary is crossed (so we are making a small step after a root), you call ida_cross_boundary and then check that it does not change the structure o&lt;br /&gt;
f a model. But ida_log_solve can change some logical variables, and some events might be triggered, but most likely only false events, which should not really &lt;br /&gt;
be called. So you don&#039;t need to call the whole ida_cross_boundary function, but only the logical solver.&lt;br /&gt;
&lt;br /&gt;
5. If a root has been found, you don&#039;t need to evaluate &lt;br /&gt;
the boundary states here.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
-Looked into each of these issues, but was unable to attend to them then, since I was still debugging prepare_intgerator&lt;br /&gt;
&lt;br /&gt;
-There was also an&lt;br /&gt;
 issue with linking new files on scons. The error detailed an unusual filename. This was because of a string concatenation mistake in the SConscripts file.&lt;br /&gt;
&lt;br /&gt;
-Further &lt;br /&gt;
re-analysed if steps being done in prepare_integrator are right; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 10&#039;&#039;&#039; (27th July to 2nd August)&lt;br /&gt;
&lt;br /&gt;
-Fixed previous errors but got stuck at an &#039;unrecognized symbol&#039; error. The error seems to be very common, but many of the fixes suggested didn&#039;t seem to work. Have requested Ksenija for help on this error.&lt;br /&gt;
&lt;br /&gt;
-Fixed many corrections suggested by Ksenija regarding process_events_.. &lt;br /&gt;
&lt;br /&gt;
-Some errors fixed include: &#039;load_rellist&#039; not defined - on running the main solver. There was a problem in linking the files. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 9&#039;&#039;&#039; (20th July to 26th July)&lt;br /&gt;
-Faced ida registration problem - Ascend GUI returns the following erroor - &#039;cannot register ida solver&#039;. Solved the problem - it was due to a wrongly commented command in the prepare_integrator.&lt;br /&gt;
&lt;br /&gt;
-Spoke at length with Ksenija, and shared most of the debugging details and progress by sharing screens on Skype.&lt;br /&gt;
&lt;br /&gt;
-Ksenija suggested I use plots to make sure that everything is alright. Plotting in Ascend requires matplotlib, faced problems getting that sorted out. &lt;br /&gt;
&lt;br /&gt;
-Continued making changes to ida.c, Ksenija pointed out a flaw in testing - was not using ida to solve the models.&lt;br /&gt;
&lt;br /&gt;
-Ran tests integrtor_idaevent and integrator_ida both returned errors so far, and I&#039;m rectifying the same.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 8&#039;&#039;&#039; (13th to 19th July)&lt;br /&gt;
-Made further changes to process_event_and_whens. &lt;br /&gt;
&lt;br /&gt;
-Tested both prepare_integrator as well as process_events_and_whens by calling these from the previous versions of the ida.c. &lt;br /&gt;
&lt;br /&gt;
-For this, I commented out several lines of code in both ida.c as well as idaboundary.c to see if it worked well - the returned values for models in the Ksenija branch were correct.&lt;br /&gt;
&lt;br /&gt;
-Continued running tests and mostly making changes to the main solver file.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 7&#039;&#039;&#039; (6th to 12th July)&lt;br /&gt;
-Continued debugging and running all tests to fully complete the integration of the codes I wrote into the Ascend framework.&lt;br /&gt;
&lt;br /&gt;
-Faced difficulties debugging. The problem was mainly due to the en bloc approach followed. Instead of debugging small pieces of code, like Ksenija advised, I ended up integrating massive code chunks (eg: ida.c) in one shot. Better code architecture planning could have helped minimize debugging trouble.&lt;br /&gt;
&lt;br /&gt;
-Completed the integration of prepare_integrator.&lt;br /&gt;
&lt;br /&gt;
-Completed the integration of process_events_and_whens.&lt;br /&gt;
&lt;br /&gt;
-Both these functions were tested, seem to be working fine.&lt;br /&gt;
&lt;br /&gt;
-Calls to these from the main function (in the ida.c file) seem to be problematic.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 6&#039;&#039;&#039; (29th June to 5th July)&lt;br /&gt;
-Changed the structure of process_events_and_whens and eliminated solve_initial conditions_and_whens.&lt;br /&gt;
&lt;br /&gt;
-process_events_and_whens will now be able to process initial conditions as well. The changed outline is as below:&lt;br /&gt;
&lt;br /&gt;
solve the logrels&lt;br /&gt;
if no values changed, &lt;br /&gt;
    return&lt;br /&gt;
if event cases were triggered (ie a positive edge):&lt;br /&gt;
    write_timestep_data&lt;br /&gt;
    write_event&lt;br /&gt;
if the event has &#039;USE&#039; statement(s), &lt;br /&gt;
    run the METHOD, &lt;br /&gt;
    run QRSlv, &lt;br /&gt;
    run the _end method, return&lt;br /&gt;
    write time_step data&lt;br /&gt;
if when cases were triggered,&lt;br /&gt;
    update the solver rel lists&lt;br /&gt;
if structure has changed (how to tell?), &lt;br /&gt;
    reanalyse lists and reconfigure IDA&lt;br /&gt;
do the above if first_run == false&lt;br /&gt;
else, its:&lt;br /&gt;
solve the logrels&lt;br /&gt;
    if no values changed, &lt;br /&gt;
        return&lt;br /&gt;
    if when cases were triggered,&lt;br /&gt;
        update the solver rel lists&lt;br /&gt;
    if structure has changed  &lt;br /&gt;
        reanalyse lists and reconfigure IDA&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 5&#039;&#039;&#039; (22nd to 28th June)&lt;br /&gt;
-Completed implementation of ida_main_solve.&lt;br /&gt;
The outline of ida_main_solve is as below:&lt;br /&gt;
&lt;br /&gt;
Boundary flags initialisation: all the condition equations are evaluated &lt;br /&gt;
    and the values of corresponding boundary flags are set.&lt;br /&gt;
Boolean variables initialisation: the system is solved using the logical &lt;br /&gt;
    equations solver in order to get the correct values of Boolean variables.&lt;br /&gt;
Using the logical values found at step 2 the configuration of the system is chosen.&lt;br /&gt;
Solver initialisation: setting solver parameters, allocating memory, et cetera.&lt;br /&gt;
Loop through all time steps:&lt;br /&gt;
    If a root has not been found at the previous iteration then then reset all &lt;br /&gt;
        the root direction settings. It means that IDA will be searching for &lt;br /&gt;
        roots of condition equations in both directions.&lt;br /&gt;
    If a root has been found and we are far from desired output then make a very &lt;br /&gt;
        small time step and solve the system with IDA. Else make a usual time &lt;br /&gt;
        step which is set by the user.&lt;br /&gt;
    If we have made a small time step then call a logical solver in order to update &lt;br /&gt;
        the values of logical variables.&lt;br /&gt;
    Check if a root has been found at this step. If yes:&lt;br /&gt;
        Call a function for processing this boundary.&lt;br /&gt;
            If there have been any changes in the system, then reinitialise the solver.&lt;br /&gt;
                Set the direction in which the roots of condition equations will be detected: &lt;br /&gt;
                if a boundary has been crossed, then at the next step (which is an &lt;br /&gt;
                    auxiliary small step) it will not be crossed in the same direction.&lt;br /&gt;
                If we are still far from desired output then repeat steps.&lt;br /&gt;
&lt;br /&gt;
So basically we have two modes here: the first is when we make an ordinary time step, check all roots and if a boundary is found then call the function for processing it. The second mode is entered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 4&#039;&#039;&#039; (15th to 21st June)&lt;br /&gt;
-Completed implementation of process_events_and_whens.&lt;br /&gt;
The outline for process_events_and_whens is as below:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
process_events_and_whens would then&lt;br /&gt;
    solve the boundary relations&lt;br /&gt;
    if no values changed, &lt;br /&gt;
        return&lt;br /&gt;
    if event cases were triggered (ie a positive edge):&lt;br /&gt;
        write_timestep_data&lt;br /&gt;
        write_event&lt;br /&gt;
    if the event has &#039;USE&#039; statement(s), &lt;br /&gt;
        run the METHOD, &lt;br /&gt;
        run QRSlv, &lt;br /&gt;
        run the _end method, return&lt;br /&gt;
        write time_step data&lt;br /&gt;
    if when cases were triggered,&lt;br /&gt;
        update the solver rel lists&lt;br /&gt;
    if structure has changed (how to tell?), &lt;br /&gt;
        reanalyse lists and reconfigure IDA&lt;br /&gt;
&lt;br /&gt;
-Started implementation of ida_main_solve, the main ida solver function.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 3&#039;&#039;&#039; (8th to 14th June)&lt;br /&gt;
- Completed implementing prepare_integrator. &lt;br /&gt;
&lt;br /&gt;
- Completed the implementation of solve_initial_conditions_and_whens.c, that&#039;s when I got stuck a little. &lt;br /&gt;
&lt;br /&gt;
- Considered changes to the implementation plan below, to try and make it more efficient.&lt;br /&gt;
&lt;br /&gt;
To do: Need to organize header files in a better way and make the overall code in the solvers/ida folder more compact.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 2&#039;&#039;&#039; (1st to 7th June)&lt;br /&gt;
&lt;br /&gt;
- Took more time to read up the IDA Sundials manual, especially the architecture of the IDA solver module.&lt;br /&gt;
&lt;br /&gt;
- Received the following implementation outline from Ksenija and John:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
integrator_ida_solve&lt;br /&gt;
    prepare_integrator&lt;br /&gt;
    solve_initial_conditions_and_whens&lt;br /&gt;
    write_timestep_data&lt;br /&gt;
    while(tsteps_remaining)&lt;br /&gt;
    integrate_to_next_step&lt;br /&gt;
    if(root triggered)&lt;br /&gt;
        process_events_and_whens&lt;br /&gt;
        advance timestep if we&#039;re really close already&lt;br /&gt;
    else:&lt;br /&gt;
        write_timestep_data&lt;br /&gt;
        advance to next timestep&lt;br /&gt;
process_events_and_whens would then&lt;br /&gt;
    solve the logrels&lt;br /&gt;
    if no values changed, return&lt;br /&gt;
    if event cases were triggered (ie a positive edge):&lt;br /&gt;
        write_timestep_data&lt;br /&gt;
        write_event&lt;br /&gt;
    if the event has &#039;USE&#039; statement(s), &lt;br /&gt;
        run the METHOD, &lt;br /&gt;
        run QRSlv, &lt;br /&gt;
        run the _end method, return&lt;br /&gt;
    write time_step data&lt;br /&gt;
    if when cases were triggered,&lt;br /&gt;
        update the solver rel lists&lt;br /&gt;
    if structure has changed (how to tell?)&lt;br /&gt;
        reanalyse lists and reconfigure IDA&lt;br /&gt;
     &lt;br /&gt;
solve_initial_conditions_and_whens would be&lt;br /&gt;
&lt;br /&gt;
    once the continuous part of the problem is solved, we need to evaluate the bnds, and then run LRSlv.&lt;br /&gt;
    If that results in triggering any changes to WHEN cases, then we should&lt;br /&gt;
        reanalyse the equations&lt;br /&gt;
        reconfigure IDA&lt;br /&gt;
        go back and solver the continuous problem again (loop)&lt;br /&gt;
    If there are too many iterations, then we should return an error to the user.&lt;br /&gt;
    we don&#039;t want to trigger any EVENTs during this process; only WHENs.&lt;br /&gt;
&lt;br /&gt;
prepare_integrator&lt;br /&gt;
    &lt;br /&gt;
    allocate ida_mem etc&lt;br /&gt;
    pass the integrator configuration settings&lt;br /&gt;
    analyse the equations&lt;br /&gt;
    configure IDA (set up the sufficient memory for y, y_d etc vectors)&lt;br /&gt;
 &lt;br /&gt;
- looking to complete before 16th June&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Week 1&#039;&#039;&#039; (25th to 31st May)&lt;br /&gt;
&lt;br /&gt;
- Setup CUnit and ran some tests&lt;br /&gt;
&lt;br /&gt;
- Spent time understanding IDA issues and implementation aspects&lt;br /&gt;
&lt;br /&gt;
- Looked at ida.c and idaboundary.c in the ksenija2 branch of the svn repository.&lt;br /&gt;
&lt;br /&gt;
- Worked on enhancing this code based on an implementation outline from John.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Community bonding period (27th April to 25th May)&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
- Installed Linux, and all other dependencies to prepare for coding.&lt;br /&gt;
&lt;br /&gt;
- Built Ascend on my 64-bit 15.04 Ubuntu system. Log [[File:attached]].&lt;br /&gt;
&lt;br /&gt;
- Reading the code in the Ksenija2 branch of the SVN repository.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==GSoC 2015 Proposal==&lt;br /&gt;
&lt;br /&gt;
Organization: ASCEND&lt;br /&gt;
&lt;br /&gt;
===Abstract:=== The IDA solver requires more refinements both in the numerical as well as user-facing aspects is desirable. One important issue is to do with EVENT syntax - which helps to model whole range of systems, in which parametric constants themselves change values with triggered events, or even more sophisticated systems in which constraint equations are added or deleted when a given event is triggered. This project attempts to strengthen the functionalities of the IDA solver.&lt;br /&gt;
(* I&#039;m using the proposal format, obtained from Carlos&#039; GSoC 2012 proposal *) &lt;br /&gt;
&lt;br /&gt;
===About:===&lt;br /&gt;
Master&#039;s student at TUM Germany/IITM India. Specializing in the numerical aspects in EECS, such as machine learning and probabilistic methods. Research enthusiast,  few publications. Amateur poet and writer. &lt;br /&gt;
&lt;br /&gt;
Email: &lt;br /&gt;
raymond.harry@tum.de/raymond.joseph.7@gmail.com&lt;br /&gt;
&lt;br /&gt;
Project Topic&lt;br /&gt;
Numerical Algorithmic Enhancement: Dynamic Modelling in ASCEND&lt;br /&gt;
&lt;br /&gt;
Basic Information&lt;br /&gt;
&lt;br /&gt;
URL link to your CV: https://drive.google.com/file/d/0B04lAfTBvrd0a1dWc3hPYnZQTkU/view?usp=sharing &lt;br /&gt;
&lt;br /&gt;
URL link to your personal blog/homepage: http://raymondjoseph7.wix.com/ctkotww&lt;br /&gt;
&lt;br /&gt;
Research page: http://dl.acm.org/author_page.cfm?id=92458619092 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Goals of the project===&lt;br /&gt;
&lt;br /&gt;
The main goal (Deliverable 1, D1) is to fully integrate IDA into ASCEND by providing and refining EVENT support syntax elements, especially ensuring that the integrator restarts efficiently after events are triggered with modified constraint equations.&lt;br /&gt;
Secondary goals are:&lt;br /&gt;
&lt;br /&gt;
- Improve IDA numerical solver efficiency. (D2)&lt;br /&gt;
&lt;br /&gt;
- Refine existing code in the \ksenija2 branch. (D3)&lt;br /&gt;
&lt;br /&gt;
- Possible GUI improvements in the use of the IDA solver. (D4)&lt;br /&gt;
&lt;br /&gt;
- Event syntax for periodic events. (D5)&lt;br /&gt;
&lt;br /&gt;
- Implement model of the simple solar water heater system as a test case. (D6)&lt;br /&gt;
&lt;br /&gt;
- Make the documentation of migration and faq for users with applications. (D7)&lt;br /&gt;
&lt;br /&gt;
===Schedule===&lt;br /&gt;
&lt;br /&gt;
Week 1: Read up and understand IDA integration issues. Great sources and discussions have been documented on the integration issues.&lt;br /&gt;
&lt;br /&gt;
Week 2: Scrub existing \ksenija2 code.&lt;br /&gt;
&lt;br /&gt;
Week 3, 4 &amp;amp; 5: Implement advanced EVENT syntax, to complete integration of IDA into ASCEND. (D1)&lt;br /&gt;
&lt;br /&gt;
Week 6: Explore possible syntax architectural changes for the IDA solver problems. (D3)&lt;br /&gt;
&lt;br /&gt;
Week 7: Read latest cutting-edge research papers and see if new methods are available to increase solver efficiency. (Hint: Yes, more optimal methods are available, that perform better than the IDA solver adopted as part of the SUNDIALS project) (D2)&lt;br /&gt;
&lt;br /&gt;
Week 8: Implement solver improvements to make the IDA solver state-of-the-art. (D2)&lt;br /&gt;
&lt;br /&gt;
Week 9: GUI improvements. (D4)&lt;br /&gt;
&lt;br /&gt;
Week 10: Periodic events support syntax. (D5) &lt;br /&gt;
&lt;br /&gt;
Week 11: Buffer week for finalizing code, and running tests. Fix any bugs discovered.&lt;br /&gt;
&lt;br /&gt;
Week 12: Implement test case: solar water heater system. (D6)&lt;br /&gt;
&lt;br /&gt;
Week 13: Documentation and FAQs. (D7)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Coding experience===&lt;br /&gt;
Programming languages you have learnt, and how many lines of code, approximately, you have written in each.&lt;br /&gt;
C++             &amp;gt;1500 LoC (Lots of numerical methods experience. Monte-carlo methods, Newton-Raphson method, ODE simulation, etc.)&lt;br /&gt;
MatLab         &amp;gt;500 LoC (Lots of experience with advanced numerical methods. Example: water-filling, convex optimization using CVX, modified gradient-descent methods, linear algebra systems in communication, etc.)&lt;br /&gt;
Java             &amp;gt;400 LoC&lt;br /&gt;
Python         &amp;gt;1000 LoC (* Though most of it is classified. I worked as a data analytics programmer at a French startup, Tilkee *)&lt;br /&gt;
&lt;br /&gt;
I also have lex/yacc experience. Used these to create my own &#039;business artifact modelling language&#039; called BASQL. Based on SQL, the language was relatively successful, a paper on this was published at the IEEE ICES conference jointly with Youakim Badr. The language achieved great success and further research on this is being carried out by Youakim Badr at LIRIS-CNRS, INSA Lyon, France.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Some of these coding samples is provided in the URL.&lt;br /&gt;
&lt;br /&gt;
MatLab: https://drive.google.com/folderview?id=0B04lAfTBvrd0fjhuLWFBNHprOXpsdlhDbU5XSmd3WnNfa3JqakdXRFRScmxIYkdfeTJKWGc&amp;amp;usp=sharing&lt;br /&gt;
&lt;br /&gt;
C++: https://drive.google.com/folderview?id=0B04lAfTBvrd0TWl2T1BBSXlFNDQ&amp;amp;usp=sharing&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Have you written any ASCEND models? Please provide copies.&lt;br /&gt;
&lt;br /&gt;
I have been able to write two successful (albeit, very simple models):&lt;br /&gt;
&lt;br /&gt;
1. The first one is on the &#039;dynamics of love affairs&#039; paper by (Felmlee and Greenberg). The model can be found here: &lt;br /&gt;
https://drive.google.com/file/d/0B04lAfTBvrd0SjU2aHZTZ2lwYkE/view?usp=sharing&lt;br /&gt;
The snapshot of the solved model is below:&lt;br /&gt;
https://drive.google.com/file/d/0B04lAfTBvrd0RmRmTzh5MVQ2WmM/view?usp=sharing&lt;br /&gt;
&lt;br /&gt;
2. The second model is the simple non-flow domestic solar water heater system. The model can be found here: &lt;br /&gt;
https://drive.google.com/file/d/0B04lAfTBvrd0SjU2aHZTZ2lwYkE/view?usp=sharing&lt;br /&gt;
The snapshot of the solved model is below: &lt;br /&gt;
https://drive.google.com/file/d/0B04lAfTBvrd0TERrcG5nbS1kckU/view?usp=sharing&lt;br /&gt;
&lt;br /&gt;
[[Category:GSOC2015]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Derinfo.png&amp;diff=4724</id>
		<title>File:Derinfo.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Derinfo.png&amp;diff=4724"/>
		<updated>2014-09-14T18:38:37Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Derinfo.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Use_der_syntax.png&amp;diff=4723</id>
		<title>File:Use der syntax.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Use_der_syntax.png&amp;diff=4723"/>
		<updated>2014-09-14T18:38:22Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Use der syntax.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Huygens_clock.png&amp;diff=4722</id>
		<title>File:Huygens clock.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Huygens_clock.png&amp;diff=4722"/>
		<updated>2014-09-14T18:36:58Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Huygens clock.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events3.png&amp;diff=4721</id>
		<title>File:Plot events3.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events3.png&amp;diff=4721"/>
		<updated>2014-09-14T18:36:20Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Plot events3.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events2.png&amp;diff=4720</id>
		<title>File:Plot events2.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events2.png&amp;diff=4720"/>
		<updated>2014-09-14T18:36:05Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Plot events2.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events1.png&amp;diff=4719</id>
		<title>File:Plot events1.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events1.png&amp;diff=4719"/>
		<updated>2014-09-14T18:35:10Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Plot events1.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Vessel_with_siphon.png&amp;diff=4718</id>
		<title>File:Vessel with siphon.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Vessel_with_siphon.png&amp;diff=4718"/>
		<updated>2014-09-14T18:34:20Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Vessel with siphon.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Hysteron.png&amp;diff=4717</id>
		<title>File:Hysteron.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Hysteron.png&amp;diff=4717"/>
		<updated>2014-09-14T18:33:28Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: Ksenija uploaded a new version of &amp;amp;quot;File:Hysteron.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=Publications&amp;diff=4628</id>
		<title>Publications</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=Publications&amp;diff=4628"/>
		<updated>2014-07-07T15:13:19Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Note: not all the PDF links are present in the following text. If a link is missing, please using the original [http://www.ascend4.org/ascend_bibliography.htm ASCEND bibliography] page to find the document.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
In date order, divided according to category.&lt;br /&gt;
&lt;br /&gt;
== Doctoral theses ==&lt;br /&gt;
&lt;br /&gt;
V Rico-Ramirez, 1998. &#039;&#039;Representation, Analysis and Solution of Conditional Models in an Equation-Based Environment&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/victhesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
K A Abbott, 1996. &#039;&#039;Very Large Scale Modeling&#039;&#039; Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/kirkthesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
B A Allan, 1997. &#039;&#039;A More Reusable Modeling System&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/benthesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
B T Safrit, 1996. &#039;&#039;Synthesis of Azeotropic Batch Distillation Separation Systems&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/safritthesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
M Thomas, 1996. &#039;&#039;Tool and Information Management in Engineering Design&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/markthesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
R S Huss, 1995. &#039;&#039;Collocation Methods For Flexible Distillation Design&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/bobthesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
J Zaher, 1995. &#039;&#039;Conditional Modeling&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfThesis/joethesis.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
P Piela, 1989. &#039;&#039;ASCEND: An Object-Oriented Computer Environment for Modeling and Analysis&#039;&#039;, Ph.D. Thesis, Carnegie Mellon University, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/htbibliography/b9.html Abstract]&lt;br /&gt;
&lt;br /&gt;
== Journal papers ==&lt;br /&gt;
&lt;br /&gt;
A W Westerberg, 2003, &#039;&#039;A retrospective on design and process synthesis&#039;&#039;, Computers &amp;amp; Chemical Engineering, &#039;&#039;&#039;28&#039;&#039;&#039; (4), 447-458. {{doi|10.1016/j.compchemeng.2003.09.029}}.&lt;br /&gt;
&lt;br /&gt;
V Rico-Ramírez, B A Allan and A W Westerberg, 1999, &#039;&#039;Conditional Modeling. 1. Requirements for an Equation-Based Environment&#039;&#039;, Ind. Eng. Chem. Res., 38 (2), pp 519–530. {{doi|10.1021/ie9800593}}&lt;br /&gt;
&lt;br /&gt;
V Rico-Ramírez and A W Westerberg, 1999, &#039;&#039;Conditional Modeling. 2. Solving Using Complementarity and Boundary-Crossing Formulations&#039;&#039;, Ind. Eng. Chem. Res., 38 (2), pp 531–553. {{doi|10.1021/ie9800602}}&lt;br /&gt;
&lt;br /&gt;
Benjamin A Allan and Arthur W Westerberg, 1999, &#039;&#039;Anonymous Class in Declarative Process Modeling&#039;&#039;, Ind. Eng. Chem. Res., &#039;&#039;&#039;38&#039;&#039;&#039; (3), pp 692–704. {{doi|10.1021/ie980297y}}&lt;br /&gt;
&lt;br /&gt;
Kirk A Abbott, Benjamin A Allan, and Arthur W Westerberg, 1997, &#039;&#039;Global preordering for Newton equations using model hierarchy&#039;&#039;, AIChE Journal &#039;&#039;&#039;43&#039;&#039;&#039; (12), {{doi|10.1002/aic.690431207}}&lt;br /&gt;
&lt;br /&gt;
Robert S Huss and Arthur W Westerberg, 1996, &#039;&#039;Collocation Methods for Distillation Design. 2. Applications for Distillation&#039;&#039;, Ind. Eng. Chem. Res., &#039;&#039;&#039;35&#039;&#039;&#039; (5), pp 1611–1623, {{doi|10.1021/ie9503508}}&lt;br /&gt;
&lt;br /&gt;
B T Safrit, A W Westerberg, U Diwekar, O M Wahnschafft, 1995, &#039;&#039;Extending Continuous Conventional and Extractive Distillation Feasibility Insights to Batch Distillation&#039;&#039;, Ind. Eng. Chem. Res., 34, 3257-3264. {{doi|10.1021/ie00037a012}}&lt;br /&gt;
&lt;br /&gt;
B Katzenberg and P Piela, 1993, &#039;&#039;Work Language Analysis and the Naming Problem&#039;&#039;, Communication of the ACM, Vol. 36, No. 6, 86-92. {{doi|10.1145/153571.163286}}&lt;br /&gt;
&lt;br /&gt;
P Piela, R McKelvey, and A Westerberg, 1993, &#039;&#039;An Introduction to the ASCEND Modeling System: Its Language and Interactive Environment&#039;&#039;, ; Journal of Management Information Systems ,Vol. 9, No.3, 91-121. [http://ascend.cheme.cmu.edu/ftp/htbibliography/b6.html Abstract], [http://www.jstor.org/stable/40398044 Full article]&lt;br /&gt;
&lt;br /&gt;
P Piela, B Katzenberg, and R McKelvey, 1992, &#039;&#039;Integrating the User into Research on Engineering Design Systems ASCEND: An Object-Oriented Computer Environment for Modeling&#039;&#039;, Research in Engineering Design, Vol. 3, 211-221. {{doi|10.1007/BF01580843}}&lt;br /&gt;
&lt;br /&gt;
Oliver J Smith IV and Arthur W Westerberg, 1991, &#039;&#039;The optimal design of pressure swing adsorption systems&#039;&#039;, Chemical Engineering Science, &#039;&#039;&#039;48&#039;&#039; (12), {{doi|10.1016/0009-2509(91)85001-E}}&lt;br /&gt;
&lt;br /&gt;
P Piela, T Epperly, K Westerberg and A Westerberg, 1991, &#039;&#039;ASCEND: An Object-Oriented Computer Environment for Modeling and Analysis: The Modeling Language.&#039;&#039;, Computers and Chemical Engineering, Vol. 15, No. 1, 53-72, . {{doi|10.1016/0098-1354(91)87006-U}}&lt;br /&gt;
&lt;br /&gt;
R F Woodbury, 1990, &#039;&#039;Variations in Solids: A Declarative Treatment&#039;&#039;, Computers and Graphics, Special Issue on Features and Geometric Reasoning, Vol. 14, No. 2, 173-188. {{doi|10.1016/0097-8493(90)90030-2}}&lt;br /&gt;
&lt;br /&gt;
S Kuru and A W Westerberg, 1985, &#039;&#039;A Newton-Raphson based strategy for exploiting latency in dynamic simulation&#039;&#039;, Computers &amp;amp; Chemical Engineering, Vol 9., No. 2, {{doi|10.1016/0098-1354(85)85007-9}} or [[Media:Kuru1985.pdf|PDF]]&lt;br /&gt;
&lt;br /&gt;
A W Westerberg and D R Benajamin, 1985, &#039;&#039;Thoughts on a Future Equation-Oriented Flowsheeting System&#039;&#039;, Computers and Chemical Engineering, Vol. 9, No. 5, 517-526. {{doi|10.1016/0098-1354(85)80026-0}}&lt;br /&gt;
&lt;br /&gt;
Michael H Locke and Arthur W Westerberg, 1983, &#039;&#039;The ascend-II system—a flowsheeting application of a successive quadratic programming methodology&#039;&#039;, Computers &amp;amp; Chemical Engineering &#039;&#039;&#039;7&#039;&#039;&#039; (5) 615-630. {{doi|10.1016/0098-1354(83)80007-6}}&lt;br /&gt;
&lt;br /&gt;
A W Westerberg and S W Director, 1978, &#039;&#039;A modified least squares next term algorithm for solving sparse n × n sets of nonlinear equations&#039;&#039;, Computers and Chemical Engineering, Vol. 2, pp 77-81. {{doi|10.1016/0098-1354(78)80011-8}} (this paper describes the basis of our [[QRSlv]] solver)&lt;br /&gt;
&lt;br /&gt;
== Conference presentations and papers ==&lt;br /&gt;
A Sadrieh, P A Bahri, &#039;&#039;Application of Graphic Processing Unit in Model Predictive Control&#039;&#039;, Computer Aided Chemical Engineering, Elsevier, 2011, Volume 29, 21st European Symposium on Computer Aided Process Engineering, Pages 492-496, ISSN 1570-7946, ISBN 9780444538956, {{doi|10.1016/B978-0-444-53711-9.50099-7}} or try [http://www.sciencedirect.com/science/article/pii/B9780444537119500997 this link].&lt;br /&gt;
&lt;br /&gt;
J Pye, K Lovegrove and G Burgess. &#039;&#039;[http://solar-thermal.anu.edu.au/files/2010/10/pye-2010-solarpaces-combined-cycle.pdf Combined-cycle solarised gas turbine with steam, organic and CO2 bottoming cycles]&#039;&#039;, Proceedings of SolarPACES 2010, Perpignan, France, Sept 2010.&lt;br /&gt;
&lt;br /&gt;
J Coventry and J Pye, 2009, &#039;&#039;[http://solar-thermal.anu.edu.au/files/2010/02/SolarPACES2009_CoventryPye.pdf Coupling supercritical and superheated direct steam generation with thermal energy storage]&#039;&#039;. SolarPACES 2009, Berlin, 15-18 Sept.&lt;br /&gt;
&lt;br /&gt;
H G Silva and R L R Salcedo, 2006, &#039;&#039;Modeling and Optimization of Chemical Processes: ASCEND IV and Stochastic Optimizers&#039;&#039;, Proceedings of [http://www.actapress.com/Content_Of_Proceeding.aspx?ProceedingID=381 Modelling and Simulation], Montreal, Canada.&lt;br /&gt;
&lt;br /&gt;
Allan, B.A. and A. W. Westerberg, &#039;&#039;Compiling and Solving 100,000 equations on a PC in (3) minutes&#039;&#039; Carnegie Mellon University, Pittsburgh, PA, 1998. Presented April 1998 at INFORMS Montreal conference. [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/informs98.pdf PDF].&lt;br /&gt;
&lt;br /&gt;
Bhargava, H., Krishnan, R., and Piela, P., &#039;&#039;Formalizing the Semantics of ASCEND&#039;&#039;, Proceeding of the 27th Hawaii International Conference on the System Sciences, IEEE Computer Society Press, 1994. &lt;br /&gt;
[http://ascend.cheme.cmu.edu/ftp/htbibliography/b1.html Abstract], {{doi|http://dx.doi.org/10.1109/HICSS.1994.323312}}.&lt;br /&gt;
&lt;br /&gt;
Krishnan, R., Piela, P., and Westerberg, A., &#039;&#039;Reusing Mathematical Models in ASCEND&#039;&#039;, Recent Developments in Decision Support Systems, C. W. Holsapple and A. B. Whinston (eds.). Proceedings of the NATO ASI on Decision Support Systems. Springer-Verlag, in cooperation with NATO Scientific Affairs Division, 1993. [http://ascend.cheme.cmu.edu/ftp/htbibliography/b5.html Abstract], read [http://books.google.com.au/books?id=UefgA0jltcgC&amp;amp;pg=PA275 via Google Books].&lt;br /&gt;
&lt;br /&gt;
Westerberg, A.W., Abbott, K.A., and Allan, B.A., &#039;&#039;Plans for ASCEND IV: Our Next Generation Equational-Based Modeling Environment&#039;&#039;, AspenWorld 94, Boston Massachusetts, November 1994. [http://ascend.cheme.cmu.edu/ftp/htbibliography/b10.html abstract], [[Media:westerberg-1994-aspenworld.pdf|paper]], [[Media:AspenWorldASCENDSlides.pdf|slides]].&lt;br /&gt;
&lt;br /&gt;
Westerberg, A., Piela, P., McKelvey, R., and Epperly, T., &#039;&#039;The ASCEND Modeling Environment and Its Implications&#039;&#039;, Proceedings of the 4th International Symposium on Process Systems Engineering, I: Design, I.2.1 - I.2.12, 1991. [http://ascend.cheme.cmu.edu/ftp/htbibliography/ba.html Abstract].&lt;br /&gt;
&lt;br /&gt;
Westerberg, A., Piela, P., Subrahmanian, E., Podnar, G., and Elm, W., &#039;&#039;A Future Computer Environment for Preliminary Design&#039;&#039;, Proceedings of the Third International Conference on Foundations of Computer Aided Process Design, J. Siirola, I. Grossmann, and G. Stephanopoulos (eds.), 1989. [http://ascend.cheme.cmu.edu/ftp/htbibliography/bb.html Abstract]&lt;br /&gt;
&lt;br /&gt;
== Technical reports ==&lt;br /&gt;
&lt;br /&gt;
L Cisternas,  N Luza, E Gálvez, 2007. &#039;&#039;[[Media:HydroSim-reporte-tecnico.pdf|HydroSim: Una Librería para Simulación, Modelación y Optimización en Hidrometalurgia]]&#039;&#039; (in spanish; english title: &#039;&#039;HydroSim: a library for simulation, modelling and optimisation in hydrometallurgy&#039;&#039;), Technical report, Universidad de Antifagasta, Design of Product and Process Group and CICITEM. &#039;&#039;&#039;PDF&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
J L Perry and B A Allan, 1996. &#039;&#039;[http://repository.cmu.edu/compsci/2143/ Design and Use of Dynamic Modeling in ASCEND IV]&#039;&#039;, Carnegie Mellon University, EDRC Technical Report 06-224-96. &#039;&#039;&#039;PDF&#039;&#039;&#039; ([http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/AscendIVP.pdf alternative link])&lt;br /&gt;
&lt;br /&gt;
P Piela and A W Westerberg, 1994. &#039;&#039;[http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/processModeling.pdf Equation-Based Process Modeling]&#039;&#039;, Carnegie Mellon University, EDRC Technical Report. &#039;&#039;&#039;PDF&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
A W Westerberg, 1994. &#039;&#039;[http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/ascendIntro.pdf ASCEND Modeling Language and Environment Notes]&#039;&#039;, CAPD Short Course Notes, Carnegie Mellon University.&lt;br /&gt;
&lt;br /&gt;
T Epperly, 1989. [[Media:epperlyPaperICES05-29-89.pdf|&#039;&#039;Implementation of an ASCEND Interpreter&#039;&#039;]], Carnegie Mellon University, EDRC Technical Report 05-29-89, &#039;&#039;&#039;PDF&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
B A Allan and A W Westerberg, 1997.&#039;&#039;[http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/aiche97-201f.pdf Reusability and Scalability in Modeling] (Previously titled &amp;quot;Reusability in Modeling and Modeling Support Environments&amp;quot;)&#039;&#039;, Carnegie Mellon University, ICES Technical Report, November 1997. Pittsburgh, PA. (AIChE 1997 Meeting Paper 201f.) (see also [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/slides-201f.pdf presentation slides])&lt;br /&gt;
&lt;br /&gt;
B A Allan and A W Westerberg, 1997. &#039;&#039;The ASCEND IV Language Syntax and Semantics&#039;&#039;, Carnegie Mellon University, EDRC Technical Report. (this report is now edited and incorporated into our [[:Category:Documentation|documentation]]).&lt;br /&gt;
&lt;br /&gt;
B A Allan, V Rico-Ramirez, M Thomas and K Tyner, 1996. &#039;&#039;ASCEND IV: A portable Mathematical Modeling Environment&#039;&#039;, Carnegie Mellon University, ICES Technical Report, October. [http://ascend.cheme.cmu.edu/ftp/pdfHelp/ascend-help-BOOK-3.pdf PDF].&lt;br /&gt;
&lt;br /&gt;
K Dee and A Westerberg, 1922. &#039;&#039;CEPHDA: Chemical Engineering Process Hierarchical Design with ASCEND&#039;&#039;, Carnegie Mellon University, EDRC Technical Report 06-140-92 [http://ascend.cheme.cmu.edu/ftp/htbibliography/b2.html Abstract].&lt;br /&gt;
&lt;br /&gt;
V Rico-Ramirez, B A Allan and A W Westerberg, 1998. &#039;&#039;Conditional Modeling in an Equation-Based Environment&#039;&#039;, Carnegie Mellon University, ICES Technical Report 06-242-98. [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/tech_modeling.pdf PDF].&lt;br /&gt;
&lt;br /&gt;
V Rico-Ramirez and A W Westerberg, 1998. &#039;&#039;Complementarity Formulation for the Representation of Algebraic Systems Containing Conditional Equations&#039;&#039;, Carnegie Mellon University, ICES Technical Report 06-243-98. [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/tech_complementarity.pdf PDF].&lt;br /&gt;
&lt;br /&gt;
K Westerberg, 1989. &#039;&#039;Development of Software for Solving Systems of Nonlinear Equations&#039;&#039;, Carnegie Mellon University, EDRC Technical Report 05-36-89, Pittsburgh, PA. [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/nonlinear_rpt.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
K Westerberg, 1989. &#039;&#039;Development of Software for Solving Systems of Linear Equations&#039;&#039;, Carnegie Mellon University, EDRC Technical Report 05-35-89. [http://ascend.cheme.cmu.edu/ftp/pdfPapersRptsSlides/linear_rpt.pdf PDF]&lt;br /&gt;
&lt;br /&gt;
J Zaher, 1991. &#039;&#039;Developing Reusable Libraries in the ASCEND environment&#039;&#039;, Carnegie Mellon University, EDRC Technical Report 06-108-91. [http://ascend.cheme.cmu.edu/ftp/htbibliography/bf.html PDF].&lt;br /&gt;
&lt;br /&gt;
== Undergraduate theses ==&lt;br /&gt;
&lt;br /&gt;
K Bestuzheva, 2014. &#039;&#039;Computer modelling of successive collisions of a particle with moving boundaries in an external magnetic field&#039;&#039; (in Russian &#039;&#039;Компьютерное моделирование последовательных отражений частицы от движущихся поверхностей при наличии внешнего магнитного поля&#039;&#039;). Final thesis for the degree of a specialist, State Management University, Moscow, Russia. [[:File:Bestuzheva_final_thesis.pdf | PDF]]&lt;br /&gt;
&lt;br /&gt;
R Hogan, 2012. &#039;&#039;Efficiency of supercritical carbon dioxide for the working fluid in the power cycle for high-temperature concentrated solar power&#039;&#039;. BE thesis, Australian National University. Available on request.&lt;br /&gt;
&lt;br /&gt;
K Sio, 2010. &#039;&#039;Cost Optimisation of DSG Thermal Storage System&#039;&#039;. BE thesis, Australian National University. Available on request.&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Bestuzheva_final_thesis.pdf&amp;diff=4627</id>
		<title>File:Bestuzheva final thesis.pdf</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Bestuzheva_final_thesis.pdf&amp;diff=4627"/>
		<updated>2014-07-07T14:54:02Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4450</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4450"/>
		<updated>2013-09-23T15:05:40Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat. The heater is swithed on when the temperature rises above 23 degrees and is switched on when the temperature falls below 19 degrees.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - another version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
[[File:hysteron.png|thumb|none|A sketch of the cilinder]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 09 - September, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working on GUI features.&lt;br /&gt;
&lt;br /&gt;
The points where an event was triggered are marked red on the plot. Plots for each event can also be created showing how the variable changes at each step of processing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events1.png|thumb|none|Several equal values of the independent variable mean that there was an event]]&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events2.png|thumb|none|Red points indicate events]]&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events3.png|thumb|none|The user may choose an event and plot the values found at the chosen boundary]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 02 - September, 08.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of the Huygens pendulum clock: {{srcbranch|ksenija2|models/ksenija/clock.a4c}}&lt;br /&gt;
&lt;br /&gt;
[[File:huygens_clock.png|thumb|none|The phase plane of the Huygens clock model]]&lt;br /&gt;
&lt;br /&gt;
* Changed cond_config.c so that when the OTHERWISE case of the event is switched on at the boundary, the method associated with the event is not called.&lt;br /&gt;
&lt;br /&gt;
* Switched off event analyzing in configure_conditional_problem (which is called during problem building). The events are analyzed later, so there is no need for that call.&lt;br /&gt;
&lt;br /&gt;
* Set the event_active flags FALSE before analyzing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Report errors if the analysis of the system after solving the boundary equations fails.&lt;br /&gt;
&lt;br /&gt;
* Until this week the integrator was reinitialized after each root found. This didn&#039;t influence the speed much, but for some models this reinitialization could lead to incorrect solutions. Without it when solving the hysteron model IDA reported an error because of two very close roots.&lt;br /&gt;
&lt;br /&gt;
To solve this problem I changed the boundary crossing code. Now after solving the boundary equations for the first time it is checked if values of some discrete variables have changed. If yes, the system is analyzed one more time. These actions are repeated in a loop until no events are called or no discrete variables change their values. So the case when an event causes another event is handled properly and no unneeded reinitialization of the integrator is done.&lt;br /&gt;
&lt;br /&gt;
* Thought about boundary emission and read about the implementation of this feature in Modelica and Mathematica.&lt;br /&gt;
&lt;br /&gt;
* I decided to spend the last week of GSOC on improving the plotting tools for models with events. There are several points at each boundary which can be useful for debugging of the models, and I am going to plot them in such a way that the user would be able to see those points and distinguish them from other points.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Event syntax.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New event syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
event_name: EVENT (bvar)&lt;br /&gt;
 CASE TRUE: USE rel1;&lt;br /&gt;
 OTHERWISE: USE rel2;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
bvar is a boolean variable, rel1 and rel2 are relations or logrelations or submodels. When bvar changes its value from TRUE to FALSE, the corresponding case is activated. Then the model is solved with LRSlv and QRSlv, and after that rel1 is deactivated.&lt;br /&gt;
&lt;br /&gt;
rel2 is switched on during the continious-time frame or at those boundaries at which this event is not triggered.&lt;br /&gt;
&lt;br /&gt;
If the model contains methods with names event_name and/or event_name_end, these methods will be called before and after solving the system at the boundary correspondingly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New boundary crossing algorithm.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Here is the algorithm of the boundary crossing function:&lt;br /&gt;
&lt;br /&gt;
*For each boundary:&lt;br /&gt;
**If this boundary has been crossed:&lt;br /&gt;
***Has it been crossed twice in one direction?&lt;br /&gt;
***No:&lt;br /&gt;
****change the boundary flag.&lt;br /&gt;
***Yes:&lt;br /&gt;
****if it has not been done yet, save current values of discrete variables.&lt;br /&gt;
****Change the boundary flag.&lt;br /&gt;
****Call the logical solver (it could be called not it this loop, but it would make the code more complicated and it would not make the algorithm much more efficient because double crossing of two boundaries at the same time is rather improbable. In all other cases there is no difference.)&lt;br /&gt;
****Change the boundary flag one more time (so the value of the flag will be the same as at the beginning of the function.&lt;br /&gt;
*Call the logical solver.&lt;br /&gt;
*If there has been a double crossing, set the correct previous values for those variables for which it is necessary.&lt;br /&gt;
*Have some discrete variables changed?&lt;br /&gt;
**No:&lt;br /&gt;
***reset the boundary flags, continue integration.&lt;br /&gt;
**Yes:&lt;br /&gt;
***reanalyze the system: switch on the correct regions (WHEN cases), call the methods associated with events, switch on the boundary equations.&lt;br /&gt;
***Solve the system with QRSlv.&lt;br /&gt;
***Check if some boundaries have been crossed as a result of solving the system with QRSlv. Set the correct boundary flags.&lt;br /&gt;
***Solve the system with LRSlv.&lt;br /&gt;
***If some discrete variables have changed, repeat from reanalyzing the system.&lt;br /&gt;
***Reanalyze the system: switch on the correct regions, call the methods associated with events, switch off the boundary equations.&lt;br /&gt;
***Reset the boundary flags, continue integration.&lt;br /&gt;
&lt;br /&gt;
Here boundaries are data structures corresponding to the relations declared inside CONDITIONAL.&lt;br /&gt;
Boundary flags can have two values: 0 and 1. If a flag is 0 (or 1), this means that when solving the system of logical equations the statement: SATISFIED(cond_rel), where cond_rel in the boundary condition, will return 0(or 1). The flags are set at the beginning of integration and changed at boundary crossings.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pre() syntax.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The aim of pre() variables is to access the value of the variable before the boundary. Pre() variables are FIXed and change their values only at boundaries. An example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
y IS_A solver_var;&lt;br /&gt;
PREVIOUS y;&lt;br /&gt;
bnd_eq: y = pre(y)*0.9;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4449</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4449"/>
		<updated>2013-09-20T14:19:27Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat. The heater is swithed on when the temperature rises above 23 degrees and is switched on when the temperature falls below 19 degrees.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - another version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
[[File:hysteron.png|thumb|none|A sketch of the cilinder]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 09 - September, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working on GUI features.&lt;br /&gt;
&lt;br /&gt;
The points where an event was triggered are marked red on the plot. Plots for each event can also be created showing how the variable changes at each step of processing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events1.png|thumb|none|Several equal values of the independent variable mean that there was an event]]&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events2.png|thumb|none|Red points indicate events]]&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_events3.png|thumb|none|The user may choose an event and plot the values found at the chosen boundary]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 02 - September, 08.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of the Huygens pendulum clock: {{srcbranch|ksenija2|models/ksenija/clock.a4c}}&lt;br /&gt;
&lt;br /&gt;
[[File:huygens_clock.png|thumb|none|The phase plane of the Huygens clock model]]&lt;br /&gt;
&lt;br /&gt;
* Changed cond_config.c so that when the OTHERWISE case of the event is switched on at the boundary, the method associated with the event is not called.&lt;br /&gt;
&lt;br /&gt;
* Switched off event analyzing in configure_conditional_problem (which is called during problem building). The events are analyzed later, so there is no need for that call.&lt;br /&gt;
&lt;br /&gt;
* Set the event_active flags FALSE before analyzing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Report errors if the analysis of the system after solving the boundary equations fails.&lt;br /&gt;
&lt;br /&gt;
* Until this week the integrator was reinitialized after each root found. This didn&#039;t influence the speed much, but for some models this reinitialization could lead to incorrect solutions. Without it when solving the hysteron model IDA reported an error because of two very close roots.&lt;br /&gt;
&lt;br /&gt;
To solve this problem I changed the boundary crossing code. Now after solving the boundary equations for the first time it is checked if values of some discrete variables have changed. If yes, the system is analyzed one more time. These actions are repeated in a loop until no events are called or no discrete variables change their values. So the case when an event causes another event is handled properly and no unneeded reinitialization of the integrator is done.&lt;br /&gt;
&lt;br /&gt;
* Thought about boundary emission and read about the implementation of this feature in Modelica and Mathematica.&lt;br /&gt;
&lt;br /&gt;
* I decided to spend the last week of GSOC on improving the plotting tools for models with events. There are several points at each boundary which can be useful for debugging of the models, and I am going to plot them in such a way that the user would be able to see those points and distinguish them from other points.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events3.png&amp;diff=4448</id>
		<title>File:Plot events3.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events3.png&amp;diff=4448"/>
		<updated>2013-09-20T14:18:56Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events2.png&amp;diff=4447</id>
		<title>File:Plot events2.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events2.png&amp;diff=4447"/>
		<updated>2013-09-20T14:18:07Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_events1.png&amp;diff=4446</id>
		<title>File:Plot events1.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_events1.png&amp;diff=4446"/>
		<updated>2013-09-20T14:17:10Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4445</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4445"/>
		<updated>2013-09-20T13:26:25Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat. The heater is swithed on when the temperature rises above 23 degrees and is switched on when the temperature falls below 19 degrees.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - another version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
[[File:hysteron.png|thumb|none|A sketch of the cilinder]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 02 - September, 08.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of the Huygens pendulum clock: {{srcbranch|ksenija2|models/ksenija/clock.a4c}}&lt;br /&gt;
&lt;br /&gt;
[[File:huygens_clock.png|thumb|none|The phase plane of the Huygens clock model]]&lt;br /&gt;
&lt;br /&gt;
* Changed cond_config.c so that when the OTHERWISE case of the event is switched on at the boundary, the method associated with the event is not called.&lt;br /&gt;
&lt;br /&gt;
* Switched off event analyzing in configure_conditional_problem (which is called during problem building). The events are analyzed later, so there is no need for that call.&lt;br /&gt;
&lt;br /&gt;
* Set the event_active flags FALSE before analyzing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Report errors if the analysis of the system after solving the boundary equations fails.&lt;br /&gt;
&lt;br /&gt;
* Until this week the integrator was reinitialized after each root found. This didn&#039;t influence the speed much, but for some models this reinitialization could lead to incorrect solutions. Without it when solving the hysteron model IDA reported an error because of two very close roots.&lt;br /&gt;
&lt;br /&gt;
To solve this problem I changed the boundary crossing code. Now after solving the boundary equations for the first time it is checked if values of some discrete variables have changed. If yes, the system is analyzed one more time. These actions are repeated in a loop until no events are called or no discrete variables change their values. So the case when an event causes another event is handled properly and no unneeded reinitialization of the integrator is done.&lt;br /&gt;
&lt;br /&gt;
* Thought about boundary emission and read about the implementation of this feature in Modelica and Mathematica.&lt;br /&gt;
&lt;br /&gt;
* I decided to spend the last week of GSOC on improving the plotting tools for models with events. There are several points at each boundary which can be useful for debugging of the models, and I am going to plot them in such a way that the user would be able to see those points and distinguish them from other points.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Hysteron.png&amp;diff=4444</id>
		<title>File:Hysteron.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Hysteron.png&amp;diff=4444"/>
		<updated>2013-09-20T13:25:27Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4441</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4441"/>
		<updated>2013-09-17T16:05:36Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 02 - September, 08.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of the Huygens pendulum clock: {{srcbranch|ksenija2|models/ksenija/clock.a4c}}&lt;br /&gt;
&lt;br /&gt;
[[File:huygens_clock.png|thumb|none|The phase plane of the Huygens clock model]]&lt;br /&gt;
&lt;br /&gt;
* Changed cond_config.c so that when the OTHERWISE case of the event is switched on at the boundary, the method associated with the event is not called.&lt;br /&gt;
&lt;br /&gt;
* Switched off event analyzing in configure_conditional_problem (which is called during problem building). The events are analyzed later, so there is no need for that call.&lt;br /&gt;
&lt;br /&gt;
* Set the event_active flags FALSE before analyzing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Report errors if the analysis of the system after solving the boundary equations fails.&lt;br /&gt;
&lt;br /&gt;
* Until this week the integrator was reinitialized after each root found. This didn&#039;t influence the speed much, but for some models this reinitialization could lead to incorrect solutions. Without it when solving the hysteron model IDA reported an error because of two very close roots.&lt;br /&gt;
&lt;br /&gt;
To solve this problem I changed the boundary crossing code. Now after solving the boundary equations for the first time it is checked if values of some discrete variables have changed. If yes, the system is analyzed one more time. These actions are repeated in a loop until no events are called or no discrete variables change their values. So the case when an event causes another event is handled properly and no unneeded reinitialization of the integrator is done.&lt;br /&gt;
&lt;br /&gt;
* Thought about boundary emission and read about the implementation of this feature in Modelica and Mathematica.&lt;br /&gt;
&lt;br /&gt;
* I decided to spend the last week of GSOC on improving the plotting tools for models with events. There are several points at each boundary which can be useful for debugging of the models, and I am going to plot them in such a way that the user would be able to see those points and distinguish them from other points.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4439</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4439"/>
		<updated>2013-09-09T17:15:10Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 01.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 02.09 - 16.09 - add automatic boundary emission.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;September, 02 - September, 08.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of the Huygens pendulum clock: {{srcbranch|ksenija2|models/ksenija/clock.a4c}}&lt;br /&gt;
&lt;br /&gt;
[[File:huygens_clock.png|thumb|none|The phase plane of the Huygens clock model]]&lt;br /&gt;
&lt;br /&gt;
* Changed cond_config.c so that when the OTHERWISE case of the event is switched on at the boundary, the method associated with the event is not called.&lt;br /&gt;
&lt;br /&gt;
* Switched off event analyzing in configure_conditional_problem (which is called during problem building). The events are analyzed later, so there is no need for that call.&lt;br /&gt;
&lt;br /&gt;
* Set the event_active flags FALSE before analyzing the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Report errors if the analysis of the system after solving the boundary equations fails.&lt;br /&gt;
&lt;br /&gt;
* Until this week the integrator was reinitialized after each root found. This didn&#039;t influence the speed much, but for some models this reinitialization could lead to incorrect solutions. Without it when solving the hysteron model IDA reported an error because of two very close roots.&lt;br /&gt;
&lt;br /&gt;
To solve this problem I changed the boundary crossing code. Now after solving the boundary equations for the first time it is checked if values of some discrete variables have changed. If yes, the system is analyzed one more time. These actions are repeated in a loop until no events are called or no discrete variables change their values. So the case when an event causes another event is handled properly and no unneeded reinitialization of the integrator is done.&lt;br /&gt;
&lt;br /&gt;
* Thought about boundary emission and read about the implementation of this feature in Modelica and Mathematica.&lt;br /&gt;
&lt;br /&gt;
* I decided to spend the last week of GSOC on improving the plotting tools for models with events. There are several points at each boundary which can be useful for debugging of the models, and I am going to plot them in such a way that the user would be able to see those points and distinguish them from other points.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Huygens_clock.png&amp;diff=4438</id>
		<title>File:Huygens clock.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Huygens_clock.png&amp;diff=4438"/>
		<updated>2013-09-09T17:09:46Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4436</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4436"/>
		<updated>2013-09-04T07:57:40Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 01.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 02.09 - 16.09 - add automatic boundary emission.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/clock.a4c}} - a model of the Galileo-Huygens clock from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4435</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4435"/>
		<updated>2013-09-04T07:37:05Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 01.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 02.09 - 16.09 - add automatic boundary emission.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 26 - September, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code.&lt;br /&gt;
&lt;br /&gt;
* Changed the ida_cross_boundary function so that previous of discrete variables values are now corrected only if a double crossing has been detected.&lt;br /&gt;
&lt;br /&gt;
* Added a warning message which is shown if relation instances are referenced by both events and whens.&lt;br /&gt;
&lt;br /&gt;
* Set the flags indicating if there is a method associated with the event. If there isn&#039;t, the search for the method is skipped.&lt;br /&gt;
&lt;br /&gt;
* Changed the integrator_ida_solve function crossing one and the same boundary twice in one direction is avoided not only if IDASolve is recalled up to the same tindex (which happens if the root is found far from the desired output t), but also if the timestep is advanced.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4426</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4426"/>
		<updated>2013-08-30T06:45:49Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 01.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 02.09 - 16.09 - add automatic boundary emission.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4417</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4417"/>
		<updated>2013-08-27T07:11:46Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 16.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4416</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4416"/>
		<updated>2013-08-27T07:00:42Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax. (done)&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models. (done)&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 16.09 - test the event handling code, fix bugs.&lt;br /&gt;
* 17.09 - 23.09 - write documentation.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4404</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4404"/>
		<updated>2013-08-26T09:12:34Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Management (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax.&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models.&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 08.09 - try to improve ASCEND conditional syntax (see [[User:Ksenija#Possible_ways_of_improvement_of_our_conditional_syntax.]]). Not sure how much time I will spend on it.&lt;br /&gt;
* 09.09 - 16.09 - test the new syntax.&lt;br /&gt;
* 17.09 - 23.09 - write documentation, discuss the reinit functionality.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Add the reinit syntax.&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontinuously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cylinder, the position of the piston is the input u, the position of the cylinder’s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A sketch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 19 - August, 25.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented one more (experimental) approach to event handling. Mostly because of the problem with unwanted multiple boundary crossings in option 2 I think that the better approach is option 1. Here is the description of the two algorithms and their comparison:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 1.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This code uses flags set by IDA for solving the systems of logical relations at boundaries. Except when finding the initial values of boundary flags, for this algorithm there is no difference between strict and non-strict inequalities.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 1:&lt;br /&gt;
&lt;br /&gt;
The most important: currently this algorithm is more reliable. The main problem with the second option is that in some models the system gets stuck at some boundary, crossing it again and again because of small unwanted variation in the values of variables on which the boundary depends. This may result in wrong solution of the model or infinite loops. This can be avoided by some changes in models (for example, excluding the equation which causes the unneeded variation from the boundary system), but option 1 works without such changes too.&lt;br /&gt;
&lt;br /&gt;
It is a bit more fast than option 2, though I can&#039;t say that it is easy to see the difference in speed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;Option 2.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
With this option LRSlv doesn&#039;t use the flags set by IDA and finds the values of boolean variables only by current values of variables in the model.&lt;br /&gt;
&lt;br /&gt;
The advantages of option 2:&lt;br /&gt;
&lt;br /&gt;
In option 1 there is an assumption that if the boundary is crossed, then after solving the system this boundary won&#039;t be crossed in the opposite direction. But this assumption makes the code more reliable and helps avoid the problem which I described in option 1. And as far as I understand, all models can be written in such a way that they will satisfy this assumption (for some of them the conditions will need to be a bit modified, but it is possible and not so difficult for the user).&lt;br /&gt;
&lt;br /&gt;
In option 2 strict and non-strict inequalities are different. This works in the following way: when IDA report a root found, only non-strict inequalities are satisfied. ida_cross_boundary is called, all necessary actions are performed. But strict inequalities at this point are not satisfied. So IDA makes one more very small timestep, and ida_cross_boundary is called one more time. These additional calls of ida_cross_boundary make option 2 slower. But the question is: is it correct to trigger the events and/or switch to a new region after this small timestep, but not at the point where IDA reported a root? On the other hand, the advantage is that option 2 enables the user to set the order of events/regions. For example, if some region should be switched on before the event is triggered, and not after. But I haven&#039;t yet seen such models in which the order matters.&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing PreInfo to the var properties window.&lt;br /&gt;
&lt;br /&gt;
* Added one more version (the one from the Modelica specification) of the bouncing ball model: {{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}}. It works fine, so now the problem with the bouncing ball may be considered solved.&lt;br /&gt;
&lt;br /&gt;
* Fixed some bugs in event handling:&lt;br /&gt;
&lt;br /&gt;
** In the case of double crossings two calls to log_solve result in previous values of boolean variables being equal to their current values, while actually they may differ. So before solving the logical system the values of discrete variables are saved in an array and then the previous values are restored.&lt;br /&gt;
&lt;br /&gt;
** Reset the boundary flag not only for the last processed boundary, but for all boundaries, because multiple boundaries may be crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
** Flag the boundaries which are crossed as a result of solving the system at the boundary.&lt;br /&gt;
&lt;br /&gt;
* Fixed a problem with wrong initial evaluation of boundaries.&lt;br /&gt;
&lt;br /&gt;
For example, in the bouncing ball model the initial speed is 0. The model contains a conditional relation v &amp;gt;= 0. Before starting integration this condition is evaluated as true. The boundary flag is 1. The ball starts falling, the speed is negative, but the boundary flag is still 1.&lt;br /&gt;
&lt;br /&gt;
To fix this problem I created an array of flags indicating if the boundary still needs to be evaluated. At the next timestep an attempt to evaluate the flagged boundaries is made, and this is repeated until all boundaries have the correct flags. This code is more reliable when the boundaries are not crossed during the first timestep.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, especially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambiguous. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach because in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was switched on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambiguous, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There shouldn’t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinality as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambiguous and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Summing up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analysis functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4401</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4401"/>
		<updated>2013-08-21T08:53:00Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax.&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models.&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 08.09 - try to improve ASCEND conditional syntax (see [[User:Ksenija#Possible_ways_of_improvement_of_our_conditional_syntax.]]). Not sure how much time I will spend on it.&lt;br /&gt;
* 09.09 - 16.09 - test the new syntax.&lt;br /&gt;
* 17.09 - 23.09 - write documentation, discuss the reinit functionality.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Add the reinit syntax.&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event3.a4c}} - the Modelica version of the bouncing ball model.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow from Ju.I.Neimark, &amp;quot;Mathematical models in natural science and engineering&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
[[File:vessel_with_siphon.png|thumb|none|A scetch of the vessel]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, espesially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambigious. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach bacause in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png|thumb|none|A screenshot of the plot for the test model]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Vessel_with_siphon.png&amp;diff=4400</id>
		<title>File:Vessel with siphon.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Vessel_with_siphon.png&amp;diff=4400"/>
		<updated>2013-08-21T08:44:02Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4397</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4397"/>
		<updated>2013-08-19T17:21:50Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax.&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models.&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 08.09 - try to improve ASCEND conditional syntax (see [[User:Ksenija#Possible_ways_of_improvement_of_our_conditional_syntax.]]). Not sure how much time I will spend on it.&lt;br /&gt;
* 09.09 - 16.09 - test the new syntax.&lt;br /&gt;
* 17.09 - 23.09 - write documentation, discuss the reinit functionality.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Add the reinit syntax.&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 12 - August, 18.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about possible ways of improving the conditional syntax. After trying several options of the parser rule found the one which didn&#039;t cause conflicts. Thought about how the data structures will need to be changed so as to handle new syntax. (All this was before I received a letter from Ben saying that I shouldn&#039;t do it now).&lt;br /&gt;
&lt;br /&gt;
* Started working on reinit() syntax. The idea was that the reinit(x,expr) operator would evaluate expr to a value, make x unknown and introduce the equation x = value. I added a parser rule and a new flag to the relation statement structure. Then I spent some time (not so little, I should say) reading the code, espesially that deals with relations, and thought about the implementation of reinit. I came to a conclusion that reinit seems to be rather ambigious. Currently I think that in models it can be easily replaced by assignments or a combination of boundary equations and FIX/FREEs.&lt;br /&gt;
&lt;br /&gt;
* So I moved on to calling methods from events. Until this week they were found by standard names event and end_event, but they couldn&#039;t be associated with particular events. Now they are found using the name of the event (for example, if the name of the event is my_event, then the method called before solving the system at the boundary would be called my_event, and the method called after solving the system would be called my_event_end). But I am also considering leaving also the old approach bacause in some models the same methods need to be called regardless of which event is triggered. May be some optimisation of calling the methods is possible (need to think about it). One more question is: how to associate events declared in loops with methods?&lt;br /&gt;
&lt;br /&gt;
* Made some minor changes in cond_config.c (where the events which need to be triggered are found and the system is reconfigured).&lt;br /&gt;
&lt;br /&gt;
* Rewrote one more model using the pre() syntax.&lt;br /&gt;
&lt;br /&gt;
And finally I went to the country for the weekend and got lost in a forest. And that forest was a bit more frightening then, for example, a forest consisting of undirected cycle-free graphs :)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4334</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4334"/>
		<updated>2013-08-12T06:37:52Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax.&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models.&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 08.09 - try to improve ASCEND conditional syntax (see [[User:Ksenija#Possible_ways_of_improvement_of_our_conditional_syntax.]]). Not sure how much time I will spend on it.&lt;br /&gt;
* 09.09 - 16.09 - test the new syntax.&lt;br /&gt;
* 17.09 - 23.09 - write documentation, discuss the reinit functionality.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Add the reinit syntax.&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 29 - August, 11.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
During these two weeks I have been working on pre() variables. Here is the description of their implementation:&lt;br /&gt;
&lt;br /&gt;
* When creating the type definition of the model the symchars for pre() variables are automatically generated using the name of the argument. The type of the argument is found and the pre() variable is given the same type. pre() variables are added to the model childlist.&lt;br /&gt;
&lt;br /&gt;
* At the same time some checks are done: it is checked that pre() variables are contained only in the autogenerated IS_A statements, relations and ALIASES statements; that there are no nested pres (because there is no sense in variables like pre(pre(x))); that there are no pres inside derivative variables (but derivatives inside pres are ok).&lt;br /&gt;
&lt;br /&gt;
* The structures for real atom instances contain a structure PreInfo which stores the information about pre() arguments (for pre() variables) and about pre() variables (for variables for which pres are declared).&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of variables which have pres. If such variables are merged, their pre() variables are also merged. Also pre() variables are refined if their arguments are refined.&lt;br /&gt;
&lt;br /&gt;
* In the analyzer a list of pre() variables is created in the problem_t structure. Each pre() variable has the pointer to its argument. It is checked that pre() variables are incident only in those relations which are referenced by an event. All pre() variables are automatically fixed. At the end of problem analysis the list of pre() variables is moved to the diffvars structure which is later used by the integrator.&lt;br /&gt;
&lt;br /&gt;
* On the solver side everything is rather simple. At each boundary the value of the pre() variable is updated and becomes equal to the value of its argument. All the other time pres remain unchanged. So the following relation: x = pre(x)+1 solved at the boundary means that the new value of x (the value of x after the boundary) will be equal to the value of x before the boundary plus one.&lt;br /&gt;
&lt;br /&gt;
* I have updated the following models: the model of a bouncing ball and one of the test models.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4310</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4310"/>
		<updated>2013-08-03T16:13:00Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Todos for the rest of the summer.===&lt;br /&gt;
&lt;br /&gt;
* 03.08 - 11.08 - add the pre() syntax.&lt;br /&gt;
* 12.08 - 18.08 - test the pre() syntax. Add pre functionality to the solver. Update the models.&lt;br /&gt;
* 19.08 - 25.08 - think about possible ways of making event handling more efficient.&lt;br /&gt;
* 26.08 - 08.09 - try to improve ASCEND conditional syntax (see [[User:Ksenija#Possible_ways_of_improvement_of_our_conditional_syntax.]]). Not sure how much time I will spend on it.&lt;br /&gt;
* 09.09 - 16.09 - test the new syntax.&lt;br /&gt;
* 17.09 - 23.09 - write documentation, discuss the reinit functionality.&lt;br /&gt;
&lt;br /&gt;
After GSOC:&lt;br /&gt;
&lt;br /&gt;
* Add the reinit syntax.&lt;br /&gt;
* Check one more time the code for the der syntax, may be improve some parts of it.&lt;br /&gt;
* Write and study with the help of ASCEND complex models with interesting mathematics under the guidance of my university supervisor Dr. Shananin.&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4307</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4307"/>
		<updated>2013-07-31T07:39:34Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Possible ways of improvement of our conditional syntax.===&lt;br /&gt;
&lt;br /&gt;
The first possible modification is defining the condition right in the event statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt; &lt;br /&gt;
EVENT (x &amp;gt; 0)&lt;br /&gt;
...&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some details like tolerances could be set, for example, inside brackets. But I also like the current definition of events and whens using variables. So we may leave the whole structure (cases, etc.) of the statement as it is now, but allow not only variables, but also relations as conditions. If we decide to do this for events, then, I think, we should do the same for whens.&lt;br /&gt;
&lt;br /&gt;
The second is defining relations inside events:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
a IS_A submodel1;&lt;br /&gt;
...&lt;br /&gt;
EVENT(...)&lt;br /&gt;
  CASE TRUE:&lt;br /&gt;
    USE a;&lt;br /&gt;
    x0 = x;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This would make the models shorter but still allow submodels referenced by event. In this case it seems that not so much changes in the implementation would be needed. And, as for the previous suggestion, if&lt;br /&gt;
we do this for events, the same should be done for whens, I think.&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4300</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4300"/>
		<updated>2013-07-29T11:19:16Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 22 - July, 28.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed the observer so as to observe boolean variables as well as real variables.&lt;br /&gt;
* Added two more models: one more model of a hysteron and a model of auto-oscillations of fluid level in a vessel with a siphon.&lt;br /&gt;
* Disallowed integers and symbols in the list of event conditions.&lt;br /&gt;
* Fixed the problem with non-active variables having active derivatives.&lt;br /&gt;
&lt;br /&gt;
Started working on pre() variables:&lt;br /&gt;
&lt;br /&gt;
* Remembering some problems with implicit declarations of der() variables I am going to make declarations of pre() explicit, like declarations of derivatives.&lt;br /&gt;
* Created a new structure inside a Name structure, a structure for the new statements and wrote all the needed functions and defines for them.&lt;br /&gt;
* Added a parser rule for a pre() variable and for a statement declaring pres. This statement automatically generates IS_A statements.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4293</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4293"/>
		<updated>2013-07-27T08:17:16Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event2.a4c}} - one more model of a hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot.png&amp;diff=4292</id>
		<title>File:Plot.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot.png&amp;diff=4292"/>
		<updated>2013-07-27T08:11:41Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: uploaded a new version of &amp;amp;quot;File:Plot.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4289</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4289"/>
		<updated>2013-07-26T06:07:13Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/vessel_with_siphon.a4c}} - a model of auto-oscillations of fluid level in the vessel with simultaneous inflow and outflow.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4286</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4286"/>
		<updated>2013-07-23T16:31:10Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Models in which some boundaries are crossed simultaneously.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 15 - July, 21.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a model of a bouncing ball which instantly changes its speed when hitting the floor.&lt;br /&gt;
* Added a model of a thermostat.&lt;br /&gt;
* Wrote code for solving the boundary equations before integration if needed. If the boolean variables match the values in an event case before integration is started, the event will be triggered.&lt;br /&gt;
* Added a model with two boundaries crossed simultaneously.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Read more about a model with a sliding mode.&lt;br /&gt;
* Searched for new models with interesting kinds of boundaries. (Yu.I. Neimark, Mathematical Models in Natural Science and Engineering; M. Krasnosel&#039;skii, A. Pokrovskii, Systems with Hysteresis.)&lt;br /&gt;
* Read about the Pantelides algorithm. (http://jpye.dyndns.org/pantelides/; E. Hairer, G. Wanner, Solving ordinary differential equations II. Stiff and differential-algebraic problems)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4281</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4281"/>
		<updated>2013-07-19T11:44:02Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/simultaneous.a4c}} - a model with two boundaries crossed simultaneously.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Models in which some boundaries are crossed simultaneously.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: [[Event handling]]. Some more interesting examples may concern encoding analog signals into digital signals (see [https://en.wikipedia.org/wiki/Delta-sigma_modulation Delta-sigma modulation] on Wikipedia). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4279</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4279"/>
		<updated>2013-07-18T08:14:32Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Example models.===&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/test_event2.a4c}} - the simplest model with events. When x crosses some given values, it increases discontiniously, and then integration continues in a usual way.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/thermostat.a4c}} - a model of a thermostat.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/bball_event.a4c}}, {{srcbranch|ksenija2|models/ksenija/bball_event2.a4c}} - a bouncing ball. When it hits the ground, it instantly changes its speed. Some problems concerning this models still remain. First, I don&#039;t know what to do with the system not being square. Adding and subtracting y is definitely not a good idea. Second, when the ball almost rests on the floor it is hard to process all boundary crossings correctly.&lt;br /&gt;
&lt;br /&gt;
{{srcbranch|ksenija2|models/ksenija/hysteron_event.a4c}} - a model of a simple hysteron. This is an example from Krasnosel&#039;skii, M.; Pokrovskii, A.. Systems with Hysteresis. A piston is moving inside a cilinder, the position of the piston is the input u, the position of the cilinder&#039;s right end is the output x.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Models which are not yet written:&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* A vessel with a sliding mode.&lt;br /&gt;
* Models in which some boundaries are crossed simultaneously.&lt;br /&gt;
* Some other models?&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4278</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4278"/>
		<updated>2013-07-18T07:00:22Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 8 - July, 14.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This week I have been working mostly on solving the model of a hysteron.&lt;br /&gt;
&lt;br /&gt;
* The IVP is solved only when the function which analyses events returns 1 which indicates that some events have been activated.&lt;br /&gt;
* Changed the code which sets the boundary flags (indicating if the boundary is satisfied) in order to handle crossing one and the same boundary twice in one direction.&lt;br /&gt;
* The boundary flags are reset only after processing all events and whens at the boundary, so if the boundary is satisfied at the moment when the root is detected, it will be considered satisfied while solving all the boundary equations. Will need to think if it is ok to make such an assumption.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
* Added more test models and wrote a test for integrating models containing events.&lt;br /&gt;
&lt;br /&gt;
Now the hysteron model is solved correctly.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4272</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4272"/>
		<updated>2013-07-08T07:33:34Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}, {{srcbranchdir|ksenija2|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4271</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4271"/>
		<updated>2013-07-08T07:30:22Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 1 - July, 7.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some more checks and warnings.&lt;br /&gt;
* Added a test model of a simple hysteron using the EVENT syntax.&lt;br /&gt;
* Wrote code for finding events which should be activated. First it is checked if some discrete variables have changed their values. If yes, we go through the list of events, check if the discrete variables which are used by these events have changed their values. An event is activated only if its condition changes its value. Then the values of the discrete variables are compared to the values listed in the case of an event. If they match, the event becomes active.&lt;br /&gt;
* Started working on the solution of the test model. After some events become active the system is solved with QRSlv and then again with LRSlv. The first two or three boundaries are crossed correctly (and one of them contains an event) but then a wrong region is chosen and after the next event the solution becomes incorrect. In this model boundary behavior is mixed with region, so I decided to put the solution of this model off till some simpler models work.&lt;br /&gt;
* So I added one more test model. It may resemble a thermostat controller but the equations are arbitrary. The only requirement for the functions was that one should increase and the other should decrease. When I tried solving the model three events were triggered correctly and then the values of boolean variables became incorrect and since then no events were activated. I found out that the reason was the following: the decreasing function stopped right at the boundary. An event was triggered, the decreasing function was switched off and the increasing one was swithed on. The value of the output variable was already above the boundary, but the boundary flag showed that the boundary was only crossed from above to below. The next time it was crossed the boolean variable took such a value as if this time the boundary was crossed from below to above, which was wrong. So I added a flag to the boundaries indicating in which direction it was last crossed. Using the value of this flag and the value returned by IDA&#039;s function IDAGetRootInfo I check for crossings of such kinds and now all the boundaries are processed correctly. I will commit as soon as I put this code in order.&lt;br /&gt;
&lt;br /&gt;
The screenshot of the plot for the test model:&lt;br /&gt;
&lt;br /&gt;
[[File:Plot_event.png]]&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot_event.png&amp;diff=4270</id>
		<title>File:Plot event.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot_event.png&amp;diff=4270"/>
		<updated>2013-07-08T07:29:28Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Plot.png&amp;diff=4267</id>
		<title>File:Plot.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Plot.png&amp;diff=4267"/>
		<updated>2013-07-08T07:21:12Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: uploaded a new version of &amp;amp;quot;File:Plot.png&amp;amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4260</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4260"/>
		<updated>2013-07-01T13:57:28Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 24 - June, 30.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished the work on the compiler part: arrays of EVENTs are now created correctly; I changed CopyInstance so as it handles instances containing events, wrote code for merging instances referenced by events, changed some switches to handle event instances.&lt;br /&gt;
* Changed the C++ layer so as to handle event instances. Events are shown correctly in the PyGTK GUI now.&lt;br /&gt;
* Added one more model and some asserts and output to the test.&lt;br /&gt;
* Added the OTHERWISE case to events which will define the model behaviour at continious-time frame when no events are triggered.&lt;br /&gt;
* Started working on problem analysis. Created the needed data structures and added events to lists in the structures which represent the problem being analyzed. Added flags indicating if the relation/logical relation/etc. is referenced by some events.&lt;br /&gt;
* Added a simple test for the analysis part.&lt;br /&gt;
* Wrote code for analyzing events in the configure_conditional_problem function which is called at the end of problem analysis. Wrote code for passing the events lists into the slv_system_t structure.&lt;br /&gt;
* Events inside WHEN statements are switched on and off in configure_conditional_problem depending on the values of logical variables used by WHENs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4257</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4257"/>
		<updated>2013-06-24T06:31:59Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 17 - June, 23.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* I created a new structure StateEVENT for storing the information about the statement and wrote the functions for working with this structure;&lt;br /&gt;
* wrote a new parser rule for the new statement;&lt;br /&gt;
* created a new type EVENT (events will be implemented as instances so they should have a type description);&lt;br /&gt;
* created a new type of instance for events EventInstance;&lt;br /&gt;
* added a new field to structures of those instances which can be referenced by events. This field will store pointers to all events which reference the instance;&lt;br /&gt;
* wrote code for instantiating events. It includes the following: checking the events and making sure that all needed instances exist, creating an event instance and creating lists of pointers to relations, submodels, etc.&lt;br /&gt;
* wrote code for destroying event instances.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4205</id>
		<title>User:Ksenija</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=User:Ksenija&amp;diff=4205"/>
		<updated>2013-06-17T13:57:18Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&#039;&#039;&#039;Ksenija Bestuzheva&#039;&#039;&#039;&lt;br /&gt;
&amp;lt;br&amp;gt;Development branch: {{srcbranchdir|ksenija|}}&lt;br /&gt;
&lt;br /&gt;
Student in the State University of Managament (Moscow), studying applied mathematics and information technology. &lt;br /&gt;
&lt;br /&gt;
I have some knowledge of mathematical analysis, linear algebra, complex variable theory, differential equations, mathematical statistics, control theory. I have experience with C, C++, Java programming through my studies.&lt;br /&gt;
&lt;br /&gt;
==GSOC2013==&lt;br /&gt;
&lt;br /&gt;
===Project plan.===&lt;br /&gt;
&lt;br /&gt;
* Community bonding period&lt;br /&gt;
** May 23 – June 17. Discuss the project in a more detailed way. Think over the plan, discuss some complicated cases like nested events, events inside whens, defining the direction of the boundaries, etc.&lt;br /&gt;
* Parser changes&lt;br /&gt;
** June 17 – June 25. Create the parser rule and the structure for storing the new statement. Write code for verifying the new statement, create the event type description, generate the event names.&lt;br /&gt;
** June 25 – July 8. Write test models and test cases, fix bugs (if any).&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** July 8 – July 13. Process the event statement in the instantiator. Make all checks which can&#039;t be done before. Make sure that the needed variables and relations exist.&lt;br /&gt;
** July 13 – July 24. Create the event instances. Find all needed conditions, create pointers between relations, assignments and events. This may require a new compiler pass or may be it is better to do it in one of the already existing passes (together with WHENs, may be?).&lt;br /&gt;
** July 24 – July 27. Finish the work with the compiler. Make sure that all cases are processed correctly.&lt;br /&gt;
** July 27 – August 2. Test, fix bugs (if any). &lt;br /&gt;
The needed changes in the compiler should be done by mid-term evaluation.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** August 2 – August 16. Create the lists of events in the problem data structure. Add the needed flags to relations and assignments. Write code for switching the flags on and off at the boundaries.&lt;br /&gt;
** August 16 – August 20. Test, fix bugs (if any).&lt;br /&gt;
* Changes in the solver&lt;br /&gt;
** August 20 – September 6. Change the boundary crossing function. After solving the logical relations go through the events list and change the flags using the written before function. Solve the system and reset the flags. Some more checks for system consistency are likely to be required.&lt;br /&gt;
** September 6 – September 16. Test, fix bugs (if any). Add more models which use the new functionality.&lt;br /&gt;
* Completing the project&lt;br /&gt;
** September 16 – September 23. Final testing, writing documentation.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Variable structure modeling in ASCEND. Plan for summer of code 2013 and my final-year project.==&lt;br /&gt;
&lt;br /&gt;
===The goal.===&lt;br /&gt;
&lt;br /&gt;
====General description of the problem.====&lt;br /&gt;
&lt;br /&gt;
Currently ASCEND solves problems with region switching behavior pretty well. But it still doesn&#039;t have good means for describing and solving models with changes happening exactly at the boundaries (which may also be one-sided - e. g. hysteresis). These may be solving some boundary equations or assigning values to some variables.&lt;br /&gt;
&lt;br /&gt;
====Example problems.====&lt;br /&gt;
&lt;br /&gt;
Some example problems are given on this page: http://ascend4.org/Event_handling. Some more interesting examples may concern encoding analog signals into digital signals (see https://en.wikipedia.org/wiki/Delta-sigma_modulation). I am still searching for more examples.&lt;br /&gt;
&lt;br /&gt;
===Syntax.===&lt;br /&gt;
&lt;br /&gt;
====Requirements.====&lt;br /&gt;
&lt;br /&gt;
The syntax should enable the user to 1) state which actions should be performed at the boundary 2) associate them with an exact boundary 3) state in which direction the boundary should work (for one-sided boundaries). And, as usual, the syntax should be unambigious, flexible and clear.&lt;br /&gt;
&lt;br /&gt;
====Proposed syntax.====&lt;br /&gt;
&lt;br /&gt;
So, I think that the new statement will look something like:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
EVENT (condition)&lt;br /&gt;
  (* Some equations solved or assignments done when the condition becomes true *)&lt;br /&gt;
END EVENT&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We will need a way to define in which direction the boundary is crossed. For the user it can be something like this:&lt;br /&gt;
condition in the form x == 0 means two-sided boundaries;&lt;br /&gt;
conditions in the form x &amp;gt;= 0 or x &amp;lt;= mean one-sided boundaries: the actions associated with these boundaries will be performed not every time when x crosses 0, but only when the corresponding condition from false becomes true. &lt;br /&gt;
&lt;br /&gt;
One of the use cases involves accessing the value of a variable before the boundary and after the boundary in one equation (for example, v(+) = v(-)*0.9). My suggestion is to use auxiliary variables for such models. Then the model would be written in the following way:&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
v, v1 IS_A speed;&lt;br /&gt;
t IS_A time; &lt;br /&gt;
g IS_A acceleration;&lt;br /&gt;
DERIVATIVE OF v WITH t;&lt;br /&gt;
x IS_A distance;&lt;br /&gt;
v = der(x,t);&lt;br /&gt;
der(v,t) = g;&lt;br /&gt;
v1 = v;&lt;br /&gt;
EVENT (x == 0)&lt;br /&gt;
   v := v1*0.9;&lt;br /&gt;
END EVENT;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementation.===&lt;br /&gt;
&lt;br /&gt;
ASCEND is efficient with large models, and this work shouldn&#039;t break this characteristic. So we need the code to be fast and efficient.&lt;br /&gt;
&lt;br /&gt;
ASCEND already has some functionality for variable structure systems, so some code should be reused. As in WHENs, the relations will be compiled regardless of which values the logical variables have at any given moment. One more boolean child will be added to relations: it will indicate if the relation is a boundary equation or an ordinary one. Boundary equations should also store pointers to the boundaries which they are associated list. Still need to think what to do with assignments. Or may be we can use constant assignments so as not to break the rule that assignments are used only in methods.&lt;br /&gt;
&lt;br /&gt;
In IDA we can use the code for boundary crossing as the basis. But instead of just reconfiguring the system and reinitializing IDA we should detect if there are any events associated with the boundary and, if there are, perform the needed actions.&lt;br /&gt;
&lt;br /&gt;
==GSOC2012==&lt;br /&gt;
&lt;br /&gt;
===Project description.===&lt;br /&gt;
&lt;br /&gt;
The goal of the project is to add new syntax for derivatives which will improve the capabilities of ASCEND in dynamic modelling and increase usability. Currently defining derivatives in ASCEND is not enough intuitive and convenient: for the user the process consists of, firstly, defining usual variables by using the IS_A statement, and then stating that one variable is the derivative of the other. With the new syntax the derivative of x in respect to t would look like der(x,t) and by using this expression new variables would be created automatically. Corresponding changes to the solver API would need to be made. &lt;br /&gt;
&lt;br /&gt;
=== Project plan. ===&lt;br /&gt;
&lt;br /&gt;
* Parser changes&lt;br /&gt;
** Generate names of the type and of the new variable&lt;br /&gt;
** Create a unique type description for a derivative&lt;br /&gt;
** Add all needed checks&lt;br /&gt;
** Append a new IS_A statement to the model’s statements.&lt;br /&gt;
* Changes in the instantiator&lt;br /&gt;
** Add DerInfo to RealAtomInstance.&lt;br /&gt;
** Process the ‘der’ expression.&lt;br /&gt;
** Do all checks which can’t be done when creating the type description.&lt;br /&gt;
* Changes in the problem analysis function&lt;br /&gt;
** Change the analysis function so that it would use the new relationship between variables and their derivatives.&lt;br /&gt;
* Changes in the solvers.&lt;br /&gt;
** Change IDA so that it would use the results of the work of analysis function fully. &lt;br /&gt;
** Write the analysis function for LSODE using the results of the work of analysis function.&lt;br /&gt;
** Write the analysis function for DOPRI5 using the results of the work of analysis function.&lt;br /&gt;
&lt;br /&gt;
=== Todos for the near future ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 9.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Create a type description for a derivative atom instance.&#039;&#039;&#039; I have already started writing some code in my working copy a few days ago. I have written a function CreateDerivAtomTypeDef which is rather similar to CreateAtomTypeDef, but a derivative atom type description is always real (the types of the state and independent variables are checked before calling CreateDerivAtomTypeDef), refines solver_var and keeps pointers to the type descriptions of state and independent variables. The dimension is defined by using DiffDimensions(dimension of the state variable, dimension of the independent variable. CreateDerivAtomTypeDef is called only if the corresponding type with the name that is generated for it is not found in the type library, so the types will be unique. I think that a function which finds a type by the value of the string which a pointer points to, not by a pointer itself, will be needed for finding this type and the type solver_var. The childlist of the derivative type can’t be formed in the usual way, because this type is created when creating the model type is still in process and the derivative atom’s children would mess up with the model’s children. I suppose that the children can be just copied from solver_var – now I can’t think of any reasons why this may be not the right way to form the childlist.&lt;br /&gt;
# &#039;&#039;&#039;Make der(der(x,t),t) expressions possible.&#039;&#039;&#039; May be it should have been done earlier, but doing it now is also ok, I hope. The parser rule will be changed to der(expr,varlist). Expr can be either of type e_var or e_der. The function DoDer in typedef.c will check the type of expr, and if it is e_der, it will call DoDer for this expr and then replace the name of the state variable with the generated name.&lt;br /&gt;
# &#039;&#039;&#039;Change the independent statement.&#039;&#039;&#039; Add a special flag ‘independent’ to the solver_var.&lt;br /&gt;
# &#039;&#039;&#039;Add DerInfo to RealAtomInstance.&#039;&#039;&#039; I will need to think about the implementation.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Finish changing the functions from typedef.c&#039;&#039;&#039; so that they would use the new names.&lt;br /&gt;
# &#039;&#039;&#039;Create arrays of derivatives.&#039;&#039;&#039; My idea is that such variables as der(x[i],t[j]) can be instantiated as der(x,t)[i][j]. So that to make everything clearer for the user the information about which of the arguments is an array could be added to the string form of the name, for example: der(x[set],t[set]). So it would be easily distinguished from der(x[i][j],t), and so on. There should&#039;t be any problems with setting the DerInfo because the arguments of the derivatives are also stored in the varlist in the NameUnion. I will need to draw attention to the case when some of the derivative arguments are created inside loops.&lt;br /&gt;
# &#039;&#039;&#039;Test&#039;&#039;&#039; everything that has been done on this stage.&lt;br /&gt;
# &#039;&#039;&#039;Change the instantiator&#039;&#039;&#039; in order to process new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 13.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_find_indep_var&#039;&#039;&#039;: with the use of the diffvars code it could find the independent variable in a much simpler way.&lt;br /&gt;
# &#039;&#039;&#039;Change integrator_analyse_ode&#039;&#039;&#039; so that it would use the diffvars structure. After it is done the new derivatives should work with all the currently available in ASCEND ODE/DAE solvers.&lt;br /&gt;
# Think about &#039;&#039;&#039;some more possible refinements&#039;&#039;&#039; of problem analysis?&lt;br /&gt;
&lt;br /&gt;
===Weekly reports.===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 21 - May, 27.&#039;&#039;&#039; &lt;br /&gt;
&lt;br /&gt;
* I had some problems with my laptop, so I installed Linux Ubuntu and everything that is needed for ASCEND on my desktop. &lt;br /&gt;
* Created the derivative type description.&lt;br /&gt;
* Generated an IS_A statement. &lt;br /&gt;
* Nested der expressions are now parsed successfully.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;May, 28 - June, 3.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added some checks for ders in the methods section. Only those ders are accepted which are also used in the declarative section.&lt;br /&gt;
* Added a case for expressions of type e_der to EvaluateExpr and to some other functions, mostly those which deal with relations. Now models with ders are instantiated successfully.&lt;br /&gt;
* Modifyed DoDer (the function where the name and the IS_A statement are generated) so as to support derivatives of array elements. Now if a model contains such a derivative, an array of derivatives is created having the same cardinatily as the array containing the state variable does.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 4 - June, 10.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Thought about derivatives in those statements where names (not expressions) are required. Discussed with mentors how to modify the grammar rule so as to make it unambigious and to cover all possible cases. Also discussed the naming of the derivatives.&lt;br /&gt;
* Added the new element to NameUnion which stores information about the arguments of the derivative. Updated name.c, name.h, nameio.c, nameio.h.&lt;br /&gt;
* Started making changes to the functions from typedef.c which process derivatives using the new names.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 11 - June, 17.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Finished changing functions from typedef.c using the new names.&lt;br /&gt;
* Implemented arrays of derivatives.&lt;br /&gt;
* Added a test for the parser changes.&lt;br /&gt;
* Fixed some bugs.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 18 - June, 24.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Improved derivatives of arrays so that they would contain only those variables which are mentioned in the model. So, if derivatives of some array elements are not used in the model, they wouldn&#039;t be created.&lt;br /&gt;
* Epic fail! After a discussion with my mentors we decided that the derivatives should be declared explicitly.&lt;br /&gt;
* Created a new statement: DERIVATIVE fvarlist WITH fname (WITH fname is optional). For each variable from fvarlist a derivative name is created, added to the new varlist, and an IS_A statement for this new varlist is created. After the derivatives are declared they can be accessed by names like der(x) and der(x,t).&lt;br /&gt;
* Added a function to typedef.c which generates the type for the IS_A statement. Modified the function which previously generated the der variables: now the only thing it does is extending the list of derivative arguments if it comes across a derivative with a single argument and generating the symchar. &lt;br /&gt;
* Added a function which finds the type for nested derivatives (or generates one). &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;June, 25 - Jule, 1.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Created a compound statement ISDER which contains a list of IS_A statements. It is needed so as to allow variables of different types in the varlist.&lt;br /&gt;
* Added some new test models, updated the tests.&lt;br /&gt;
* Created struct DerInfo which stores pointers to state and independent variables (for derivatives), and to derivatives (for state and independent variables). All RealAtomInstances contain this struct. If a variable is not a derivative, state or independent variable, its DerInfo is NULL.&lt;br /&gt;
* Wrote functions which create DerInfo and get some information from it. &lt;br /&gt;
* Added a search to MakeInstance. If the variable that is going to be instantiated is a derivative, try to find a derivative of the same state variable with respect to the same independent variable. If found, the instance is not created and the instance which was found is linked to parent. If not found, instantiate the variable and set DerInfo.&lt;br /&gt;
* Implemented merges of independent variables. Still need to work on one case (which requires merging of derivatives).&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 2 - July, 8.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Implemented merges of state variables and derivatives.&lt;br /&gt;
* Changed FindInstances. If a model contains merges of state or independent variables, the derivative symchar may be different from the one which is added to the childlist. So, a derivative is now found by the derinfo of its state and independent variables, not by its symchar.&lt;br /&gt;
* In typedef.c replaced errors when a derivative can&#039;t be found in the childlist with warnings.&lt;br /&gt;
* Changed the search for a derivative which is done before creating the derivative instance. The new search makes it possible to avoid some problems with merges of arrays.&lt;br /&gt;
* Fixed some problems.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 9 - July, 15.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Removed some unneeded code. Added more comments.&lt;br /&gt;
* Fixed some problems in the compiler. Added more tests for the new syntax.&lt;br /&gt;
* Added user&#039;s documentation for the der syntax.&lt;br /&gt;
* Created new files k12_analyze.c and k12_diffvars.c with functions which build the diffvars lists and the derivative sequences using the new pointers between instances.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 16 - July, 22.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Changed functions from integrator.c: now they use the diffvars structure.&lt;br /&gt;
* Added new models. Rewrote some existing models with the new syntax.&lt;br /&gt;
* Added more tests.&lt;br /&gt;
* Added switching between der and ode_id syntax to the GUI.&lt;br /&gt;
* Converted the documentation file into LyX.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 23 - July, 29.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a test for DOPRI5.&lt;br /&gt;
* Added more models using the new syntax.&lt;br /&gt;
* Fixed a few bugs in the integrators.&lt;br /&gt;
* Read the documentation of SWIG and some books about C++ and Python.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;July, 30 - August, 05.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a &#039;der&#039; function which can be called from the Python layer to access derivatives from Python scripts given the object wrappers on the derivative arguments.&lt;br /&gt;
* Fixed some bugs in the compiler.&lt;br /&gt;
* Wrote one more test model for the compiler.&lt;br /&gt;
* Added some error messages.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;August, 06 - August, 12.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
* Added a button for viewing DerInfo to the var properties window.&lt;br /&gt;
* Worked on updating the derivative types if the derivative arguments are refined or merged.&lt;br /&gt;
* Updated the tests: added some new ones and changed the relations so that they are now dimensionally correct.&lt;br /&gt;
* Fixed a few minor problems in the compiler.&lt;br /&gt;
* Added more comments.&lt;br /&gt;
&lt;br /&gt;
===Final report.===&lt;br /&gt;
&lt;br /&gt;
Suming up the result of the work, the following goals have been achieved during this summer:&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;New syntax&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
New derivative syntax has been implemented:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y WITH t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=a4c&amp;gt;&lt;br /&gt;
DERIVATIVE OF x,y;&lt;br /&gt;
INDEPENDENT t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As a result of the above statements new variables are created: der(x,t) and der(y,t) which automatically become derivatives of x and y correspondingly. The use of new syntax is described in the documentation in a more detailed way: {{srcbranch|ksenija|doc/howto-dersyntax.lyx}} &lt;br /&gt;
&lt;br /&gt;
There are just a few changes which will need to be made so as to support partial and higher-order derivatives. I will describe them because it may be useful for those who may want to implement partial and higher-order derivatives in the future.&lt;br /&gt;
&lt;br /&gt;
Higher-order derivatives are now fully implemented on the compiler level as nested derivatives. For example, der(der(x,t),t) stores pointers to der(x,t) and t and is a second order derivative of x.&lt;br /&gt;
&lt;br /&gt;
This summer I didn&#039;t care about syntax like der(x,t,t). My thought is that in this case a list of independent variable instances in DerInfo could be replaced with a list of lists of instances, where the length of each list is equal to the order of the derivative.&lt;br /&gt;
&lt;br /&gt;
The implementation of the new syntax contains some first steps towards supporting partial derivatives on the compiler level. Arrays of derivatives will need to be created for derivatives with respect to arrays. The same is already done for arrays of state variables, so actually there will be nothing new. Functions DoDerIsa and DoDer from typedef.c and ExecuteISA and MakeInstance from instantiate.c will need to be changed.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Pointers between instances&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
All RealAtomInstances have now a pointer to a struct DerInfo. It stores gl_lists of state, independent variables, derivatives with respect to the instance and derivatives of the instance. &lt;br /&gt;
&lt;br /&gt;
When some instances are merged or refined, the derivatives connected with them are also merged or refined. There can never be two derivative instances for the same state and independent variable, and the type of the derivative always matches the types of the arguments. So, the user doesn&#039;t have to care about this.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Analysis&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
There are now two problem analisys functions: the old and the new one. The new function builds the diffvars structure using the pointers between instances. Everything that is needed for the new analyze function is contained in files k12_analyze and k12_diffvars ({{srcbranch|ksenija|ascend/system/k12_analyze.h}} and {{srcbranch|ksenija|ascend/system/k12_diffvars.h}}). The global variable g_use_dersyntax defined in {{srcbranch|ksenija|ascend/system/system.h}} can be used to choose between the two functions. &lt;br /&gt;
&lt;br /&gt;
The integrators now use the diffvars structure. The actions needed for building the derivative sequences are no longer repeated.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;UI changes&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Switching between the old and the new derivative syntax can be done using Tools - Use the der() syntax button in the PyGTK GUI. The needed option should be chosen before pressing Solve or Integrate. Otherwise the diffvars structure won&#039;t be built and user will have to reload the model.&lt;br /&gt;
&lt;br /&gt;
[[File:Use_der_syntax.png|200px|thumb|center|The &#039;Use the der() syntax&#039; switch in the PyGTK GUI]]&lt;br /&gt;
&lt;br /&gt;
The DerInfo can be viewed by pressing the DerInfo button in the variable properties window.&lt;br /&gt;
&lt;br /&gt;
[[File:Derinfo.png|200px|thumb|center|The DerInfo window]]&lt;br /&gt;
&lt;br /&gt;
Derivatives can be accessed from Python scripts by using the &#039;der&#039; function, e.g.:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=py&amp;gt;&lt;br /&gt;
x = M.x&lt;br /&gt;
y = M.y&lt;br /&gt;
print &amp;quot;Der(x,y) = &amp;quot;,float(der(x,y))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where &amp;lt;tt&amp;gt;M&amp;lt;/tt&amp;gt; is a simulation Python object (see {{srcbranch|ksenija|models/ksenija/example.py}})&lt;br /&gt;
&lt;br /&gt;
== Ideas for the new syntax for models with hysteresis.==&lt;br /&gt;
&lt;br /&gt;
The difficulty which I came across when trying to write some models with hysteresis was that I couldn&#039;t set the state of the system, on which its behavior depends. It should change with time, but the WHEN statement is not suited for this, I think.&lt;br /&gt;
&lt;br /&gt;
I have three ideas about the new syntax. I will illustrate the use of the proposed statements with a simple house heating model suggested by John Pye. Indoor temperature &#039;set&#039; to ~20 °C, and then thermal losses causing heat to leak out according to a thermal resistance equation. Then, a heater turning on when temperature is below 18 °C, and off when above 20 °C.&lt;br /&gt;
&lt;br /&gt;
The first idea is to make a statement that makes it possible to change the value of a variable or to switch the status of a relation (active or inactive) when some conditions are satisfied. For example, in the house heating model it may be setting the initial temperature T0 to current temperature T when T reaches 20 °C, and then T0 will stay the same until T drops to 18. Whether the heater is turned on or off will depend on T0. Or it may be possible to change the state of the heater and the expression for T directly using the same statement without changing T0. I think that such syntax can help to describe any system with hysteresis and it is intuitive. However, this idea may have some disadvantages. &lt;br /&gt;
&lt;br /&gt;
The second idea is to save the value of the last extremum (or root, there is not much difference) of some function. So T0 will be the last extremum of T, and the equation for T will change when T is below 18 or above 20, and when T is between 18 and 20 the equation will depend on T0. Though I suppose that the first variant is better because it is more general.&lt;br /&gt;
&lt;br /&gt;
The third possibility is an operator that returns the value of a variable after the last boundary crossing. If it differs from the current value, it is also considered as a boundary crossed. This operator can be used to form a condition on which the current value of T0 will depend. For example, when T0 after the last boundary crossing is below 18 and T is above 20, T0 = T is used. When T0 after the last boundary crossing is above 20 and T is between 18 and 20, T0 is equal to T0 after the last boundary crossing, and so on. &lt;br /&gt;
&lt;br /&gt;
=== Response from John ===&lt;br /&gt;
&lt;br /&gt;
I think that ASCEND does have the notion of state -- CONDITIONAL statements, combined with the logical variables and relations, do provide a way to infer the state of a model from the current values of the conditional variables.&lt;br /&gt;
&lt;br /&gt;
What seems to be lacking currently is the ability to hold any &#039;&#039;additional&#039;&#039; state information anywhere else, such as with &#039;sticky&#039; (or &#039;memory&#039;) logical variables that retain their value from previous steps, and are only triggered to change when &#039;&#039;events&#039;&#039; happen.&lt;br /&gt;
&lt;br /&gt;
Your first idea, I think, corresponds to what is already possible with WHEN. That function uses the values of logical variables to turn relations on and off. The syntax is fairly horrible, but the semantics are there (and I would like to try to fix the syntax one day).&lt;br /&gt;
&lt;br /&gt;
Your second and third ideas essentially relate to adding some form of &#039;memory&#039; function in ASCEND. I think that the ideas you put forward would work with the thermostat use-case, but are possibly not general enough to warrant being implemented into the language.&lt;br /&gt;
&lt;br /&gt;
One possibly-general approach that to these problems that we previously put forward was the idea an &#039;EVENT&#039; statement that was triggered by some kind of boundary-crossing, that causes a METHOD to be run. In that method, we could potentially do all sorts of different things, such as reversing the velocity of a ball when it bounces, etc. We could also switch the value of boolean state variables, such as &amp;quot;heating_on := TRUE&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
One thing that is lacking in that approach, however, is the ability to write boundary equations, eg &amp;quot;fuel_rate(+) = fuel_rate(-) +  5 {g/s}&amp;quot;. In other words, it would be great to be able to access the values of the variables *before* the boundary, and use those to write equations that allow us to set the state *after* the boundary.&lt;br /&gt;
&lt;br /&gt;
The idea of adding boundary equations makes the whole thing much harder. In effect, one would need to launch a mini [[QRSlv]] or similar to evaluate and solve all of the boundary equations. The &amp;quot;IDACalcIC&amp;quot; solver does this task currently, but is quite limited on the kinds of initial conditions that it supports. A more flexible set would require us to write our own initial conditions code.&lt;br /&gt;
&lt;br /&gt;
Here are some use-cases for this stuff think I think are good to ponder -- basically the whole topic of &#039;event handling&#039; and &#039;hybrid simulation&#039;.&lt;br /&gt;
* thermostat controller, as previously discussed&lt;br /&gt;
* a bouncing ball that *instantly* reverses its velocity (or v(+) = -v(-) * 0.9, perhaps) when hitting the ground. Note that Leon&#039;s approach uses a springy floor, instead of the instant velocity reversal approach. Sometimes we don&#039;t want to have to add this extra physics to our simulation.&lt;br /&gt;
* simulation of a logic circuit, with flip-flops, and gates, and delays -- discrete state simulation&lt;br /&gt;
* a flow rate controller that increments flow in fixed steps when certain thresholds are passed.&lt;br /&gt;
* a tank becoming full and starting to overflow&lt;br /&gt;
* a vessel with an inlet in the side, and an outlet protruding into the pipe from above; if the level is above the outlet, liquid comes out; if the level is below, gas (or nothing) comes out. &#039;sliding mode&#039; is when a system like this gets stuck on the boundary or oscilates rapidly across it. how do we deal with that?&lt;br /&gt;
&lt;br /&gt;
Note also that in our current ASCEND, we have all boundaries being explicitly stated, through CONDITIONAL statements. In future, we would like at least some boundaries to be automatically created, eg when you write &amp;quot;y = abs(x-5) + 1&amp;quot;, you would like your solver to add a boundary at &amp;quot;x - 5 = 0&amp;quot;, so that you ensure an accurate solution as the solution passes by the cusp.&lt;br /&gt;
&lt;br /&gt;
-- [[User:Jpye|Jpye]] 05:05, 10 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
I may have explained my idea not clear enough. I didn&#039;t mean that the statement which I described first should be similar to WHEN, the difference is that after the status is switched it remains the same even if the condition becomes false. So it is also some sort of &#039;remembering&#039; previous states. Though the EVENT statement seems to be more general and to give the user more opportunities.&lt;br /&gt;
&lt;br /&gt;
As far as I understand, writing boundary equations will require one more new statement, won&#039;t it?&lt;br /&gt;
&lt;br /&gt;
So is the variant you described already accepted and can I start working on the implementation?&lt;br /&gt;
&lt;br /&gt;
Today I have looked through ida.c and some other files one more time in order to learn more about how initial conditions are calculated.&lt;br /&gt;
&lt;br /&gt;
--[[User:Ksenija|Ksenija]] 00:21, 11 February 2012 (EST)&lt;br /&gt;
&lt;br /&gt;
[[Category:ASCEND Contributors]]&lt;br /&gt;
[[Category:GSOC2012]]&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Solow_plot.png&amp;diff=4171</id>
		<title>File:Solow plot.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Solow_plot.png&amp;diff=4171"/>
		<updated>2013-04-23T14:14:25Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
	<entry>
		<id>https://ascend4.org/index.php?title=File:Solow.png&amp;diff=4170</id>
		<title>File:Solow.png</title>
		<link rel="alternate" type="text/html" href="https://ascend4.org/index.php?title=File:Solow.png&amp;diff=4170"/>
		<updated>2013-04-23T14:12:17Z</updated>

		<summary type="html">&lt;p&gt;Ksenija: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Ksenija</name></author>
	</entry>
</feed>